Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utilized local storage for store cart and wish list items, Because earlier, when refreshing the page, the data erased. (fixes #273) #274

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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