idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 1 | import { createSlice, PayloadAction } from "@reduxjs/toolkit"; |
| 2 | import type { RootState } from "./store"; |
| 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; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 7 | } |
| 8 | |
| 9 | // Define the initial state using that type |
| 10 | const initialState: appState = { |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame^] | 11 | accountId: "", |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 12 | }; |
| 13 | |
| 14 | export const appSlice = createSlice({ |
| 15 | name: "app", |
| 16 | // `createSlice` will infer the state type from the `initialState` argument |
| 17 | initialState, |
| 18 | reducers: { |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame^] | 19 | setAccountId: (state, action: PayloadAction<string>) => { |
| 20 | state.accountId = action.payload; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 21 | }, |
| 22 | }, |
| 23 | }); |
| 24 | |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame^] | 25 | export const { setAccountId } = appSlice.actions; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 26 | |
| 27 | // Other code such as selectors can use the imported `RootState` type |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame^] | 28 | // export const selectCount = (state: RootState) => state.app.value; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 29 | |
| 30 | export default appSlice.reducer; |