-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathapp.slice.ts
More file actions
executable file
·39 lines (34 loc) · 970 Bytes
/
app.slice.ts
File metadata and controls
executable file
·39 lines (34 loc) · 970 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { useDispatch, useSelector } from 'react-redux';
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { State, Dispatch } from '@/utils/store';
import { User } from '@/types';
export interface AppState {
checked: boolean;
loggedIn: boolean;
user?: User;
}
const initialState: AppState = {
checked: false,
loggedIn: false,
user: undefined,
};
const slice = createSlice({
name: 'app',
initialState,
reducers: {
setLoggedIn: (state: AppState, { payload }: PayloadAction<boolean>) => {
state.checked = true;
state.loggedIn = payload;
},
setUser: (state: AppState, { payload }: PayloadAction<User | undefined>) => {
state.user = payload;
},
reset: () => initialState,
},
});
export function useAppSlice() {
const dispatch = useDispatch<Dispatch>();
const state = useSelector(({ app }: State) => app);
return { dispatch, ...state, ...slice.actions };
}
export default slice.reducer;