-
Notifications
You must be signed in to change notification settings - Fork 38
Deliver Eat Lemony #23
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?
Changes from all commits
a1631f6
8fb510a
4d8580f
d648ac6
280cf5f
00a2629
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 |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import React from 'react'; | ||
| import '../styles/components/makeorder.scss'; | ||
| import MakeOrderItem from './MakeOrderItem'; | ||
|
|
||
| function MakeOrder({ currentOrder, sendOrderToApi }) { | ||
|
|
||
| function renderOrder() { | ||
| return Object.values(currentOrder).map(currentorderitem => { | ||
| return <MakeOrderItem | ||
| currentorderitem={currentorderitem} | ||
| key={currentorderitem.id} | ||
| /> | ||
| }) | ||
| } | ||
|
|
||
| // create array of prices out of currentOrder object; | ||
| let orderKeys = Object.keys(currentOrder); | ||
| let orderPrices = [] | ||
| orderKeys.forEach(key => { | ||
| orderPrices.push(currentOrder[key].price) | ||
| }) | ||
|
|
||
| // reducer for total | ||
| let totalPrice = orderPrices.reduce((acc, item) => acc = acc + item, 0); | ||
| let orderPlusDelivery = totalPrice + 2.95; | ||
|
|
||
| return ( | ||
| <section className="customerOrder"> | ||
| <h2 className="customerOrder__title">Your Order</h2> | ||
| <ul className="customerOrder__order menu--settings">{renderOrder()} | ||
| <li>Delivery charge £2.95</li> | ||
| <li>Total: £{orderPlusDelivery}</li> | ||
| </ul> | ||
| <button className="customerOrder__submit" onClick={sendOrderToApi} type="submit">Place order</button> | ||
| </section> | ||
| ) | ||
| } | ||
| export default MakeOrder; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import React from 'react'; | ||
| import '../styles/components/makeorderitem.scss'; | ||
|
|
||
| class MakeOrderItem extends React.Component { | ||
| constructor(){ | ||
| super(); | ||
| } | ||
|
|
||
| render(){ | ||
|
|
||
| const pricedisplay = this.props.currentorderitem.price.toFixed(2); | ||
|
|
||
| return ( | ||
| <li> | ||
| <span>{this.props.currentorderitem.quantity}</span> * <strong>{this.props.currentorderitem.item}</strong> : £{pricedisplay} | ||
| </li> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| export default MakeOrderItem; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import React from 'react'; | ||
| import MenuItem from './MenuItem'; | ||
|
|
||
| class Menu extends React.Component { | ||
| constructor(){ | ||
| super(); | ||
|
|
||
| this.getCourse = this.getCourse.bind(this) | ||
| } | ||
|
|
||
| getCourse(course) { | ||
| // values = Object.values(this.props.menu); | ||
| // values.map(); | ||
| return this.props.menu.filter(menuitem => menuitem.category === course) | ||
| .map(menuitem => { | ||
|
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. Indentation here would make code easier to read |
||
| return <MenuItem | ||
| menuitem={menuitem} | ||
| key={menuitem.id} | ||
| receiveItemOrder={this.props.receiveItemOrder} | ||
| removeItemOrder={this.props.removeItemOrder} /> | ||
| }) | ||
| }; | ||
|
|
||
|
|
||
| render(){ | ||
| return ( | ||
| <ul className="menu menu--settings"> | ||
| <h2>Drinks</h2> {this.getCourse("drinks")} | ||
| <h2>Cakes</h2> {this.getCourse("cakes")} | ||
| <h2>Breakfast</h2> {this.getCourse("breakfast")} | ||
| </ul> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| export default Menu; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import React from 'react'; | ||
| import '../styles/components/menuitem.scss'; | ||
| import cx from 'classnames'; | ||
|
|
||
| class MenuItem extends React.Component { | ||
| constructor(){ | ||
| super(); | ||
|
|
||
| this.state = { | ||
| menuitemQuantity: 0, | ||
| added: false, | ||
| error: false | ||
| }; | ||
|
|
||
| this.handleClick = this.handleClick.bind(this) | ||
| this.handleSubmit = this.handleSubmit.bind(this) | ||
| this.itemOrder = this.itemOrder.bind(this) | ||
| } | ||
|
|
||
| handleSubmit(event) { | ||
| event.preventDefault(); | ||
| if (this.state.menuitemQuantity <= 0) { | ||
| this.setState({ | ||
| error: !this.state.error | ||
| }); | ||
| } else { | ||
| if (this.state.added === false) { | ||
| this.itemOrder(); | ||
| this.setState({ | ||
| added: !this.state.added | ||
| }); | ||
| } else { | ||
| this.props.removeItemOrder(this.props.menuitem.id); | ||
| this.setState({ | ||
| added: !this.state.added | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| handleClick(event) { | ||
| if (event.target.value === "-" && this.state.menuitemQuantity > 0) { | ||
| this.setState({ | ||
| menuitemQuantity: this.state.menuitemQuantity - 1 | ||
| }); | ||
| } else if (event.target.value === "+") { | ||
| this.setState({ | ||
| menuitemQuantity: this.state.menuitemQuantity + 1 | ||
| }); | ||
| if (this.state.error === true) { | ||
| this.setState({ | ||
| error: false | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| itemOrder() { | ||
| let price = (this.props.menuitem.price * this.state.menuitemQuantity); | ||
| const order = { | ||
| id: this.props.menuitem.id, | ||
| item: this.props.menuitem.name, | ||
| price: price, | ||
| quantity: this.state.menuitemQuantity, | ||
| }; | ||
| this.props.receiveItemOrder(order); | ||
| } | ||
|
|
||
| render(){ | ||
|
|
||
| const buttonclasses = cx('menuitem__submit', { | ||
| 'added': this.state.added, | ||
| '': !this.state.added | ||
|
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. think this can be removed as it does not add any classes |
||
| }); | ||
| const errorclasses = cx('menuitem__error', { | ||
| 'show--error': this.state.error, | ||
| '': this.state.error | ||
| }); | ||
|
|
||
| const pricedisplay = this.props.menuitem.price.toFixed(2); | ||
|
|
||
| return ( | ||
| <li className="menuitem"> | ||
| <form className="menuitem__form" onSubmit={this.handleSubmit}> | ||
|
|
||
| <div className="quantity"> | ||
| <input className="quantity__change" onClick={this.handleClick} value="-" type="button"/> | ||
| <input className="quantity__display" type="text" size='1' value={this.state.menuitemQuantity} readOnly/> | ||
| <input className="quantity__change" onClick={this.handleClick} value="+" type="button"/> | ||
| </div> | ||
|
|
||
| <div className="menuitem__details"> | ||
| <div className="menuitem__select"> | ||
| <label className="menuitem__item">{this.props.menuitem.name} | ||
| <span className="menuitem__price"> £{pricedisplay}</span> | ||
| </label> | ||
| <button type="submit" className={buttonclasses}></button> | ||
| <span className={errorclasses}>Please select quantity</span> | ||
| </div> | ||
|
|
||
| </div> | ||
|
|
||
|
|
||
| </form> | ||
| </li> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| export default MenuItem; | ||
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 might be easier to store orders in an object that uses ids as keys as it would make look ups much simpler