Skip to content

Commit 914c115

Browse files
committed
Persisted cart and auth data in localstorage
1 parent 6b8f68f commit 914c115

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

src/contexts/auth.jsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import React, { useReducer, createContext } from "react";
1+
import React, { useReducer, createContext, useEffect } from "react";
2+
import _get from "lodash.get";
3+
import useLocalStorage from "hooks/useLocalStorage";
24

35
const initialState = {
46
isLoggedIn: false,
@@ -60,7 +62,18 @@ export const signOut = (dispatch) => {
6062
};
6163

6264
const AuthProvider = ({ children }) => {
63-
const [state, dispatch] = useReducer(reducer, initialState);
65+
const [persistedUser, setPersistedUser] = useLocalStorage("user", null);
66+
const persistedUserState = {
67+
...initialState,
68+
user: persistedUser,
69+
isLoggedIn: _get(persistedUser, "username", "").length > 0
70+
};
71+
const [state, dispatch] = useReducer(reducer, persistedUserState);
72+
73+
useEffect(() => {
74+
setPersistedUser(state.user);
75+
}, [state.isLoggedIn]);
76+
6477
return (
6578
<AuthDispatchContext.Provider value={dispatch}>
6679
<AuthStateContext.Provider value={state}>

src/contexts/cart.jsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React, { useReducer, createContext } from "react";
1+
import React, { useReducer, createContext, useEffect } from "react";
2+
import useLocalStorage from "hooks/useLocalStorage";
23

34
const initialState = {
45
isCartOpen: false,
@@ -85,7 +86,18 @@ export const clearCart = (dispatch) => {
8586
};
8687

8788
const CartProvider = ({ children }) => {
88-
const [state, dispatch] = useReducer(reducer, initialState);
89+
const [persistedCartItems, setPersistedCartItems] = useLocalStorage(
90+
"cartItems",
91+
[]
92+
);
93+
const persistedCartState = {
94+
isCartOpen: false,
95+
items: persistedCartItems || []
96+
};
97+
const [state, dispatch] = useReducer(reducer, persistedCartState);
98+
useEffect(() => {
99+
setPersistedCartItems(state.items);
100+
}, [JSON.stringify(state.items)]);
89101
return (
90102
<CartDispatchContext.Provider value={dispatch}>
91103
<CartStateContext.Provider value={state}>

src/pages/auth.jsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useContext } from "react";
22
import { Formik, Form, Field } from "formik";
3-
import { useHistory } from "react-router-dom";
3+
import { useHistory, useLocation } from "react-router-dom";
44
import * as Yup from "yup";
55
import _get from "lodash.get";
66
import { AuthDispatchContext, signIn } from "contexts/auth";
@@ -14,7 +14,9 @@ const LoginSchema = Yup.object().shape({
1414
const AuthPage = () => {
1515
const authDispatch = useContext(AuthDispatchContext);
1616
const history = useHistory();
17-
17+
const location = useLocation();
18+
const fromUrl = _get(location, "state.from.pathname");
19+
console.log("location => ", location);
1820
const goToForgotPassword = (e) => {
1921
e.preventDefault();
2022
};
@@ -25,7 +27,11 @@ const AuthPage = () => {
2527

2628
const signInSuccess = (userData) => {
2729
signIn(authDispatch, userData);
28-
history.push("/");
30+
if (fromUrl) {
31+
history.push(fromUrl);
32+
} else {
33+
history.push("/");
34+
}
2935
};
3036

3137
return (

0 commit comments

Comments
 (0)