-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9158e8
commit 89af175
Showing
8 changed files
with
121 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,36 @@ | ||
import { createSlice } from '@reduxjs/toolkit'; | ||
import { User } from '@/typings'; | ||
import { PayloadAction, createSlice } from '@reduxjs/toolkit'; | ||
|
||
interface AuthState { | ||
isAuthenticated: boolean; | ||
isLoading: boolean; | ||
isLoading: boolean; | ||
user: User | null; | ||
} | ||
|
||
const initialState = { | ||
isAuthenticated: false, | ||
isLoading: true, | ||
isLoading: true, | ||
user: null, | ||
} as AuthState; | ||
|
||
const authSlice = createSlice({ | ||
name: 'auth', | ||
initialState, | ||
reducers: { | ||
setAuth: state => { | ||
state.isAuthenticated = true; | ||
}, | ||
logout: state => { | ||
state.isAuthenticated = false; | ||
}, | ||
finishInitialLoad: state => { | ||
state.isLoading = false; | ||
}, | ||
}, | ||
name: 'auth', | ||
initialState, | ||
reducers: { | ||
setAuth: (state) => { | ||
state.isAuthenticated = true; | ||
}, | ||
setUser(state, action: PayloadAction<User>) { | ||
state.user = action.payload; | ||
}, | ||
logout: (state) => { | ||
state.isAuthenticated = false; | ||
}, | ||
finishInitialLoad: (state) => { | ||
state.isLoading = false; | ||
}, | ||
}, | ||
}); | ||
|
||
export const { setAuth, logout, finishInitialLoad } = authSlice.actions; | ||
export const { setAuth, setUser, logout, finishInitialLoad } = authSlice.actions; | ||
export default authSlice.reducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// utils/localStorage.js | ||
|
||
import { RootState } from './store'; | ||
|
||
export const loadState = () => { | ||
try { | ||
if (typeof window !== 'undefined') { | ||
console.log('You are on the browser'); | ||
return undefined; | ||
} else { | ||
console.log('You are on the server'); | ||
// 👉️ can't use localStorage | ||
} | ||
const serializedState = localStorage.getItem('state'); | ||
if (serializedState === null) { | ||
return undefined; | ||
} | ||
return JSON.parse(serializedState); | ||
} catch (err) { | ||
return undefined; | ||
} | ||
}; | ||
|
||
export const saveState = (state: RootState) => { | ||
try { | ||
const serializedState = JSON.stringify(state); | ||
localStorage.setItem('state', serializedState); | ||
} catch { | ||
// ignore write errors | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
'use client'; | ||
|
||
import { store } from './store'; | ||
import { useRef } from 'react'; | ||
import { Provider } from 'react-redux'; | ||
import { AppStore, makeStore } from './store'; | ||
|
||
interface Props { | ||
children: React.ReactNode; | ||
} | ||
export default function StoreProvider({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
const storeRef = useRef<AppStore>(); | ||
if (!storeRef.current) { | ||
// Create the store instance the first time this renders | ||
storeRef.current = makeStore(); | ||
} | ||
|
||
export default function CustomProvider({ children }: Props) { | ||
return <Provider store={store}>{children}</Provider>; | ||
return <Provider store={storeRef.current}>{children}</Provider>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,31 @@ | ||
import { configureStore } from '@reduxjs/toolkit'; | ||
import { apiSlice } from './services/apiSlice'; | ||
import authReducer from './features/authSlice'; | ||
import { loadState, saveState } from './localStorage'; // Import the utility functions | ||
|
||
export const store = configureStore({ | ||
reducer: { | ||
[apiSlice.reducerPath]: apiSlice.reducer, | ||
auth: authReducer, | ||
}, | ||
middleware: getDefaultMiddleware => | ||
getDefaultMiddleware().concat(apiSlice.middleware), | ||
devTools: process.env.NODE_ENV !== 'production', | ||
}); | ||
export const makeStore = () => { | ||
let preloadedState; // Load any saved state | ||
|
||
export type RootState = ReturnType<(typeof store)['getState']>; | ||
export type AppDispatch = (typeof store)['dispatch']; | ||
const store = configureStore({ | ||
reducer: { | ||
[apiSlice.reducerPath]: apiSlice.reducer, | ||
auth: authReducer, | ||
}, | ||
middleware: (getDefaultMiddleware) => | ||
getDefaultMiddleware().concat(apiSlice.middleware), | ||
preloadedState, // Pass the loaded state | ||
devTools: process.env.NODE_ENV !== 'production', | ||
}); | ||
|
||
store.subscribe(() => { | ||
saveState(store.getState()); // Save the current state to local storage | ||
}); | ||
|
||
return store; | ||
}; | ||
|
||
// Infer the type of makeStore | ||
export type AppStore = ReturnType<typeof makeStore>; | ||
// Infer the `RootState` and `AppDispatch` types from the store itself | ||
export type RootState = ReturnType<AppStore['getState']>; | ||
export type AppDispatch = AppStore['dispatch']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
export type Todo = { | ||
id: number; | ||
}; | ||
|
||
export type User = { | ||
id: number; | ||
first_name: string; | ||
last_name: string; | ||
email: string; | ||
}; |