-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathindex.ts
63 lines (57 loc) · 1.83 KB
/
index.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { createSlice, type PayloadAction } from '@reduxjs/toolkit';
import type { ApiError } from 'common/api/baseGeneratedApis';
import type { BuiltinRole } from 'common/api/osrdEditoastApi';
export interface UserState {
isLogged: boolean;
loginError?: ApiError;
username: string;
// TODO PACEDTRAIN: Remove pacedTrain after development pacedTrain feature
userPreferences: { safeWord: string; showPacedTrains?: boolean };
userRoles: BuiltinRole[];
account: Record<string, string>;
}
export const userInitialState: UserState = {
isLogged: false,
loginError: undefined,
username: '',
// TODO PACEDTRAIN: Remove pacedTrain after development pacedTrain feature
userPreferences: { safeWord: '', showPacedTrains: false },
userRoles: [],
account: {},
};
export const userSlice = createSlice({
name: 'user',
initialState: userInitialState,
reducers: {
loginSuccess(
state,
action: PayloadAction<{
username: UserState['username'];
}>
) {
const { username } = action.payload;
state.username = username;
state.isLogged = true;
},
loginError(state, action: PayloadAction<ApiError | undefined>) {
state.isLogged = false;
state.loginError = action.payload;
},
logoutSuccess() {
return userInitialState;
},
setUserRoles(state, action: PayloadAction<BuiltinRole[] | undefined>) {
state.userRoles = action.payload || [];
},
updateUserPreferences(
state,
// TODO PACEDTRAIN: Remove pacedTrain after development pacedTrain feature
action: PayloadAction<{ safeWord: string; showPacedTrains?: boolean }>
) {
state.userPreferences = action.payload;
},
},
});
export const { loginSuccess, loginError, logoutSuccess, setUserRoles, updateUserPreferences } =
userSlice.actions;
export default userSlice.reducer;