Skip to content
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

weekend za delivereat #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"test": "jest",
"dev": "webpack --mode development",
"build": "webpack --mode production"
"build": "webpack --mode production",
"start": "nodemon server.js"
},
"author": "",
"license": "ISC",
Expand Down
33 changes: 26 additions & 7 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,41 @@ const express = require('express');
const bodyParser = require('body-parser');
const app = express();

const {getMenu, getItem, postOrder, getOrders } = require('./storage')

app.use(bodyParser.json());
app.use('/static', express.static('static'));
app.set('view engine', 'hbs');

const menu = {
1: {
id: 1,
name: "Strawberry cheesecake",
price: 6
}
};

app.get('/', function(req, res){
res.render('index');
});

app.get('/menu', function(req, res){
menu = getMenu();
res.json(menu)
});

app.get('/menu/:itemId', function(req, res){
const menuItem = getItem(req.params.itemId)
if(req.params.itemId){
res.json(menuItem)
} else {
res.status(404).json({error: 'Lost in the fridge'});
}
})

app.post('/order', function(req, res){
const newOrder = req.body;
res.json(postOrder(newOrder))
})

app.get('/order', function(req, res){
orders = getOrders()
res.json(orders)
})

app.listen(8080, function(){
console.log('Listening on port 8080');
});
Binary file added src/cake1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 64 additions & 0 deletions src/components/AddOrderModal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React from 'react';

import '../styles/AddOrderModal.scss';

class AddOrderModal extends React.Component {
constructor(){
super();

this.state= {
quantity: 1,
}

this.handleClick = this.handleClick.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.handleChange = this.handleChange.bind(this);
}


handleClick(event) {
this.props.receiveModalCloseBtn();
this.setState({
quantity: 1,
})
}

handleSubmit(event) {
event.preventDefault()
this.props.updateOrder(this.props.modalDetails.id, this.state.quantity);
this.props.receiveModalCloseBtn();
this.setState({
quantity: 1,
})
}

handleChange(event) {
this.setState({
quantity: parseInt(event.target.value, 10),
})
}

render(){
return(
<div className={this.props.className} id="addOrderModal">
<div className="modal-content">
<div className="modal-header">
<span className="closeBtn" onClick={this.handleClick}>&times;</span>
<h2>Add to Order</h2>
</div>
<div className="modal-body">
<img src={"/static/"+this.props.modalDetails.photoUrl} />
<h3>{this.props.modalDetails.description}</h3>
<form className="itemOrder" onSubmit={this.handleSubmit}>
<input onChange={this.handleChange} value={this.state.quantity} type="number" step="1" max="10" min="1"/>
<button>Add to Order</button>
</form>
</div>
</div>
</div>
)
}

}

export default AddOrderModal
4 changes: 3 additions & 1 deletion src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import Menu from './Menu.js'

import '../styles/App.scss';

Expand All @@ -10,7 +11,8 @@ class App extends React.Component {
render(){
return (
<div>
Delivereat app
<h1 className="mainHeading">Diabetes</h1>
<Menu />
</div>
)
}
Expand Down
122 changes: 122 additions & 0 deletions src/components/Menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import React from 'react';
import AddOrderModal from './AddOrderModal.js'
import Order from './Order.js'

// import '../styles/Menu.scss';

class Menu extends React.Component {
constructor(){
super();

this.state ={
menu: [],
modalDetails: "",
modalOpen: false,
order: {},
total: ""
}

this.menuCall = this.menuCall.bind(this);
this.itemCall = this.itemCall.bind(this);
this.handleClick = this.handleClick.bind(this);
this.updateOrder = this.updateOrder.bind(this);
this.receiveModalCloseBtn = this.receiveModalCloseBtn.bind(this);
this.receiveOrderSubmit = this.receiveOrderSubmit.bind(this);
this.receiveRemoveItem = this.receiveRemoveItem.bind(this);
}

componentDidMount(){
this.menuCall()
}

menuCall(){
fetch(`/menu`)
.then(function(response) {
return response.json();
})
.then(body => {
this.setState({
menu: body
})
})
}

itemCall(menuId){
fetch(`/menu/${menuId}`)
.then(function(response) {
return response.json();
})
.then(body => {
this.setState({
modalDetails: body
})
})
}

handleClick(menuId) {
this.itemCall(menuId);
this.setState({
modalOpen: true
})
}

receiveModalCloseBtn(){
this.setState({
modalOpen: false,
})
}

updateOrder(itemId, itemQuantity){
const newOrder = Object.assign({}, this.state.order);

if (newOrder[itemId]) {
newOrder[itemId] = newOrder[itemId] + itemQuantity
} else {
newOrder[itemId] = itemQuantity
}

Object.entries(this.order).reduce()

this.setState({
order: newOrder,
total: ""
})
}

receiveOrderSubmit(){
fetch(`/order`, {method:"POST", headers: {
"Content-Type": "application/json; charset=utf-8",
}, body: JSON.stringify(this.state.order)})
this.setState({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to put setState inside a then so you can ensure that the POSTt has completed successfully

order: {}
})
}

receiveRemoveItem(menuItem){
const orderEntries = Object.entries(this.state.order);
console.log(menuItem)
// this.setState{(
// order:
// )}
}

render(){
return(
<div>
{this.state.menu.map (item => {
return(
<div onClick={() => this.handleClick(item.id)}>
{item.name}{item.price}
{item.vegetarian ? <img className="vegan" src="static/vegan.png" /> : null}
</div>
)
})}
<div><AddOrderModal receiveModalCloseBtn={this.receiveModalCloseBtn} updateOrder={this.updateOrder} className= {this.state.modalOpen ? "modal2" : "modal"} modalDetails={this.state.modalDetails}/></div>
<div><Order receiveRemoveItem={this.receiveRemoveItem} receiveOrderSubmit={this.receiveOrderSubmit} order={this.state.order} menu={this.state.menu} /></div>
</div>
)

}
}

export default Menu;
52 changes: 52 additions & 0 deletions src/components/Order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from 'react';

class Order extends React.Component {
constructor(){
super();

this.state= {
}

this.handleClick=this.handleClick.bind(this);
this.handleClick2=this.handleClick2.bind(this);
}

handleClick(){
this.props.receiveOrderSubmit();
}

handleClick2(id){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could have a more descriptive name

this.props.receiveRemoveItem(id);
}

render(){
return(
<div>
<h3>Your Order Basket</h3>
<div>
{Object.entries(this.props.order).map (([id, quantity]) => {
const menuItem = this.props.menu.find(item => item.id.toString() === id)
return(
<div data-id={id}>
{quantity}x {id} x {menuItem.name} x £{menuItem.price}
<button
data-id={id}
key={menuItem.id}
receiveRemoveItem={this.props.receiveRemoveItem}
onClick={event => this.handleClick2(id)}>Remove Item</button>
</div>
)
})}
</div>
<div>
<button onClick={this.handleClick} >Submit</button>
</div>
<div>
<button onClick={this.handleClick} >Submit</button>
</div>
</div>
)
}
}

export default Order
Binary file added src/components/cake1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading