Skip to content

Commit 04960f4

Browse files
committed
redux
1 parent 4bce576 commit 04960f4

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

Chapter8/redux/src/store/store.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { configureStore } from '@reduxjs/toolkit';
2+
import userReducer from './userSlice';
3+
4+
export const store = configureStore({
5+
reducer: { user: userReducer },
6+
});
7+
8+
export type RootState = ReturnType<typeof store.getState>;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { createSlice } from '@reduxjs/toolkit';
2+
import type { PayloadAction } from '@reduxjs/toolkit';
3+
import { User } from '../api/authenticate';
4+
5+
type State = {
6+
user: undefined | User;
7+
permissions: undefined | string[];
8+
loading: boolean;
9+
};
10+
const initialState: State = {
11+
user: undefined,
12+
permissions: undefined,
13+
loading: false,
14+
};
15+
16+
export const userSlice = createSlice({
17+
name: 'user',
18+
initialState,
19+
reducers: {
20+
authenticateAction: (state) => {
21+
state.loading = true;
22+
},
23+
authenticatedAction: (state, action: PayloadAction<User | undefined>) => {
24+
state.user = action.payload;
25+
state.loading = false;
26+
},
27+
authorizeAction: (state) => {
28+
state.loading = true;
29+
},
30+
authorizedAction: (state, action: PayloadAction<string[]>) => {
31+
state.permissions = action.payload;
32+
state.loading = false;
33+
},
34+
},
35+
});
36+
37+
export const { authenticateAction, authenticatedAction, authorizeAction, authorizedAction } =
38+
userSlice.actions;
39+
40+
export default userSlice.reducer;

0 commit comments

Comments
 (0)