File tree Expand file tree Collapse file tree 3 files changed +38
-7
lines changed Expand file tree Collapse file tree 3 files changed +38
-7
lines changed Original file line number Diff line number Diff line change 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" ;
2
4
3
5
const initialState = {
4
6
isLoggedIn : false ,
@@ -60,7 +62,18 @@ export const signOut = (dispatch) => {
60
62
} ;
61
63
62
64
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
+
64
77
return (
65
78
< AuthDispatchContext . Provider value = { dispatch } >
66
79
< AuthStateContext . Provider value = { state } >
Original file line number Diff line number Diff line change 1
- import React , { useReducer , createContext } from "react" ;
1
+ import React , { useReducer , createContext , useEffect } from "react" ;
2
+ import useLocalStorage from "hooks/useLocalStorage" ;
2
3
3
4
const initialState = {
4
5
isCartOpen : false ,
@@ -85,7 +86,18 @@ export const clearCart = (dispatch) => {
85
86
} ;
86
87
87
88
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 ) ] ) ;
89
101
return (
90
102
< CartDispatchContext . Provider value = { dispatch } >
91
103
< CartStateContext . Provider value = { state } >
Original file line number Diff line number Diff line change 1
1
import React , { useContext } from "react" ;
2
2
import { Formik , Form , Field } from "formik" ;
3
- import { useHistory } from "react-router-dom" ;
3
+ import { useHistory , useLocation } from "react-router-dom" ;
4
4
import * as Yup from "yup" ;
5
5
import _get from "lodash.get" ;
6
6
import { AuthDispatchContext , signIn } from "contexts/auth" ;
@@ -14,7 +14,9 @@ const LoginSchema = Yup.object().shape({
14
14
const AuthPage = ( ) => {
15
15
const authDispatch = useContext ( AuthDispatchContext ) ;
16
16
const history = useHistory ( ) ;
17
-
17
+ const location = useLocation ( ) ;
18
+ const fromUrl = _get ( location , "state.from.pathname" ) ;
19
+ console . log ( "location => " , location ) ;
18
20
const goToForgotPassword = ( e ) => {
19
21
e . preventDefault ( ) ;
20
22
} ;
@@ -25,7 +27,11 @@ const AuthPage = () => {
25
27
26
28
const signInSuccess = ( userData ) => {
27
29
signIn ( authDispatch , userData ) ;
28
- history . push ( "/" ) ;
30
+ if ( fromUrl ) {
31
+ history . push ( fromUrl ) ;
32
+ } else {
33
+ history . push ( "/" ) ;
34
+ }
29
35
} ;
30
36
31
37
return (
You can’t perform that action at this time.
0 commit comments