forked from danskernesdigitalebibliotek/dpl-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.slice.js
More file actions
44 lines (40 loc) · 1.18 KB
/
user.slice.js
File metadata and controls
44 lines (40 loc) · 1.18 KB
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
40
41
42
43
44
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
// This thunk doesn't actually do anything but resolve straight away,
// but this allows us to chain a `.then()` on the action on the caller
// side.
export const attemptAuthentication = createAsyncThunk(
"user/attemptAuthentication",
() => Promise.resolve()
);
const userSlice = createSlice({
name: "user",
initialState: { status: "unauthenticated" },
reducers: {
updateStatus(state, action) {
if (state.status === "unauthenticated" || state.status === "attempting") {
if (action.payload.hasToken) {
state.status = "authenticated";
} else if (action.payload.doFail && state.status === "attempting") {
state.status = "failed";
}
}
},
setStatusAuthenticated(state) {
state.status = "authenticated";
},
setStatusUnauthenticated(state) {
state.status = "unauthenticated";
}
},
extraReducers: {
[attemptAuthentication.pending]: (state) => {
state.status = "attempting";
}
}
});
export const {
updateStatus,
setStatusAuthenticated,
setStatusUnauthenticated
} = userSlice.actions;
export default userSlice.reducer;