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