Skip to content
This repository was archived by the owner on Sep 3, 2021. It is now read-only.

Commit dae421c

Browse files
committed
feat(order): Added API call for submitting orders
1 parent 448d8ba commit dae421c

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

application/src/components/order-form/orderForm.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,51 @@ import React, { Component } from 'react';
22
import { Template } from '../../components';
33
import './orderForm.css';
44

5+
const ADD_ORDER_URL = "http://localhost:4000/api/add-order"
6+
57
class OrderForm extends Component {
68
constructor(props) {
79
super(props);
810
this.state = {
9-
item: "",
10-
quantity: 1
11+
order_item: "",
12+
quantity: "1"
1113
}
1214
}
1315

1416
menuItemChosen(event) {
15-
this.setState({ item: event.target.value });
17+
this.setState({ order_item: event.target.value });
1618
}
1719

1820
menuQuantityChosen(event) {
1921
this.setState({ quantity: event.target.value });
2022
}
2123

24+
submitOrder(event) {
25+
event.preventDefault();
26+
if (this.state.order_item === "") return;
27+
fetch(ADD_ORDER_URL, {
28+
method: 'POST',
29+
body: JSON.stringify({
30+
order_item: this.state.order_item,
31+
quantity: this.state.quantity
32+
}),
33+
headers: {
34+
'Content-Type': 'application/json'
35+
}
36+
})
37+
.then(res => res.json())
38+
.then(response => console.log("Success", JSON.stringify(response)))
39+
.catch(error => console.error(error));
40+
}
41+
2242
render() {
2343
return (
2444
<Template>
2545
<div className="form-wrapper">
2646
<form>
2747
<label className="form-label">I'd like to order...</label><br />
2848
<select
29-
value={this.state.item}
49+
value={this.state.order_item}
3050
onChange={(event) => this.menuItemChosen(event)}
3151
className="menu-select"
3252
>
@@ -45,7 +65,7 @@ class OrderForm extends Component {
4565
<option value="5">5</option>
4666
<option value="6">6</option>
4767
</select>
48-
<button type="button" className="order-btn">Order It!</button>
68+
<button type="button" className="order-btn" onClick={(event) => this.submitOrder(event)}>Order It!</button>
4969
</form>
5070
</div>
5171
</Template>

0 commit comments

Comments
 (0)