1
0
mirror of https://github.com/franjsco/tick3t synced 2025-06-05 22:19:18 +02:00

refactor: TicketManager component

This commit is contained in:
Francesco Esposito
2019-07-31 16:10:45 +02:00
parent 9ad5722b5b
commit a602a949d3

View File

@@ -2,16 +2,16 @@ import React, { Component } from 'react';
import { Link, Redirect } from 'react-router-dom'; import { Link, Redirect } from 'react-router-dom';
import { import {
Row, Row,
Col, Col,
Form, Form,
FormGroup, FormGroup,
Label, Label,
Input Input
} from 'reactstrap'; } from 'reactstrap';
import { config } from '../../config'; import { getAllTicketStatus } from '../../api/categories';
import { getAuthHeader } from '../../utils/auth'; import { viewTicket, updateTicket } from '../../api/tickets';
import Card from '../../components/Card'; import Card from '../../components/Card';
import Table from '../../components/Table'; import Table from '../../components/Table';
@@ -27,7 +27,7 @@ class TicketManager extends Component {
data: {}, data: {},
categories: [], categories: [],
status: '', status: '',
message: '', note: '',
error: '', error: '',
updated: false, updated: false,
isLoading: false, isLoading: false,
@@ -42,7 +42,7 @@ class TicketManager extends Component {
this.setState({ isLoading: true }); this.setState({ isLoading: true });
const { match: { params } } = this.props; const { match: { params } } = this.props;
/*
fetch(`${config.baseURL}tickets/${params.ticketId}`) fetch(`${config.baseURL}tickets/${params.ticketId}`)
.then(response => { .then(response => {
if (!response.ok) { if (!response.ok) {
@@ -59,26 +59,15 @@ class TicketManager extends Component {
this.setState({ data: json.data, isLoading: false }) this.setState({ data: json.data, isLoading: false })
}) })
.catch(error => this.setState({ error, isLoading: false })); .catch(error => this.setState({ error, isLoading: false }));
*/
viewTicket(params.ticketId)
fetch(`${config.baseURL}categories?type=TicketStatus`) .then((json) => this.setState({ data: json.data, isLoading: false }))
.then((response) => { .catch(error => this.setState({ error, isLoading: false }));
if (!response.ok) {
throw new Error('Error API');
}
return response.json(); getAllTicketStatus()
}) .then((json) => this.setState({ categories: json.data }))
.then((json) => { .catch(error => this.setState({ error, isLoading: false }));
this.setState({
categories: json.data
});
})
.catch((err) => {
this.setState({
error: err.message
});
});
} }
handleInputChange(event) { handleInputChange(event) {
@@ -91,53 +80,35 @@ class TicketManager extends Component {
}); });
} }
handleSubmit(event) { handleSubmit(event) {
event.preventDefault(); event.preventDefault();
const data = this.state; const data = this.state;
const { match: { params } } = this.props; const { match: { params } } = this.props;
if (!data.type === '---' || !data.message) { if (!data.type === '---' || !data.note) {
alert('Form is invalid'); alert('Form is invalid');
return; return;
} }
const header = new Headers(); const ticket = {
ticketId: params.ticketId,
status: data.status,
note: data.note
};
header.append('authorization', getAuthHeader()); updateTicket(ticket)
header.append('Content-Type','application/json') .then(json => {
alert('Ticket Update');
fetch(`${config.baseURL}tickets/${params.ticketId}`, {
method: 'PUT',
headers: header,
body: JSON.stringify({
status: this.state.status,
note: this.state.message
})
})
.then(res => {
if(!res.ok) {
throw new Error('Error API');
}
return res.json();
})
.then((json) => {
alert("Ticket Updated");
this.setState({ this.setState({
updated: true updated: true
}); });
}) })
.catch((err) => { .catch(err => this.setState({ ticketErr: err.message }));
this.setState({
ticketErr: err.message
});
});
} }
render() { render() {
const { data, updated, isLoading, error} = this.state; const { data, updated, isLoading, error } = this.state;
if (error) { if (error) {
return ( return (
@@ -155,7 +126,7 @@ class TicketManager extends Component {
return ( return (
<Redirect to="/admin/" /> <Redirect to="/admin/" />
) )
} }
return ( return (
<div> <div>
@@ -170,7 +141,6 @@ class TicketManager extends Component {
<Link to="/admin/">Back to Ticket List</Link> <Link to="/admin/">Back to Ticket List</Link>
</Col> </Col>
</Row> </Row>
</Card> </Card>
<Card <Card
@@ -190,12 +160,12 @@ class TicketManager extends Component {
</Col> </Col>
</FormGroup> </FormGroup>
<FormGroup row> <FormGroup row>
<Label sm={2} className="font-weight-bold" for="message">Note</Label> <Label sm={2} className="font-weight-bold" for="note">Note</Label>
<Col sm={10}> <Col sm={10}>
<Input <Input
name="message" name="note"
type="textarea" type="textarea"
value={this.state.message} value={this.state.note}
onChange={this.handleInputChange} onChange={this.handleInputChange}
placeholder="Note" placeholder="Note"
/> />
@@ -205,11 +175,8 @@ class TicketManager extends Component {
<Col align="right"> <Col align="right">
<Button type="submit">Update Ticket</Button> <Button type="submit">Update Ticket</Button>
</Col> </Col>
</FormGroup> </FormGroup>
</Form> </Form>
</Card> </Card>
</div> </div>