Skip to content

Commit

Permalink
Fixed the persistent issue and cart items for particular users
Browse files Browse the repository at this point in the history
  • Loading branch information
MohdFaisalBidda committed Dec 25, 2023
1 parent b9f9d23 commit 01136b0
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 24 deletions.
36 changes: 33 additions & 3 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"react-router-dom": "^6.8.0",
"react-scroll": "^1.8.9",
"react-spinners": "^0.13.8",
"redux-persist": "^6.0.0",
"verb": "^0.8.10"
},
"devDependencies": {
Expand Down
9 changes: 7 additions & 2 deletions client/src/Components/Cart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ const Cart = () => {
const dispatch = useDispatch();
const cart = useSelector((state) => state.cart.cartItems);
const cartTotal = useSelector((state) => state.cart);
// console.log(cart[0].cartQuantity);
const user = useSelector((state) => state.auth);
const cartItemUser =JSON.parse(user.token)
console.log(user.user.user._id);
console.log(cart);
console.log(cart.filter((el) => el.userId === user.user.user._id));
// console.log(cart.);

const handleRemove = (product) => {
dispatch(removeFromCart(product));
Expand All @@ -33,7 +38,7 @@ const Cart = () => {

<div className="flex lg:justify-between lg:flex-row flex-col lg:my-0 my-10">
<div className="lg:flex-[3] ">
{cart.map((item) => (
{cart.filter((el) => el.userId === user.user.user._id).map((item) => (
<>
<div className="flex justify-between lg:flex-row flex-col px-10 py-8 lg:py-0">
<img src={item.image} className='lg:w-[200px] w-[420px] object-contain mx-auto' alt="Hello" />
Expand Down
2 changes: 1 addition & 1 deletion client/src/Components/Navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const Navbar = () => {
if (logoutUser) {
dispatch(reset())
//TODO: below line should be removed after finding the solution for storing particular user's products in their own cart
dispatch(resetCart())
// dispatch(resetCart())
}
}

Expand Down
28 changes: 17 additions & 11 deletions client/src/main.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App'
import './index.css'
import { BrowserRouter } from "react-router-dom"
import store from './redux/store'
import { Provider } from 'react-redux'
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import "./index.css";
import { BrowserRouter } from "react-router-dom";
import store from "./redux/store";
import { Provider } from "react-redux";
import { PersistGate } from "redux-persist/integration/react";
import { persistStore } from "redux-persist";

ReactDOM.createRoot(document.getElementById('root')).render(
let persistor = persistStore(store);

ReactDOM.createRoot(document.getElementById("root")).render(
<Provider store={store}>
<BrowserRouter>
<App />
<PersistGate persistor={persistor}>
<App />
</PersistGate>
</BrowserRouter>
</Provider>,
)
</Provider>
);
38 changes: 31 additions & 7 deletions client/src/redux/store.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
import { configureStore } from "@reduxjs/toolkit";
import { combineReducers, configureStore } from "@reduxjs/toolkit";
import cartReducer from "./Slices/cartSlice";
import authReducer from "./Slices/authSlice";
import wishlistReducer from "./Slices/wishlistSlice";
import storage from "redux-persist/lib/storage";
import { persistReducer } from "redux-persist";

const cartPersistConfig = {
key: "cart",
storage,
};

const wishlistPersistConfig = {
key: "wishlist",
storage,
};

const authReducerPersistConfig = {
key: "auth",
storage: storage, // Do not use storage for authReducer
blacklist: ["user"], // Exclude 'user' field from being persisted
};

const reducer = combineReducers({
cart: persistReducer(cartPersistConfig, cartReducer),
auth: persistReducer(authReducerPersistConfig, authReducer),
wishlist: persistReducer(wishlistPersistConfig, wishlistReducer),
});

const persistendReducer = persistReducer(
{ key: "root", version: 1, storage },
reducer
);

export default configureStore({
reducer: {
cart: cartReducer,
auth:authReducer,
wishlist:wishlistReducer
},
})
reducer: persistendReducer,
});

1 comment on commit 01136b0

@vercel
Copy link

@vercel vercel bot commented on 01136b0 Dec 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.