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'; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 19 | |
| 20 | // Define a type for the slice state |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 21 | export interface AppState { |
| 22 | // TODO: Remove accountId when account endpoints available. |
| 23 | // Left for backwards compatibility, not necessary, included in token. |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 24 | accountId: string; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 25 | // TODO : Evaluate need for this when WebSocket will be available. |
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 |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 30 | const initialState: AppState = { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 31 | accountId: '', |
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 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 35 | export const userInfoSlice = createSlice({ |
| 36 | name: 'userInfo', |
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 | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 43 | setRefreshFromSlice: (state) => { |
| 44 | state.refresh = !state.refresh; |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 45 | }, |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 46 | }, |
| 47 | }); |
| 48 | |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 49 | export const { setAccountId, setRefreshFromSlice } = userInfoSlice.actions; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 50 | |
| 51 | // Other code such as selectors can use the imported `RootState` type |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 52 | // export const selectCount = (state: RootState) => state.app.value; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 53 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 54 | export default userInfoSlice.reducer; |