Skip to content

Commit

Permalink
utilized local storage to store cart/wishlist items
Browse files Browse the repository at this point in the history
  • Loading branch information
DharshiBalasubramaniyam committed Jun 16, 2024
1 parent 2ece73a commit 466b035
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 32 deletions.
51 changes: 25 additions & 26 deletions src/Context/SavedContext.jsx
Original file line number Diff line number Diff line change
@@ -1,64 +1,63 @@
import react, { createContext, useState } from "react";
import react, { createContext, useEffect, useState } from "react";
import all_products from "../assets/all_product";
export const SavedContext = createContext(null); //ContextAPI for providing state variables to components of Wishlist

const defaultWishlist = () => {
let list = {};
for (let index = 0; index < all_products.length; index++) {
list[index] = false;

const existingItems = JSON.parse(localStorage.getItem("shopy-wishlist"));

if (existingItems) {
return existingItems;
} else {
let list = {};
for (let index = 0; index < all_products.length; index++) {
list[index] = false;
}
return list;

}
return list;
};

const SavedContextProvider = (props) => {
const [listItem, setListItem] = useState(defaultWishlist());
const AddToList = (id) => {

setListItem((prev) => {
const updatedState = { ...prev, [id]: !listItem[id] };
console.log(updatedState);

const updatedState = { ...prev, [id]: !listItem[id] };
console.log(updatedState);
return updatedState;


});
};

const RemoveFromList = (id) => {
if (listItem[id]) {
setListItem((prev) => {

const updatedState = { ...prev, [id]: false };

console.log(updatedState);
return updatedState;
});
}
};

/*const getCartTotalAmount = () => {
let totalAmount = 0;
for (const item in cartItem) {
if (cartItem[item] > 0) {
let itemInfo = all_products.find(
(product) => product.id === Number(item)
);
totalAmount += cartItem[item] * itemInfo.new_price;
}
}
return totalAmount;
};*/

const getListQuantity = () => {
let totalQuantity = 0;
for (const item in listItem) {
if (listItem[item]) {
totalQuantity ++;
totalQuantity++;
}
}
return totalQuantity;
};

useEffect(() => {
localStorage.setItem("shopy-wishlist", JSON.stringify(listItem))
}, [listItem])

const contextValue = {
all_products,
listItem,
Expand Down
23 changes: 17 additions & 6 deletions src/Context/ShopContext.jsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import react, { createContext, useState } from "react";
import react, { createContext, useEffect, useState } from "react";
import all_products from "../assets/all_product";
export const ShopContext = createContext(null);

const getCartDefault = () => {
let cart = {};
for (let index = 0; index < all_products.length; index++) {
cart[index] = 0;

const existingItems = JSON.parse(localStorage.getItem("shopy-cart"));
if (existingItems) {
return existingItems
} else {
let cart = {};
for (let index = 0; index < all_products.length; index++) {
cart[index] = 0;
}
return cart;
}
return cart;

};

const ShopContextProvider = (props) => {
const [cartItem, setCartItem] = useState(getCartDefault());

const AddToCart = (id) => {
setCartItem((prev) => {
const updatedState = { ...prev, [id]: prev[id] + 1 };
Expand Down Expand Up @@ -54,6 +61,10 @@ const ShopContextProvider = (props) => {
return totalQuantity;
};

useEffect(() => {
localStorage.setItem("shopy-cart", JSON.stringify(cartItem))
}, [cartItem])

const contextValue = {
all_products,
cartItem,
Expand Down

0 comments on commit 466b035

Please sign in to comment.