diff --git a/server/src/api/address.js b/server/src/api/address.js index fb62420..ca0d2a5 100755 --- a/server/src/api/address.js +++ b/server/src/api/address.js @@ -15,9 +15,9 @@ const addressResource = ({ config, db }) => resource({ create(req, res) { const { body } = req; - const { address, time, origin } = body; - if (!address || !time || !origin) { - res.status(400).json({ + const { address, time } = body; + if (!address || !time) { + return res.status(400).json({ message: `Missing field`, code: 400, errors: { diff --git a/server/src/api/cart.js b/server/src/api/cart.js index 57cb1f6..c19b130 100644 --- a/server/src/api/cart.js +++ b/server/src/api/cart.js @@ -12,16 +12,16 @@ const cartResource = ({ config, db }) => resource({ /** Property name to store preloaded entity on `request`. */ id: facetName, - /** POST / - Create a new entity */ - create(req, res) { + index({ params }, res) { inMemoCart.current = Object.assign({}, initialCart, { - id: String(new Date().getTime()), + subtotal: 0.0, items: [] }); res.json(inMemoCart.current); }, + /** PUT /:id - Update a given entity */ update({ body, ...req }, res) { @@ -35,7 +35,7 @@ const cartResource = ({ config, db }) => resource({ const result = { id, count: body.qty, - meta: { restaurantId: body.restaurantId } + restaurantId: body.restaurantId }; inMemoCart.current.items = [ ...inMemoCart.current.items, @@ -43,7 +43,7 @@ const cartResource = ({ config, db }) => resource({ ]; console.log(JSON.stringify(inMemoCart.current.items, null, 2)); return new Promise(rs => setTimeout(() => { - res.send(result); + res.send(inMemoCart.current); rs(); }, 2000)); } else { @@ -53,7 +53,7 @@ const cartResource = ({ config, db }) => resource({ // res.send(result); console.log(JSON.stringify(inMemoCart.current.items, null, 2)); return new Promise(rs => setTimeout(() => { - res.send(result); + res.send(inMemoCart.current); rs(); }, 2000)); } diff --git a/server/src/api/index.js b/server/src/api/index.js index e0bf3df..4bb2e4a 100755 --- a/server/src/api/index.js +++ b/server/src/api/index.js @@ -8,9 +8,9 @@ export default ({ config, db }) => { let api = Router(); // mount the facets resource - api.use('/address', addresses({ config, db })); - api.use('/restaurants', restaurants({ config, db })); + api.use('/cart/address', addresses({ config, db })); api.use('/cart', cart({ config, db })); + api.use('/restaurants', restaurants({ config, db })); // perhaps expose some API metadata at the root api.get('/', (req, res) => { diff --git a/server/src/models/address.js b/server/src/models/address.js index ef9bc1f..d74684f 100755 --- a/server/src/models/address.js +++ b/server/src/models/address.js @@ -12,11 +12,19 @@ const restaurants = [ 'id': '121721', 'name': 'Nandos Banani', 'address': 'Road-11, Banani, Dhaka', + street1: '', + street2: '', + locality: 'city', + region: 'state', + postcode: '', + country: '', + "cuisine": "Indian", 'delivery-fee': 75.00, avgDeliveryTime: 60 }, { 'id': '6317637', 'name': 'Le Petit Souffle', + "cuisine": "Indian", 'address': 'Third Floor, Century City Mall, Kalayaan Avenue, Poblacion, Makati City', 'delivery-fee': 75.00, avgDeliveryTime: 60 @@ -25,24 +33,28 @@ const restaurants = [ 'id': '6304287', 'name': 'Izakaya Kikufuji', 'address': 'Little Tokyo, 2277 Chino Roces Avenue, Legaspi Village, Makati City', + "cuisine": "Fast Food", 'delivery-fee': 75.00, avgDeliveryTime: 60 }, { 'id': '6300002', 'name': 'Heat - Edsa Shangri-La', - 'address': 'Edsa Shangri-La, 1 Garden Way, Ortigas, Mandaluyong City\n', + 'address': 'Edsa Shangri-La, 1 Garden Way, Ortigas, Mandaluyong City', + "cuisine": "Fast Food", 'delivery-fee': 75.00, avgDeliveryTime: 60 }, { 'id': '6318506', 'name': 'Ooma', 'address': 'Third Floor, Mega Fashion Hall, SM Megamall, Ortigas, Mandaluyong City', + "cuisine": "Fast Food", 'delivery-fee': 75.00, avgDeliveryTime: 60 }, { 'id': '63185068', 'name': 'All-Ooma - All items', 'address': 'Fourth Floor, Mega Fashion Hall, SM Megamall, Ortigas, Mandaluyong City', + "cuisine": "Oriental", 'delivery-fee': 80.00, avgDeliveryTime: 55 }, diff --git a/server/src/models/menu.js b/server/src/models/menu.js index b3ee4db..4c36d18 100644 --- a/server/src/models/menu.js +++ b/server/src/models/menu.js @@ -7,7 +7,6 @@ const menu = [ "position": 1, "price": "250.00", "consumable": "1:1", - "cuisine_name": "Indian", "category_name": "Appeteasers", "discount": { "type": "", @@ -21,7 +20,6 @@ const menu = [ "position": 1, "price": "250.00", "consumable": "1:1", - "cuisine_name": "Indian", "category_name": "Appeteasers", "discount": { "type": "", @@ -35,7 +33,6 @@ const menu = [ "position": 1, "price": "215.00", "consumable": "1:1", - "cuisine_name": "Indian", "category_name": "Appeteasers", "discount": { "type": "", @@ -49,7 +46,6 @@ const menu = [ "position": 1, "price": "215.00", "consumable": "1:1", - "cuisine_name": "Indian", "category_name": "Appeteasers", "discount": { "type": "", @@ -63,7 +59,6 @@ const menu = [ "position": 1, "price": "205.00", "consumable": "1:1", - "cuisine_name": "Indian", "category_name": "Appeteasers", "discount": { "type": "", @@ -77,7 +72,6 @@ const menu = [ "position": 1, "price": "615.00", "consumable": "1:1", - "cuisine_name": "Indian", "category_name": "Appeteasers", "discount": { "type": "", @@ -91,7 +85,6 @@ const menu = [ "position": 1, "price": "520.00", "consumable": "1:1", - "cuisine_name": "Indian", "category_name": "Appeteasers", "discount": { "type": "", @@ -105,7 +98,6 @@ const menu = [ "position": 1, "price": "190.00", "consumable": "1:1", - "cuisine_name": "Fast Food", "category_name": "Fino sides", "discount": { "type": "", @@ -119,7 +111,6 @@ const menu = [ "position": 2, "price": "330.00", "consumable": "1:1", - "cuisine_name": "Fast Food", "category_name": "Fino sides", "discount": { "type": "", @@ -133,7 +124,6 @@ const menu = [ "position": 1, "price": "190.00", "consumable": "1:1", - "cuisine_name": "Fast Food", "category_name": "Fino sides", "discount": { "type": "", @@ -147,7 +137,6 @@ const menu = [ "position": 2, "price": "330.00", "consumable": "1:1", - "cuisine_name": "Fast Food", "category_name": "Fino sides", "discount": { "type": "", @@ -161,7 +150,6 @@ const menu = [ "position": 1, "price": "385.00", "consumable": "1:1", - "cuisine_name": "Indian", "category_name": "Peri-peri chicken", "discount": { "type": "", @@ -175,7 +163,6 @@ const menu = [ "position": 2, "price": "485.00", "consumable": "1:1", - "cuisine_name": "Indian", "category_name": "Peri-peri chicken", "discount": { "type": "", @@ -189,7 +176,6 @@ const menu = [ "position": 3, "price": "575.00", "consumable": "1:1", - "cuisine_name": "Indian", "category_name": "Peri-peri chicken", "discount": { "type": "", @@ -203,7 +189,6 @@ const menu = [ "position": 1, "price": "685.00", "consumable": "1:1", - "cuisine_name": "Indian", "category_name": "Peri-peri chicken", "discount": { "type": "", @@ -217,7 +202,6 @@ const menu = [ "position": 2, "price": "785.00", "consumable": "1:1", - "cuisine_name": "Indian", "category_name": "Peri-peri chicken", "discount": { "type": "", @@ -231,7 +215,6 @@ const menu = [ "position": 3, "price": "875.00", "consumable": "1:1", - "cuisine_name": "Indian", "category_name": "Peri-peri chicken", "discount": { "type": "", diff --git a/src/features/actions/api.js b/src/features/actions/api.js index cf45d74..9715a8e 100644 --- a/src/features/actions/api.js +++ b/src/features/actions/api.js @@ -7,18 +7,18 @@ const urlResolver = obtainFQDNUrl(API_URL, window.location); const apiRoutes = prepareRoutesForFetch({ postAddressObtainRestaurants: [ - `POST /address`, - (address, time, origin) => ({ address, time, origin }) + `POST /cart/address`, + (address, time) => ({ address, time }) ], getRestaurantById: restaurantId => `/restaurants/${ restaurantId }`, - postCreateNewCart: `POST /cart`, + getCart: `GET /cart`, putUpdateCartWithItem: [ - (cartId, restaurantId, itemId, qty) => `PUT /cart/${ cartId }`, - (cartId, restaurantId, itemId, qty) => ({ cartId, restaurantId, itemId, qty }) + (cartId, restaurantId, itemId, quantity) => `PUT /cart/${ cartId }`, + (cartId, restaurantId, itemId, quantity) => ({ cartId, restaurantId, itemId, quantity }) ] }, urlResolver); -export const { postAddressObtainRestaurants, getRestaurantById, putUpdateCartWithItem, postCreateNewCart } = apiRoutes; +export const { postAddressObtainRestaurants, getRestaurantById, putUpdateCartWithItem, getCart } = apiRoutes; function prepareRoutesForFetch(routes, urlResolver) { return Object.fromEntries(Array.from(Object.entries(routes), ([ k, v ]) => { diff --git a/src/features/cart/cartSlice.js b/src/features/cart/cartSlice.js index bb7d19d..3a14efc 100644 --- a/src/features/cart/cartSlice.js +++ b/src/features/cart/cartSlice.js @@ -1,13 +1,13 @@ import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'; -import { postCreateNewCart, putUpdateCartWithItem } from '../actions/api'; +import { getCart, putUpdateCartWithItem } from '../actions/api'; const ns = 'cart'; -export const obtainNewCartAsyncThunk = createAsyncThunk( +export const obtainCartAsyncThunk = createAsyncThunk( 'cart/createNew', async (data, { rejectWithValue, dispatch }) => { void data; - return postCreateNewCart(); + return getCart(); }); export const updateCartWithItemAsyncThunk = createAsyncThunk( @@ -22,7 +22,8 @@ export const updateCartWithItemAsyncThunk = createAsyncThunk( }); const initialState = { - id: null, + id: '123', + subTotal: 0, status: null, items: [] }; @@ -38,14 +39,14 @@ export const cartSlice = createSlice({ resetCart: () => Object.assign({}, initialState, { items: [] }) }, extraReducers: builder => builder - .addCase(obtainNewCartAsyncThunk.pending, (state, { payload }) => { + .addCase(obtainCartAsyncThunk.pending, (state, { payload }) => { state.status = 'pending'; }) - .addCase(obtainNewCartAsyncThunk.fulfilled, + .addCase(obtainCartAsyncThunk.fulfilled, (state, { payload, ...rest }) => { return Object.assign({}, state, payload, { status: 'ready' }); }) - .addCase(obtainNewCartAsyncThunk.rejected, (state, { payload, ...rest }) => { + .addCase(obtainCartAsyncThunk.rejected, (state, { payload, ...rest }) => { state.status = 'error'; state.items = []; }) diff --git a/src/ui/pages/RestaurantPage/menuItems.js b/src/ui/pages/RestaurantPage/menuItems.js index 98b3994..e2650a5 100644 --- a/src/ui/pages/RestaurantPage/menuItems.js +++ b/src/ui/pages/RestaurantPage/menuItems.js @@ -4,7 +4,7 @@ import { accessCart, accessCartItems, accessCartStatus, - obtainNewCartAsyncThunk + obtainCartAsyncThunk } from '../../../features/cart/cartSlice'; import React, { useCallback, useEffect, useMemo } from 'react'; import { retrieveRestaurantByIdAsyncThunk } from '../../../features/address/addressSlice'; @@ -23,7 +23,7 @@ import { e2eAssist } from '../../../testability'; 'position': 1, 'price': '250.00', 'consumable': '1:1', - 'cuisine_name': 'Indian', + 'cuisine': 'Indian', 'category_name': 'Appeteasers', 'discount': { 'type': '', @@ -64,7 +64,7 @@ export function MenuItems({ restaurantId }) { if (cartStatus) { return; } - dispatch(obtainNewCartAsyncThunk()); + dispatch(obtainCartAsyncThunk()); }, [ cartStatus, dispatch ]); @@ -90,7 +90,7 @@ export function MenuItems({ restaurantId }) { text: 'Food Item', sort: true }, { - dataField: 'cuisine_name', + dataField: 'cuisine', text: 'Cuisine', sort: true }, {