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; |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 8 | accountObject: Account; |
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 = { |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 13 | accountId: "", |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 14 | accountObject: null, |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 15 | }; |
| 16 | |
| 17 | export const appSlice = createSlice({ |
| 18 | name: "app", |
| 19 | // `createSlice` will infer the state type from the `initialState` argument |
| 20 | initialState, |
| 21 | reducers: { |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 22 | setAccountId: (state, action: PayloadAction<string>) => { |
| 23 | state.accountId = action.payload; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 24 | }, |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 25 | setAccountObject: (state, action: PayloadAction<Account>) => { |
| 26 | state.accountObject = action.payload ; |
| 27 | }, |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 28 | }, |
| 29 | }); |
| 30 | |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 31 | export const { setAccountId, setAccountObject } = appSlice.actions; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 32 | |
| 33 | // Other code such as selectors can use the imported `RootState` type |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 34 | // export const selectCount = (state: RootState) => state.app.value; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 35 | |
| 36 | export default appSlice.reducer; |