-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserSlice.ts
executable file
·39 lines (34 loc) · 903 Bytes
/
userSlice.ts
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 { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { RootState } from "../store";
export interface User {
name: string;
email: string;
avatar: {
public_id: string;
secure_url: string;
};
isLoggedInWithGoogle: Boolean;
role: string;
_id: string;
createdAt: string;
}
const initialState: { user: User | null; isLoggedIn: Boolean } = {
user: null,
isLoggedIn: false,
};
export const userSlice = createSlice({
name: "user",
initialState,
reducers: {
setUser: (state, action: PayloadAction<User>) => {
state.user = action.payload;
},
clearUser: (state) => {
state.user = null;
},
},
});
export const { setUser, clearUser } = userSlice.actions;
export const selectUser = (state: RootState) => state.user.user;
export const selectIsLoggedIn = (state: RootState) => state.user.isLoggedIn;
export default userSlice.reducer;