From f99e0aba70efad0deb907d8e27f09fc9f527c8a4 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 17 Feb 2023 17:07:50 +0700 Subject: Refactor --- src/core/utils/cart.js | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) (limited to 'src/core/utils/cart.js') diff --git a/src/core/utils/cart.js b/src/core/utils/cart.js index 66efcbf2..291d511b 100644 --- a/src/core/utils/cart.js +++ b/src/core/utils/cart.js @@ -1,36 +1,37 @@ const getCart = () => { - const cart = localStorage.getItem('cart'); - if (cart) return JSON.parse(cart); - return {}; + const cart = localStorage.getItem('cart') + if (cart) return JSON.parse(cart) + return {} } const setCart = (cart) => { - localStorage.setItem('cart', JSON.stringify(cart)); - return true; + localStorage.setItem('cart', JSON.stringify(cart)) + return true } -const getItemCart = (product_id) => { - let cart = getCart(); - return cart[product_id]; +const getItemCart = ({ productId }) => { + let cart = getCart() + return cart[productId] } -const createOrUpdateItemCart = (product_id, quantity, selected = false) => { - let cart = getCart(); - cart[product_id] = { product_id, quantity, selected }; - setCart(cart); - return true; +const addItemCart = ({ productId, quantity, selected = false }) => { + let cart = getCart() + quantity = parseInt(quantity) + cart[productId] = { productId, quantity, selected } + setCart(cart) + return true } -const deleteItemCart = (product_id) => { - let cart = getCart(); - delete cart[product_id]; - setCart(cart); - return true; +const deleteItemCart = ({ productId }) => { + let cart = getCart() + delete cart[productId] + setCart(cart) + return true } export { getCart, getItemCart, - createOrUpdateItemCart, + addItemCart, deleteItemCart } \ No newline at end of file -- cgit v1.2.3 From 8c38ac6b55ce3aa15196364619f8f22053a00c48 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Sat, 18 Feb 2023 01:15:17 +0700 Subject: fix build --- src/core/utils/cart.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src/core/utils/cart.js') diff --git a/src/core/utils/cart.js b/src/core/utils/cart.js index 291d511b..928d2ad1 100644 --- a/src/core/utils/cart.js +++ b/src/core/utils/cart.js @@ -1,11 +1,15 @@ const getCart = () => { - const cart = localStorage.getItem('cart') - if (cart) return JSON.parse(cart) + if (typeof window !== 'undefined') { + const cart = localStorage.getItem('cart') + if (cart) return JSON.parse(cart) + } return {} } const setCart = (cart) => { - localStorage.setItem('cart', JSON.stringify(cart)) + if (typeof window !== 'undefined') { + localStorage.setItem('cart', JSON.stringify(cart)) + } return true } -- cgit v1.2.3 From d22df6bd30b8ed4bcfa938dcbbedc5fc376c2304 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Mon, 20 Feb 2023 10:49:35 +0700 Subject: cart refactor --- src/core/utils/cart.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/core/utils/cart.js') diff --git a/src/core/utils/cart.js b/src/core/utils/cart.js index 928d2ad1..52e157f2 100644 --- a/src/core/utils/cart.js +++ b/src/core/utils/cart.js @@ -18,7 +18,7 @@ const getItemCart = ({ productId }) => { return cart[productId] } -const addItemCart = ({ productId, quantity, selected = false }) => { +const updateItemCart = ({ productId, quantity, selected = false }) => { let cart = getCart() quantity = parseInt(quantity) cart[productId] = { productId, quantity, selected } @@ -36,6 +36,6 @@ const deleteItemCart = ({ productId }) => { export { getCart, getItemCart, - addItemCart, + updateItemCart, deleteItemCart } \ No newline at end of file -- cgit v1.2.3 From f66b12fd1d0b83af0d7230d7b1565fbe00afbe3c Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Wed, 22 Feb 2023 11:03:34 +0700 Subject: prettier --- src/core/utils/cart.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'src/core/utils/cart.js') diff --git a/src/core/utils/cart.js b/src/core/utils/cart.js index 52e157f2..fd42ee4e 100644 --- a/src/core/utils/cart.js +++ b/src/core/utils/cart.js @@ -33,9 +33,4 @@ const deleteItemCart = ({ productId }) => { return true } -export { - getCart, - getItemCart, - updateItemCart, - deleteItemCart -} \ No newline at end of file +export { getCart, getItemCart, updateItemCart, deleteItemCart } -- cgit v1.2.3