-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a cart store which uses a cookie for item storage Update, add, and remove now work
- Loading branch information
Showing
8 changed files
with
65 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,5 @@ | ||
export default async function addToCart({ color, size, product }, req, res) { | ||
const { id, quantity } = product | ||
import { addItem } from './utils/cartStore' | ||
|
||
console.log('product id: ', id) | ||
console.log('color id: ', color || 'Not provided') | ||
console.log('size id: ', size || 'Not provided') | ||
console.log('quantity ', quantity || 1) | ||
|
||
return { added: true, id } | ||
export default async function addToCart({ color, size, product, quantity }, req, res) { | ||
return { cart: { items: addItem(product.id, quantity, req, res) } } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export default function removeCartItem(item, req, res) { | ||
return {} | ||
import { removeItem } from './utils/cartStore' | ||
|
||
export default async function removeCartItem(item, req, res) { | ||
return { cart: { items: removeItem(item.id, req, res) } } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,11 @@ | ||
import createProduct from './utils/createProduct' | ||
import { getProducts } from './utils/cartStore' | ||
|
||
export default async function session(req, res) { | ||
const products = [createProduct(1), createProduct(2), createProduct(3)] | ||
|
||
return { | ||
name: 'Mark', | ||
email: '[email protected]', | ||
cart: { | ||
items: products.map((item, i) => ({ | ||
...item, | ||
quantity: 1, | ||
})), | ||
items: getProducts(req, res), | ||
}, | ||
currency: 'USD', | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { updateItem } from './utils/cartStore' | ||
|
||
export default function updateCartItem(item, quantity, req, res) { | ||
return {} | ||
return { cart: { items: updateItem(item.id, quantity, req, res) } } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import createProduct from './createProduct' | ||
|
||
const CART_COOKIE = 'rsf_mock_cart' | ||
|
||
const initialStore = [ | ||
{ id: 1, quantity: 1 }, | ||
{ id: 2, quantity: 1 }, | ||
] | ||
|
||
function getStore(req, res) { | ||
if (!req.cookies[CART_COOKIE]) { | ||
res.setHeader('Set-Cookie', `${CART_COOKIE}=${JSON.stringify(initialStore)}; Path=/`) | ||
} | ||
const store = req.cookies[CART_COOKIE] || initialStore | ||
try { | ||
return JSON.parse(store) | ||
} catch (err) { | ||
console.log('Failed parsing store from cookie', req.cookies[CART_COOKIE]) | ||
return [] | ||
} | ||
} | ||
|
||
function toProduct({ id, quantity }) { | ||
return { ...createProduct(id), quantity } | ||
} | ||
|
||
export function getProducts(req, res) { | ||
return getStore(req, res).map(toProduct) | ||
} | ||
|
||
export function updateItem(id, quantity, req, res) { | ||
const newStore = [...getStore(req, res)] | ||
const item = newStore.find(e => e.id === id) | ||
item.quantity = quantity | ||
res.setHeader('Set-Cookie', `${CART_COOKIE}=${JSON.stringify(newStore)}; Path=/`) | ||
return newStore.map(toProduct) | ||
} | ||
|
||
export function removeItem(id, req, res) { | ||
const newStore = [...getStore(req, res)].filter(e => e.id !== id) | ||
res.setHeader('Set-Cookie', `${CART_COOKIE}=${JSON.stringify(newStore)}; Path=/`) | ||
return newStore.map(toProduct) | ||
} | ||
|
||
export function addItem(id, quantity, req, res) { | ||
const newStore = [{ id, quantity }, ...getStore(req, res)] | ||
res.setHeader('Set-Cookie', `${CART_COOKIE}=${JSON.stringify(newStore)}; Path=/`) | ||
return newStore.map(toProduct) | ||
} |