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