blob: c85ebd767c3490dec4715f52847f5f60ceb2d8c4 [file] [log] [blame]
simon26e79f72022-10-05 22:16:08 -04001/*
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 */
simond47ef9e2022-09-28 22:24:28 -040018import { createSlice, PayloadAction } from '@reduxjs/toolkit';
simon07b4eb02022-09-29 17:50:26 -040019
simond47ef9e2022-09-28 22:24:28 -040020import Account from '../../model/Account';
idillond858c182022-09-16 13:18:26 -040021
22// Define a type for the slice state
23interface appState {
idillon531b6f22022-09-16 14:02:00 -040024 accountId: string;
simonc7d52452022-09-23 02:09:42 -040025 accountObject: Account | null;
ervinanoh34eb9472022-09-13 04:20:28 -040026 refresh: boolean;
idillond858c182022-09-16 13:18:26 -040027}
28
29// Define the initial state using that type
30const initialState: appState = {
simond47ef9e2022-09-28 22:24:28 -040031 accountId: '',
ervinanoh99655642022-09-01 15:11:31 -040032 accountObject: null,
ervinanoh34eb9472022-09-13 04:20:28 -040033 refresh: true,
idillond858c182022-09-16 13:18:26 -040034};
35
36export const appSlice = createSlice({
simond47ef9e2022-09-28 22:24:28 -040037 name: 'app',
idillond858c182022-09-16 13:18:26 -040038 // `createSlice` will infer the state type from the `initialState` argument
39 initialState,
40 reducers: {
idillon531b6f22022-09-16 14:02:00 -040041 setAccountId: (state, action: PayloadAction<string>) => {
42 state.accountId = action.payload;
idillond858c182022-09-16 13:18:26 -040043 },
ervinanoh99655642022-09-01 15:11:31 -040044 setAccountObject: (state, action: PayloadAction<Account>) => {
ervinanoh34eb9472022-09-13 04:20:28 -040045 state.accountObject = action.payload;
46 },
47 setRefreshFromSlice: (state) => {
48 state.refresh = !state.refresh;
ervinanoh99655642022-09-01 15:11:31 -040049 },
idillond858c182022-09-16 13:18:26 -040050 },
51});
52
ervinanoh34eb9472022-09-13 04:20:28 -040053export const { setAccountId, setAccountObject, setRefreshFromSlice } = appSlice.actions;
idillond858c182022-09-16 13:18:26 -040054
55// Other code such as selectors can use the imported `RootState` type
idillon531b6f22022-09-16 14:02:00 -040056// export const selectCount = (state: RootState) => state.app.value;
idillond858c182022-09-16 13:18:26 -040057
58export default appSlice.reducer;