simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 18 | import { createSlice, PayloadAction } from '@reduxjs/toolkit'; |
simon | 2007698 | 2022-10-11 15:04:13 -0400 | [diff] [blame] | 19 | import { Account } from 'jami-web-common'; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 20 | |
| 21 | // Define a type for the slice state |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame^] | 22 | export interface AppState { |
| 23 | // TODO: Remove accountId when account endpoints available. |
| 24 | // Left for backwards compatibility, not necessary, included in token. |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 25 | accountId: string; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame^] | 26 | account?: Account; |
| 27 | // TODO : Evaluate need for this when WebSocket will be available. |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 28 | refresh: boolean; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | // Define the initial state using that type |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame^] | 32 | const initialState: AppState = { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 33 | accountId: '', |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame^] | 34 | account: undefined, |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 35 | refresh: true, |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 36 | }; |
| 37 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame^] | 38 | export const userInfoSlice = createSlice({ |
| 39 | name: 'userInfo', |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 40 | // `createSlice` will infer the state type from the `initialState` argument |
| 41 | initialState, |
| 42 | reducers: { |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 43 | setAccountId: (state, action: PayloadAction<string>) => { |
| 44 | state.accountId = action.payload; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 45 | }, |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame^] | 46 | setAccount: (state, action: PayloadAction<Account | undefined>) => { |
| 47 | state.account = action.payload; |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 48 | }, |
| 49 | setRefreshFromSlice: (state) => { |
| 50 | state.refresh = !state.refresh; |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 51 | }, |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 52 | }, |
| 53 | }); |
| 54 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame^] | 55 | export const { setAccountId, setAccount, setRefreshFromSlice } = userInfoSlice.actions; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 56 | |
| 57 | // Other code such as selectors can use the imported `RootState` type |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 58 | // export const selectCount = (state: RootState) => state.app.value; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 59 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame^] | 60 | export default userInfoSlice.reducer; |