simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 1 | import { createSlice, PayloadAction } from '@reduxjs/toolkit'; |
| 2 | import Account from '../../model/Account'; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 3 | |
| 4 | // Define a type for the slice state |
| 5 | interface appState { |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 6 | accountId: string; |
simon | c7d5245 | 2022-09-23 02:09:42 -0400 | [diff] [blame] | 7 | accountObject: Account | null; |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 8 | refresh: boolean; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 9 | } |
| 10 | |
| 11 | // Define the initial state using that type |
| 12 | const initialState: appState = { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 13 | accountId: '', |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 14 | accountObject: null, |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 15 | refresh: true, |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 16 | }; |
| 17 | |
| 18 | export const appSlice = createSlice({ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 19 | name: 'app', |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 20 | // `createSlice` will infer the state type from the `initialState` argument |
| 21 | initialState, |
| 22 | reducers: { |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 23 | setAccountId: (state, action: PayloadAction<string>) => { |
| 24 | state.accountId = action.payload; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 25 | }, |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 26 | setAccountObject: (state, action: PayloadAction<Account>) => { |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 27 | state.accountObject = action.payload; |
| 28 | }, |
| 29 | setRefreshFromSlice: (state) => { |
| 30 | state.refresh = !state.refresh; |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 31 | }, |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 32 | }, |
| 33 | }); |
| 34 | |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 35 | export const { setAccountId, setAccountObject, setRefreshFromSlice } = appSlice.actions; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 36 | |
| 37 | // Other code such as selectors can use the imported `RootState` type |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 38 | // export const selectCount = (state: RootState) => state.app.value; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 39 | |
| 40 | export default appSlice.reducer; |