import React, { Component } from 'react'; import { Row, Col, Form, FormGroup, Label, Input } from 'reactstrap'; import Card from '../../components/Card'; import DropDown from '../../components/DropDown'; import Button from '../../components/Button'; class CreateTicket extends Component { constructor(props) { super(props); this.state = { ticketId: '', ticketErr: '', firstName: '', lastName: '', email: '', type: '', subject: '', message: '', categories: [] }; this.handleInputChange = this.handleInputChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); this.API_URL= process.env.REACT_APP_API_URL; } componentDidMount() { fetch(`${this.API_URL}categories?type=ticketType`, { method: 'GET', headers: { 'Content-Type': 'application/json' } }) .then(res => res.json()) .then((json) => { this.setState({ categories: json }); }) .catch((err) => { this.setState({ error: err.message }); }); } handleInputChange(event) { const target = event.target; const name = target.name; const value = target.type === 'checkbox' ? target.checked : target.value; this.setState({ [name]: value }); } handleSubmit(event) { event.preventDefault(); const { ticketId, error, categories, ...rest } = this.state; if (!rest.firstName || !rest.lastName || !rest.email || !rest.message || rest.type === '---' || !rest.subject) { alert('Form is invalid'); return; } fetch(`${this.API_URL}tickets`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(rest) }) .then(res => res.json()) .then((json) => { this.setState({ ticketId: json.id }); }) .catch((err) => { this.setState({ error: err.message }); }); } render() { if (this.state.ticketId) { return (

{`Ticket ID: ${this.state.ticketId}`}

); } else if (this.state.error) { return (

{this.state.error}

); } return (
); } } export default CreateTicket;