improvement: CreateTicket

This commit is contained in:
frab1t 2019-07-03 10:25:47 +02:00
parent 56c3c33650
commit ed94230f79
1 changed files with 23 additions and 13 deletions

View File

@ -8,11 +8,13 @@ import {
Input Input
} from 'reactstrap'; } from 'reactstrap';
import { config } from '../../config';
import Card from '../../components/Card'; import Card from '../../components/Card';
import DropDown from '../../components/DropDown'; import DropDown from '../../components/DropDown';
import Button from '../../components/Button'; import Button from '../../components/Button';
// TODO: improve form validation
class CreateTicket extends Component { class CreateTicket extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
@ -32,25 +34,22 @@ class CreateTicket extends Component {
this.handleInputChange = this.handleInputChange.bind(this); this.handleInputChange = this.handleInputChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this); this.handleSubmit = this.handleSubmit.bind(this);
this.API_URL= process.env.REACT_APP_API_URL;
} }
componentDidMount() { componentDidMount() {
fetch(`${this.API_URL}categories?type=ticketType`, { fetch(`${config.baseURL}categories?type=TicketType`, {
method: 'GET', method: 'GET',
headers: { 'Content-Type': 'application/json' } headers: { 'Content-Type': 'application/json' }
}) })
.then(res => res.json()) .then(res => res.json())
.then((json) => { .then((json) => {
this.setState({ this.setState({
categories: json categories: json.data
}); });
}) })
.catch((err) => { .catch((err) => {
this.setState({
error: err.message
});
}); });
} }
@ -69,23 +68,34 @@ class CreateTicket extends Component {
handleSubmit(event) { handleSubmit(event) {
event.preventDefault(); event.preventDefault();
const { ticketId, error, categories, ...rest } = this.state; // const { ticketId, error, categories, ...rest } = this.state;
if (!rest.firstName || !rest.lastName || !rest.email || !rest.message const { state } = this;
|| rest.type === '---' || !rest.subject) {
if (!state.firstName || !state.lastName || !state.email || !state.message
|| state.type === '---' || !state.subject) {
alert('Form is invalid'); alert('Form is invalid');
return; return;
} }
fetch(`${this.API_URL}tickets`, { const body = {
firstName: state.firstName,
lastName: state.lastName,
email: state.email,
type: state.type,
subject: state.subject,
message: state.message
}
fetch(`${config.baseURL}tickets`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(rest) body: JSON.stringify(body)
}) })
.then(res => res.json()) .then(res => res.json())
.then((json) => { .then((json) => {
this.setState({ this.setState({
ticketId: json.id ticketId: json.data.ticketId
}); });
}) })
.catch((err) => { .catch((err) => {