blob: b04d5db9cdbde060227d9d42fbd5f57c5d7c7c83 [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';
simon20076982022-10-11 15:04:13 -040019import { Account } from 'jami-web-common';
idillond858c182022-09-16 13:18:26 -040020
21// Define a type for the slice state
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040022export interface AppState {
23 // TODO: Remove accountId when account endpoints available.
24 // Left for backwards compatibility, not necessary, included in token.
idillon531b6f22022-09-16 14:02:00 -040025 accountId: string;
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040026 account?: Account;
27 // TODO : Evaluate need for this when WebSocket will be available.
ervinanoh34eb9472022-09-13 04:20:28 -040028 refresh: boolean;
idillond858c182022-09-16 13:18:26 -040029}
30
31// Define the initial state using that type
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040032const initialState: AppState = {
simond47ef9e2022-09-28 22:24:28 -040033 accountId: '',
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040034 account: undefined,
ervinanoh34eb9472022-09-13 04:20:28 -040035 refresh: true,
idillond858c182022-09-16 13:18:26 -040036};
37
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040038export const userInfoSlice = createSlice({
39 name: 'userInfo',
idillond858c182022-09-16 13:18:26 -040040 // `createSlice` will infer the state type from the `initialState` argument
41 initialState,
42 reducers: {
idillon531b6f22022-09-16 14:02:00 -040043 setAccountId: (state, action: PayloadAction<string>) => {
44 state.accountId = action.payload;
idillond858c182022-09-16 13:18:26 -040045 },
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040046 setAccount: (state, action: PayloadAction<Account | undefined>) => {
47 state.account = action.payload;
ervinanoh34eb9472022-09-13 04:20:28 -040048 },
49 setRefreshFromSlice: (state) => {
50 state.refresh = !state.refresh;
ervinanoh99655642022-09-01 15:11:31 -040051 },
idillond858c182022-09-16 13:18:26 -040052 },
53});
54
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040055export const { setAccountId, setAccount, setRefreshFromSlice } = userInfoSlice.actions;
idillond858c182022-09-16 13:18:26 -040056
57// Other code such as selectors can use the imported `RootState` type
idillon531b6f22022-09-16 14:02:00 -040058// export const selectCount = (state: RootState) => state.app.value;
idillond858c182022-09-16 13:18:26 -040059
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040060export default userInfoSlice.reducer;