1
0
mirror of https://github.com/franjsco/tick3t synced 2025-01-29 17:59:28 +01:00

refactor: CreateTicket

This commit is contained in:
Francesco Esposito 2019-07-31 16:10:01 +02:00
parent 115800905b
commit a9fef41cfb

View File

@ -7,9 +7,9 @@ import {
Label, Label,
Input Input
} from 'reactstrap'; } from 'reactstrap';
import { Link } from 'react-router-dom';
import { config } from '../../config'; import { createTicket } from '../../api/tickets';
import { getAllTicketType } from '../../api/categories';
import Card from '../../components/Card'; import Card from '../../components/Card';
import DropDown from '../../components/DropDown'; import DropDown from '../../components/DropDown';
@ -29,7 +29,8 @@ class CreateTicket extends Component {
type: '', type: '',
subject: '', subject: '',
message: '', message: '',
categories: [] categories: [],
error: '',
}; };
this.handleInputChange = this.handleInputChange.bind(this); this.handleInputChange = this.handleInputChange.bind(this);
@ -39,19 +40,9 @@ class CreateTicket extends Component {
componentDidMount() { componentDidMount() {
fetch(`${config.baseURL}categories?type=TicketType`, { getAllTicketType()
method: 'GET', .then(json => this.setState({ categories: json.data }))
headers: { 'Content-Type': 'application/json' } .catch(error => this.setState({ error }));
})
.then(res => res.json())
.then((json) => {
this.setState({
categories: json.data
});
})
.catch((err) => {
});
} }
@ -69,8 +60,6 @@ class CreateTicket extends Component {
handleSubmit(event) { handleSubmit(event) {
event.preventDefault(); event.preventDefault();
// const { ticketId, error, categories, ...rest } = this.state;
const { state } = this; const { state } = this;
if (!state.firstName || !state.lastName || !state.email || !state.message if (!state.firstName || !state.lastName || !state.email || !state.message
@ -79,7 +68,7 @@ class CreateTicket extends Component {
return; return;
} }
const body = { const ticket = {
firstName: state.firstName, firstName: state.firstName,
lastName: state.lastName, lastName: state.lastName,
email: state.email, email: state.email,
@ -88,25 +77,13 @@ class CreateTicket extends Component {
message: state.message message: state.message
} }
fetch(`${config.baseURL}tickets`, { createTicket(ticket)
method: 'POST', .then(json => {
headers: { 'Content-Type': 'application/json' }, this.setState( { ticketId: json.data.ticketId });
body: JSON.stringify(body)
}) })
.then(res => res.json()) .catch(error => this.setState({error: error.message}));
.then((json) => {
this.setState({
ticketId: json.data.ticketId
});
})
.catch((err) => {
this.setState({
error: err.message
});
});
} }
render() { render() {
if (this.state.ticketId) { if (this.state.ticketId) {
return ( return (