1
0
mirror of https://github.com/franjsco/tick3t synced 2025-01-14 18:37:04 +01:00

refactor: ViewTicket

This commit is contained in:
Francesco Esposito 2019-07-31 16:09:41 +02:00
parent c5d3138344
commit 115800905b

View File

@ -1,7 +1,7 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import { config } from '../../config'; import { viewTicket } from '../../api/tickets';
import Card from '../../components/Card'; import Card from '../../components/Card';
import Table from '../../components/Table'; import Table from '../../components/Table';
@ -11,11 +11,10 @@ class ViewTicket extends Component {
super(props); super(props);
this.state = { this.state = {
data: {}, data: '',
error: '', error: '',
isLoading: false, isLoading: false,
}; };
} }
componentDidMount() { componentDidMount() {
@ -23,25 +22,8 @@ class ViewTicket extends Component {
const { match: { params } } = this.props; const { match: { params } } = this.props;
fetch(`${config.baseURL}tickets/${params.ticketId}`) viewTicket(params.ticketId)
.then(response => { .then((json) => this.setState({ data: json.data, isLoading: false }))
if (!response.ok) {
throw new Error('Error API');
}
return response.json();
})
.then(json => {
console.log()
if (!json.success) {
throw new Error('Ticket Not found');
}
this.setState({
data: json.data,
isLoading: false
})
})
.catch(error => this.setState({ error, isLoading: false })); .catch(error => this.setState({ error, isLoading: false }));
} }
@ -50,9 +32,9 @@ class ViewTicket extends Component {
if (error) { if (error) {
return ( return (
<Card <Card
title="Error" title="Error"
footerLink={{path:"/", name:"Back to home"}} footerLink={{ path: "/", name: "Back to home" }}
> >
<p>{error.message}</p> <p>{error.message}</p>
</Card> </Card>