-
Notifications
You must be signed in to change notification settings - Fork 7
/
userSlice.js
37 lines (32 loc) · 978 Bytes
/
userSlice.js
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
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit"
const initialState = {
userDetails: ''
}
export const getUserAsync = createAsyncThunk(
'user/get',
async () => {
// Retrieve response from /.auth/me
const response = await fetch('/.auth/me');
// Convert to JSON
const payload = await response.json();
// Retrieve the clientPrincipal (current user)
const { clientPrincipal } = payload;
if (clientPrincipal) return clientPrincipal.userDetails;
else return '';
}
)
export const userSlice = createSlice({
name: 'user',
initialState,
reducers: {},
extraReducers: (builder) => {
builder.addCase(getUserAsync.fulfilled, (state, action) => {
// put user into state
state.userDetails = action.payload;
});
}
});
export const selectUserDetails = (state) => {
return state.user.userDetails;
}
export default userSlice.reducer;