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 |
| 22 | interface appState { |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 23 | accountId: string; |
simon | c7d5245 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 24 | accountObject: Account | null; |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 25 | refresh: boolean; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | // Define the initial state using that type |
| 29 | const initialState: appState = { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 30 | accountId: '', |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 31 | accountObject: null, |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 32 | refresh: true, |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | export const appSlice = createSlice({ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 36 | name: 'app', |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 37 | // `createSlice` will infer the state type from the `initialState` argument |
| 38 | initialState, |
| 39 | reducers: { |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 40 | setAccountId: (state, action: PayloadAction<string>) => { |
| 41 | state.accountId = action.payload; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 42 | }, |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 43 | setAccountObject: (state, action: PayloadAction<Account>) => { |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 44 | state.accountObject = action.payload; |
| 45 | }, |
| 46 | setRefreshFromSlice: (state) => { |
| 47 | state.refresh = !state.refresh; |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 48 | }, |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 49 | }, |
| 50 | }); |
| 51 | |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 52 | export const { setAccountId, setAccountObject, setRefreshFromSlice } = appSlice.actions; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 53 | |
| 54 | // Other code such as selectors can use the imported `RootState` type |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 55 | // export const selectCount = (state: RootState) => state.app.value; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 56 | |
| 57 | export default appSlice.reducer; |