summaryrefslogtreecommitdiff
path: root/src/core/utils/cart.js
diff options
context:
space:
mode:
authorHATEC\SPVDEV001 <tri.susilo@altama.co.id>2023-04-11 11:06:38 +0700
committerHATEC\SPVDEV001 <tri.susilo@altama.co.id>2023-04-11 11:06:38 +0700
commit3df233e0c23e7d4503931ab6ec8ffc41642ac104 (patch)
treeccc032defe422f5fafc4a08af672833b2fe41835 /src/core/utils/cart.js
parent006c77a85786c24199db157d1d70f48b47311d35 (diff)
parentf0a720441def88187b3913268238c379362fb9d3 (diff)
Merge branch 'master' into development_tri/feedback_UAT
Diffstat (limited to 'src/core/utils/cart.js')
-rw-r--r--src/core/utils/cart.js41
1 files changed, 38 insertions, 3 deletions
diff --git a/src/core/utils/cart.js b/src/core/utils/cart.js
index fd42ee4e..2bdffb1c 100644
--- a/src/core/utils/cart.js
+++ b/src/core/utils/cart.js
@@ -1,23 +1,51 @@
+/**
+ * Retrieves cart data from localStorage, if available.
+ *
+ * @returns {Object} - The cart data as an object, or an empty object if not found.
+ */
const getCart = () => {
- if (typeof window !== 'undefined') {
+ if (typeof window !== 'undefined' && window.localStorage) {
const cart = localStorage.getItem('cart')
if (cart) return JSON.parse(cart)
}
return {}
}
+/**
+ * Saves cart data to localStorage, if available.
+ *
+ * @param {Object} cart - The cart data to be saved.
+ * @returns {boolean} - Returns `true` if cart data is saved successfully, `false` otherwise.
+ */
const setCart = (cart) => {
- if (typeof window !== 'undefined') {
+ if (typeof window !== 'undefined' && window.localStorage) {
localStorage.setItem('cart', JSON.stringify(cart))
+ return true
}
- return true
+ return false
}
+/**
+ * Retrieves an item from the cart data based on the given product ID.
+ *
+ * @param {Object} options - The options object containing the productId.
+ * @param {string} options.productId - The ID of the product to be retrieved from the cart.
+ * @returns {Object} - Returns the item object from the cart data, or `undefined` if not found.
+ */
const getItemCart = ({ productId }) => {
let cart = getCart()
return cart[productId]
}
+/**
+ * Updates an item in the cart data with the given productId, quantity, and selected status.
+ *
+ * @param {Object} options - The options object containing the productId, quantity, and selected status.
+ * @param {string} options.productId - The ID of the product to be updated in the cart.
+ * @param {number} options.quantity - The new quantity of the product in the cart.
+ * @param {boolean} [options.selected=false] - The new selected status of the product in the cart. Default is `false`.
+ * @returns {boolean} - Returns `true`.
+ */
const updateItemCart = ({ productId, quantity, selected = false }) => {
let cart = getCart()
quantity = parseInt(quantity)
@@ -26,6 +54,13 @@ const updateItemCart = ({ productId, quantity, selected = false }) => {
return true
}
+/**
+ * Deletes an item from the cart data with the given productId.
+ *
+ * @param {Object} options - The options object containing the productId.
+ * @param {string} options.productId - The ID of the product to be deleted from the cart.
+ * @returns {boolean} - Returns `true`.
+ */
const deleteItemCart = ({ productId }) => {
let cart = getCart()
delete cart[productId]