From a9fef41cfbd4ce29cbb4059686267793dac52196 Mon Sep 17 00:00:00 2001 From: Francesco Esposito <33671357+frsposito@users.noreply.github.com> Date: Wed, 31 Jul 2019 16:10:01 +0200 Subject: [PATCH] refactor: CreateTicket --- src/containers/CreateTicket/CreateTicket.js | 49 ++++++--------------- 1 file changed, 13 insertions(+), 36 deletions(-) diff --git a/src/containers/CreateTicket/CreateTicket.js b/src/containers/CreateTicket/CreateTicket.js index d97f4ec..bf4d2ed 100644 --- a/src/containers/CreateTicket/CreateTicket.js +++ b/src/containers/CreateTicket/CreateTicket.js @@ -7,9 +7,9 @@ import { Label, Input } 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 DropDown from '../../components/DropDown'; @@ -29,7 +29,8 @@ class CreateTicket extends Component { type: '', subject: '', message: '', - categories: [] + categories: [], + error: '', }; this.handleInputChange = this.handleInputChange.bind(this); @@ -39,19 +40,9 @@ class CreateTicket extends Component { componentDidMount() { - fetch(`${config.baseURL}categories?type=TicketType`, { - method: 'GET', - headers: { 'Content-Type': 'application/json' } - }) - .then(res => res.json()) - .then((json) => { - this.setState({ - categories: json.data - }); - }) - .catch((err) => { - - }); + getAllTicketType() + .then(json => this.setState({ categories: json.data })) + .catch(error => this.setState({ error })); } @@ -68,9 +59,7 @@ class CreateTicket extends Component { handleSubmit(event) { event.preventDefault(); - - // const { ticketId, error, categories, ...rest } = this.state; - + const { state } = this; if (!state.firstName || !state.lastName || !state.email || !state.message @@ -79,7 +68,7 @@ class CreateTicket extends Component { return; } - const body = { + const ticket = { firstName: state.firstName, lastName: state.lastName, email: state.email, @@ -88,25 +77,13 @@ class CreateTicket extends Component { message: state.message } - fetch(`${config.baseURL}tickets`, { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(body) - }) - .then(res => res.json()) - .then((json) => { - this.setState({ - ticketId: json.data.ticketId - }); + createTicket(ticket) + .then(json => { + this.setState( { ticketId: json.data.ticketId }); }) - .catch((err) => { - this.setState({ - error: err.message - }); - }); + .catch(error => this.setState({error: error.message})); } - render() { if (this.state.ticketId) { return (