blob: 895415b8cdf8a85bc2997fd4089132e5eef068fc [file] [log] [blame]
simon26e79f72022-10-05 22:16:08 -04001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
simond47ef9e2022-09-28 22:24:28 -040018import { createSlice, PayloadAction } from '@reduxjs/toolkit';
idillond858c182022-09-16 13:18:26 -040019
20// Define a type for the slice state
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040021export interface AppState {
22 // TODO: Remove accountId when account endpoints available.
23 // Left for backwards compatibility, not necessary, included in token.
idillon531b6f22022-09-16 14:02:00 -040024 accountId: string;
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040025 // TODO : Evaluate need for this when WebSocket will be available.
ervinanoh34eb9472022-09-13 04:20:28 -040026 refresh: boolean;
idillond858c182022-09-16 13:18:26 -040027}
28
29// Define the initial state using that type
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040030const initialState: AppState = {
simond47ef9e2022-09-28 22:24:28 -040031 accountId: '',
ervinanoh34eb9472022-09-13 04:20:28 -040032 refresh: true,
idillond858c182022-09-16 13:18:26 -040033};
34
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040035export const userInfoSlice = createSlice({
36 name: 'userInfo',
idillond858c182022-09-16 13:18:26 -040037 // `createSlice` will infer the state type from the `initialState` argument
38 initialState,
39 reducers: {
idillon531b6f22022-09-16 14:02:00 -040040 setAccountId: (state, action: PayloadAction<string>) => {
41 state.accountId = action.payload;
idillond858c182022-09-16 13:18:26 -040042 },
ervinanoh34eb9472022-09-13 04:20:28 -040043 setRefreshFromSlice: (state) => {
44 state.refresh = !state.refresh;
ervinanoh99655642022-09-01 15:11:31 -040045 },
idillond858c182022-09-16 13:18:26 -040046 },
47});
48
simon416d0792022-11-03 02:46:18 -040049export const { setAccountId, setRefreshFromSlice } = userInfoSlice.actions;
idillond858c182022-09-16 13:18:26 -040050
51// Other code such as selectors can use the imported `RootState` type
idillon531b6f22022-09-16 14:02:00 -040052// export const selectCount = (state: RootState) => state.app.value;
idillond858c182022-09-16 13:18:26 -040053
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040054export default userInfoSlice.reducer;