-
Notifications
You must be signed in to change notification settings - Fork 38
CP Delivereat #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
CP Delivereat #11
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,63 @@ | ||
| import React from 'react'; | ||
|
|
||
| import Menu from './Menu'; | ||
| import '../styles/App.scss'; | ||
|
|
||
| class App extends React.Component { | ||
| constructor(){ | ||
| super(); | ||
|
|
||
| this.state = {menuItems:[]}; | ||
| this.fetch = this.fetch.bind(this); | ||
| this.receiveOrder = this.receiveOrder.bind(this); | ||
| this.postOrder = this.postOrder.bind(this); | ||
| } | ||
|
|
||
| componentDidMount(){ | ||
| this.fetch() | ||
| } | ||
|
|
||
| receiveOrder(){ | ||
| this.setState(); | ||
| postOrder(this.state.order) | ||
| } | ||
|
|
||
| postOrder(order){ | ||
|
|
||
| fetch('http://localhost:8080/api/order', { | ||
| method: 'post', | ||
| body: JSON.stringify(order), | ||
| headers: { | ||
| 'Content-Type': 'application/json' | ||
| } | ||
| }).then(function(response) { | ||
| return response.json(); | ||
| }).then(data => { | ||
| console.log(data) | ||
| // handle response | ||
| }); | ||
|
|
||
|
|
||
| } | ||
|
|
||
| fetch(){ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would give this method a more descriptive name. Fetch is a little too ambiguous |
||
|
|
||
| fetch('/api/menu') | ||
| .then(res => res.json()) | ||
| .then(body => { | ||
| console.log(body) | ||
| this.setState({menuItems: body}) | ||
| console.log(this.state.menuItems); | ||
|
|
||
| }) | ||
|
|
||
| } | ||
|
|
||
| render(){ | ||
| return ( | ||
| <div> | ||
| Delivereat app | ||
| <Menu menu={this.state.menuItems} receiveOrder={this.receiveOrder}/> | ||
|
|
||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import React from "react"; | ||
|
|
||
|
|
||
| class Basket extends React.Component { | ||
| constructor() { | ||
| super(); | ||
| this.basketAdd = this.basketAdd.bind(this); | ||
| this.basketRemove = this.basketRemove.bind(this); | ||
| this.state = {order:{}} | ||
|
|
||
| } | ||
|
|
||
| basketAdd(foodItem){ | ||
| const newBasket = Object.assign({}, this.state.basket); | ||
| if (this.state.basket[foodItem.id]) { | ||
| newBasket[foodItem.id] += 1 | ||
| } else { | ||
| newBasket[foodItem.id] = 1 | ||
| } | ||
|
|
||
| this.setState({ basket: newBasket }, () => { | ||
| console.log(this.state); | ||
| }) | ||
| } | ||
|
|
||
| basketRemove(foodItem){ | ||
| const newBasket = Object.assign({}, this.props.basket); | ||
| if (this.props.basket[foodItem.id]) { | ||
| newBasket[foodItem.id] -= 1 | ||
| } else { | ||
| newBasket[foodItem.id] = 0 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it might make sense to use delete the current item from basket here rather than set quantity to 0. |
||
| } | ||
|
|
||
| this.props.setState({ basket: newBasket }, () => { | ||
| console.log(this.props.state); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did you mean |
||
| }) | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| render() { | ||
|
|
||
| return ( | ||
| <div> | ||
| <ul className="basket"> | ||
|
|
||
| {Object.entries(this.props.basket).map(([id, quantity]) => | ||
| <li key={id} > | ||
|
|
||
|
|
||
| <span>{quantity} x {id} </span> | ||
| {/* {this.props.menu[id]} */} | ||
| <button onClick={() => this.basketRemove(id)}>Remove>-</button> | ||
| <button>+</button> | ||
| </li> | ||
| )} | ||
|
|
||
| </ul> | ||
| <button>Place Order</button> | ||
| </div> | ||
|
|
||
| ) | ||
| } | ||
| } | ||
|
|
||
| export default Basket; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like the order function does not exist so this is likely to break