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

still in progress #19

Open
wants to merge 5 commits 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
98 changes: 98 additions & 0 deletions apiFunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
function menu(){

const menuClosure = {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wouldn't call the object a closure, because a closure is a combination of an outer function and inner function which retains access to parent scope. Here we are just storing some data so something like menuData would be more appropriate

1: {
id:1,
title: "Strawberry cheesecake",
price: 6,
image: 'strawberrycheesecake.jpeg',
description: 'A medium sized Strawberry Cheesecake',
category: 'dessert'
},
2: {
id:2,
title: 'Blueberry cheesecake',
price: 6.50,
image: 'blueberry cheesecake.jpeg',
description: 'A medium sized Blueberry Cheesecake',
category: 'dessert'
},
3:{
id:3,
title:'Chocolate cheesecake',
price: 6.50,
image: 'chocolate cheesecake.jpeg',
description: 'A medium sized Chocolate Cheesecake',
category: 'dessert'
},
4:{
id:4,
title:'chesse',
price: 6.50,
image: 'chocolate cheesecake.jpeg',
description: 'cheese',
category: 'main'
},
5:{
id:5,
title:'Lamb Kebab',
price: 11.50,
image: 'chocolate cheesecake.jpeg',
description: 'A nice kebab',
category: 'main'
},
6:{
id:6,
title:'flatiron steak',
price: 10.50,
image: 'chocolate cheesecake.jpeg',
description: 'A nice steak',
category: 'main'
}

}
return {
getAllItems(){
return menuClosure;
},

getItem(itemId){
return menuClosure[itemId]
},
}
}



function orders(){
const orderClosure = {} // order will start as empty since orders need to be placed
let orderId =''//think I will need an order id
return {
getAllOrders(){
return orderClosure;
},

getOrder(orderId){
return orderClosure[orderId]
},

createNewOrder(newOrder){
const orderKeys = Object.keys(orderClosure);
const highestiD = Math.max(...orderKeys);
const newId = highestiD + 1;
orders = Object.assign({},orderClosure,{[newId]:newOrder})
return orderClosure
},

deleteStudent(orderId){
delete orderClosure[orderId]
}
}
}



module.exports = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Great to see closures in use :)

menu,
orders
}
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions 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 All @@ -18,6 +19,7 @@
},
"dependencies": {
"body-parser": "^1.18.3",
"classnames": "^2.2.6",
"express": "^4.16.3",
"hbs": "^4.0.1",
"jest": "^22.3.0",
Expand All @@ -36,10 +38,10 @@
"jest": "^22.4.2",
"jest-fetch-mock": "^1.6.6",
"node-sass": "^4.9.1",
"nodemon": "1.17.5",
"nodemon": "^1.17.5",
"react-test-renderer": "^16.2.0",
"sass-loader": "^7.0.3",
"style-loader": "^0.21.0",
"react-test-renderer": "^16.2.0",
"webpack": "^4.0.1",
"webpack-cli": "^2.0.9"
}
Expand Down
55 changes: 48 additions & 7 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,63 @@ const express = require('express');
const bodyParser = require('body-parser');
const app = express();

const{
menu, orders
} = require('./apiFunctions.js')

const {getAllItems, getItem} = menu()
const {getAllOrders,getOrder,createNewOrder} = orders()

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');
});

//Menu Routes
app.get('/menu', function(req, res){

const allItems = getAllItems()
res.json(allItems)
});

app.get('/menu/:itemId', function(req, res){
const item = getItem(req.params.itemId)
if(item){
return res.json(item)
} else res.status(404).json({error: 'this item does not exist'})
});







//Order Routes
app.get('/order', function(req,res){
const allOrders = getAllOrders()
res.json(allOrders)
})

app.get('/order:orderId', function(req,res){
Copy link
Contributor

Choose a reason for hiding this comment

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

There should probably be a slash after /order

const specificOrder = getOrder()
Copy link
Contributor

Choose a reason for hiding this comment

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

Get order needs to receive an order id to work

res.json(specificOrder)
})

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




//Listen Route
app.listen(8080, function(){
console.log('Listening on port 8080');
});
75 changes: 74 additions & 1 deletion src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,89 @@
import React from 'react';
import Homepage from './Homepage'
import Basket from './Basket'
import MenuResults from './MenuResults'

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

class App extends React.Component {
constructor(){
super();
this.state = {
menu: {},
order: {}, //to add empty history of order


}
this.getOrder = this.getOrder.bind(this);
// this.fetchMenu = this.fetchMenu.bind(this)
this.sendOrder = this.sendOrder.bind(this)

}

componentDidMount(){
this.fetchMenu()
}

fetchMenu(){
fetch('/menu')
.then(response => response.json())
.then(body => {
console.log(body)
this.setState({
menu: body,
})
})
}

sendOrder(){
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.

Does sendOrder not send data to server?

Copy link
Contributor

Choose a reason for hiding this comment

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

Just saw that the order gets sent from Basket. It might better to do fetch and clear data in one go so that you only clear order data once you know that the API request has been successful

order: {}
})
}


getOrder(order) {
const matchingOrders = Array.from(this.state.order).find(newOrder => {
return order.id === newOrder.id;
});

if (matchingOrders) {
matchingOrders.quantity += order.quantity;
matchingOrders.price += order.price;

const ordersWithoutCurrentOrder = this.state.order.filter(
newOrder => {
return newOrder.id !== order.id;
}
);

this.setState({
order: ordersWithoutCurrentOrder.concat(matchingOrders)
});
} else {
this.setState({
order: [...this.state.order, order]
Copy link
Contributor

Choose a reason for hiding this comment

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

Order is initialised to be an object, but here an array gets assigned

});
}
}










render(){
return (
<div>
Delivereat app
<div className='homepage-header'>
<Homepage/>
</div>
<Basket sendOrder={this.sendOrder} order={this.state.order}/>
<MenuResults getOrder={this.getOrder} menu={this.state.menu}/>
</div>
)
}
Expand Down
Loading