A local-first personal finance app
[Redux Toolkit Migration] usersSlice [Final PR] (#4128)
Joel Jeremy Marquez0cc817f2ef2d
diff --git a/packages/desktop-client/src/components/App.tsx b/packages/desktop-client/src/components/App.tsxindex 236c7736ea..5d25b4c368 100644--- a/packages/desktop-client/src/components/App.tsx+++ b/packages/desktop-client/src/components/App.tsx@@ -14,13 +14,13 @@ import { BrowserRouter } from 'react-router-dom';import { styles } from '@actual-app/components/styles';import { View } from '@actual-app/components/view';-import { signOut } from 'loot-core/client/actions';import { setAppState, sync } from 'loot-core/client/app/appSlice';import { closeBudget, loadBudget } from 'loot-core/client/budgets/budgetsSlice';import { addNotification } from 'loot-core/client/notifications/notificationsSlice';import * as Platform from 'loot-core/client/platform';import { loadGlobalPrefs } from 'loot-core/client/prefs/prefsSlice';import { SpreadsheetProvider } from 'loot-core/client/SpreadsheetProvider';+import { signOut } from 'loot-core/client/users/usersSlice';import { init as initConnection, send } from 'loot-core/platform/client/fetch';import { handleGlobalEvents } from '../global-events';@@ -144,7 +144,9 @@ function AppInner() {message: t('Login expired, please log in again.'),button: {title: t('Go to log in'),- action: () => dispatch(signOut()),+ action: () => {+ dispatch(signOut());+ },},},}),diff --git a/packages/desktop-client/src/components/LoggedInUser.tsx b/packages/desktop-client/src/components/LoggedInUser.tsxindex 83a8f01852..bf06929736 100644--- a/packages/desktop-client/src/components/LoggedInUser.tsx+++ b/packages/desktop-client/src/components/LoggedInUser.tsx@@ -10,8 +10,8 @@ import { Text } from '@actual-app/components/text';import { theme } from '@actual-app/components/theme';import { View } from '@actual-app/components/view';-import { getUserData, signOut } from 'loot-core/client/actions';import { closeBudget } from 'loot-core/client/budgets/budgetsSlice';+import { getUserData, signOut } from 'loot-core/client/users/usersSlice';import { listen } from 'loot-core/platform/client/fetch';import { type RemoteFile, type SyncedLocalFile } from 'loot-core/types/file';import { type TransObjectLiteral } from 'loot-core/types/util';diff --git a/packages/desktop-client/src/components/admin/UserAccess/UserAccessRow.tsx b/packages/desktop-client/src/components/admin/UserAccess/UserAccessRow.tsxindex fca1bb3be5..df5d5bb639 100644--- a/packages/desktop-client/src/components/admin/UserAccess/UserAccessRow.tsx+++ b/packages/desktop-client/src/components/admin/UserAccess/UserAccessRow.tsx@@ -5,8 +5,8 @@ import { useTranslation } from 'react-i18next';import { theme } from '@actual-app/components/theme';import { View } from '@actual-app/components/view';-import { signOut } from 'loot-core/client/actions';import { addNotification } from 'loot-core/client/notifications/notificationsSlice';+import { signOut } from 'loot-core/client/users/usersSlice';import { send } from 'loot-core/platform/client/fetch';import { getUserAccessErrors } from 'loot-core/shared/errors';import { type UserAvailable } from 'loot-core/types/models';diff --git a/packages/desktop-client/src/components/admin/UserDirectory/UserDirectory.tsx b/packages/desktop-client/src/components/admin/UserDirectory/UserDirectory.tsxindex 5b5425bbad..6d0e3a8f8d 100644--- a/packages/desktop-client/src/components/admin/UserDirectory/UserDirectory.tsx+++ b/packages/desktop-client/src/components/admin/UserDirectory/UserDirectory.tsx@@ -16,9 +16,9 @@ import { Text } from '@actual-app/components/text';import { theme } from '@actual-app/components/theme';import { View } from '@actual-app/components/view';-import { signOut } from 'loot-core/client/actions';import { pushModal } from 'loot-core/client/modals/modalsSlice';import { addNotification } from 'loot-core/client/notifications/notificationsSlice';+import { signOut } from 'loot-core/client/users/usersSlice';import { send } from 'loot-core/platform/client/fetch';import * as undo from 'loot-core/platform/client/undo';import { type NewUserEntity, type UserEntity } from 'loot-core/types/models';@@ -174,7 +174,9 @@ function UserDirectoryContent({message: getUserDirectoryErrors(error),button: {title: t('Go to login'),- action: () => dispatch(signOut()),+ action: () => {+ dispatch(signOut());+ },},},}),diff --git a/packages/desktop-client/src/components/manager/BudgetList.tsx b/packages/desktop-client/src/components/manager/BudgetList.tsxindex 18b9e3e614..1cf731f2f6 100644--- a/packages/desktop-client/src/components/manager/BudgetList.tsx+++ b/packages/desktop-client/src/components/manager/BudgetList.tsx@@ -33,7 +33,6 @@ import { tokens } from '@actual-app/components/tokens';import { Tooltip } from '@actual-app/components/tooltip';import { View } from '@actual-app/components/view';-import { getUserData } from 'loot-core/client/actions';import {closeAndDownloadBudget,closeAndLoadBudget,@@ -43,6 +42,7 @@ import {loadBudget,} from 'loot-core/client/budgets/budgetsSlice';import { pushModal } from 'loot-core/client/modals/modalsSlice';+import { getUserData } from 'loot-core/client/users/usersSlice';import {isElectron,isNonProductionEnvironment,diff --git a/packages/desktop-client/src/components/manager/ConfigServer.tsx b/packages/desktop-client/src/components/manager/ConfigServer.tsxindex abe7248c7a..4235880f8e 100644--- a/packages/desktop-client/src/components/manager/ConfigServer.tsx+++ b/packages/desktop-client/src/components/manager/ConfigServer.tsx@@ -8,8 +8,8 @@ import { Text } from '@actual-app/components/text';import { theme } from '@actual-app/components/theme';import { View } from '@actual-app/components/view';-import { loggedIn, signOut } from 'loot-core/client/actions';import { createBudget } from 'loot-core/client/budgets/budgetsSlice';+import { loggedIn, signOut } from 'loot-core/client/users/usersSlice';import {isNonProductionEnvironment,isElectron,diff --git a/packages/desktop-client/src/components/manager/ManagementApp.tsx b/packages/desktop-client/src/components/manager/ManagementApp.tsxindex 2c30dc9bc7..b191312fa0 100644--- a/packages/desktop-client/src/components/manager/ManagementApp.tsx+++ b/packages/desktop-client/src/components/manager/ManagementApp.tsx@@ -7,8 +7,8 @@ import { theme } from '@actual-app/components/theme';import { tokens } from '@actual-app/components/tokens';import { View } from '@actual-app/components/view';-import { loggedIn } from 'loot-core/client/actions';import { setAppState } from 'loot-core/client/app/appSlice';+import { loggedIn } from 'loot-core/client/users/usersSlice';import { ProtectedRoute } from '../../auth/ProtectedRoute';import { Permissions } from '../../auth/types';diff --git a/packages/desktop-client/src/components/manager/subscribe/Login.tsx b/packages/desktop-client/src/components/manager/subscribe/Login.tsxindex e6074ab81c..3328f6bfd7 100644--- a/packages/desktop-client/src/components/manager/subscribe/Login.tsx+++ b/packages/desktop-client/src/components/manager/subscribe/Login.tsx@@ -14,7 +14,7 @@ import { Text } from '@actual-app/components/text';import { theme } from '@actual-app/components/theme';import { View } from '@actual-app/components/view';-import { loggedIn } from 'loot-core/client/actions/user';+import { loggedIn } from 'loot-core/client/users/usersSlice';import { send } from 'loot-core/platform/client/fetch';import { isElectron } from 'loot-core/shared/environment';import { type OpenIdConfig } from 'loot-core/types/models';diff --git a/packages/desktop-client/src/components/manager/subscribe/OpenIdCallback.ts b/packages/desktop-client/src/components/manager/subscribe/OpenIdCallback.tsindex 9784ee280f..b19b141f1b 100644--- a/packages/desktop-client/src/components/manager/subscribe/OpenIdCallback.ts+++ b/packages/desktop-client/src/components/manager/subscribe/OpenIdCallback.ts@@ -1,6 +1,6 @@import { useEffect } from 'react';-import { loggedIn } from 'loot-core/client/actions/user';+import { loggedIn } from 'loot-core/client/users/usersSlice';import { send } from 'loot-core/platform/client/fetch';import { useDispatch } from '../../../redux';diff --git a/packages/desktop-client/src/components/modals/EditAccess.tsx b/packages/desktop-client/src/components/modals/EditAccess.tsxindex 40ec611ae5..1900503cba 100644--- a/packages/desktop-client/src/components/modals/EditAccess.tsx+++ b/packages/desktop-client/src/components/modals/EditAccess.tsx@@ -9,12 +9,12 @@ import { Text } from '@actual-app/components/text';import { theme } from '@actual-app/components/theme';import { View } from '@actual-app/components/view';-import { signOut } from 'loot-core/client/actions';import {type Modal as ModalType,popModal,} from 'loot-core/client/modals/modalsSlice';import { addNotification } from 'loot-core/client/notifications/notificationsSlice';+import { signOut } from 'loot-core/client/users/usersSlice';import { send } from 'loot-core/platform/client/fetch';import { getUserAccessErrors } from 'loot-core/shared/errors';diff --git a/packages/desktop-client/src/components/modals/EditUser.tsx b/packages/desktop-client/src/components/modals/EditUser.tsxindex 2a9d3b4070..25407303b8 100644--- a/packages/desktop-client/src/components/modals/EditUser.tsx+++ b/packages/desktop-client/src/components/modals/EditUser.tsx@@ -10,12 +10,12 @@ import { Text } from '@actual-app/components/text';import { theme } from '@actual-app/components/theme';import { View } from '@actual-app/components/view';-import { signOut } from 'loot-core/client/actions';import {type Modal as ModalType,popModal,} from 'loot-core/client/modals/modalsSlice';import { addNotification } from 'loot-core/client/notifications/notificationsSlice';+import { signOut } from 'loot-core/client/users/usersSlice';import { send } from 'loot-core/platform/client/fetch';import { PossibleRoles } from 'loot-core/shared/user';import { type NewUserEntity, type UserEntity } from 'loot-core/types/models';diff --git a/packages/desktop-client/src/index.tsx b/packages/desktop-client/src/index.tsxindex 8b026d1c6c..188c2895f4 100644--- a/packages/desktop-client/src/index.tsx+++ b/packages/desktop-client/src/index.tsx@@ -14,7 +14,6 @@ import { bindActionCreators } from '@reduxjs/toolkit';import { createRoot } from 'react-dom/client';import * as accountsSlice from 'loot-core/client/accounts/accountsSlice';-import * as actions from 'loot-core/client/actions';import * as appSlice from 'loot-core/client/app/appSlice';import * as budgetsSlice from 'loot-core/client/budgets/budgetsSlice';import * as modalsSlice from 'loot-core/client/modals/modalsSlice';@@ -24,6 +23,7 @@ import * as queriesSlice from 'loot-core/client/queries/queriesSlice';import { runQuery } from 'loot-core/client/query-helpers';import { store } from 'loot-core/client/store';import { redo, undo } from 'loot-core/client/undo';+import * as usersSlice from 'loot-core/client/users/usersSlice';import { send } from 'loot-core/platform/client/fetch';import { q } from 'loot-core/shared/query';@@ -37,7 +37,6 @@ import 'focus-visible';const boundActions = bindActionCreators({- ...actions,...accountsSlice.actions,...appSlice.actions,...budgetsSlice.actions,@@ -45,6 +44,7 @@ const boundActions = bindActionCreators(...notificationsSlice.actions,...prefsSlice.actions,...queriesSlice.actions,+ ...usersSlice.actions,},store.dispatch,);diff --git a/packages/loot-core/src/client/accounts/accountsSlice.ts b/packages/loot-core/src/client/accounts/accountsSlice.tsindex 425d39994d..d79737d5fb 100644--- a/packages/loot-core/src/client/accounts/accountsSlice.ts+++ b/packages/loot-core/src/client/accounts/accountsSlice.ts@@ -9,6 +9,7 @@ import {type SyncServerSimpleFinAccount,type SyncServerPluggyAiAccount,} from '../../types/models';+import { resetApp } from '../app/appSlice';import { addNotification } from '../notifications/notificationsSlice';import {getAccounts,@@ -69,6 +70,9 @@ const accountsSlice = createSlice({delete state.failedAccounts[action.payload.id];},},+ extraReducers: builder => {+ builder.addCase(resetApp, () => initialState);+ },});type UnlinkAccountPayload = {diff --git a/packages/loot-core/src/client/actions/index.ts b/packages/loot-core/src/client/actions/index.tsdeleted file mode 100644index e5abc85650..0000000000--- a/packages/loot-core/src/client/actions/index.ts+++ /dev/null@@ -1 +0,0 @@-export * from './user';diff --git a/packages/loot-core/src/client/actions/user.ts b/packages/loot-core/src/client/actions/user.tsdeleted file mode 100644index d5974207da..0000000000--- a/packages/loot-core/src/client/actions/user.ts+++ /dev/null@@ -1,50 +0,0 @@-import { send } from '../../platform/client/fetch';-import { closeBudget, loadAllFiles } from '../budgets/budgetsSlice';-import * as constants from '../constants';-import { loadGlobalPrefs } from '../prefs/prefsSlice';-import { type AppDispatch } from '../store';--export function getUserData() {- return async (dispatch: AppDispatch) => {- const data = await send('subscribe-get-user');-- dispatch({- type: constants.GET_USER_DATA,- data,- });- return data;- };-}--export function loggedIn() {- return async (dispatch: AppDispatch) => {- await dispatch(getUserData());-- // We want to be careful about how we call loadAllFiles. It will- // turn the files state from null into an array, indicating that- // we've loaded the files at least once. This first attempt at- // loading files is important - the manager uses files to decide- // if it should create a new file automatically (if there are no- // files). If we call it before we have a properly logged in- // user, later on it could trigger the codepath which- // automatically creates a file, but we really haven't loaded- // their files yet.- //- // Since the above comment was written, We let in users even if- // their account is invalid. So we should list all their files- // regardless. Previously, we kicked users out to a "need payment- // info" screen and we didn't want to call this.- dispatch(loadAllFiles());- };-}--export function signOut() {- return async (dispatch: AppDispatch) => {- await send('subscribe-sign-out');-- dispatch(getUserData());- dispatch(loadGlobalPrefs());- dispatch(closeBudget());- dispatch({ type: constants.SIGN_OUT });- };-}diff --git a/packages/loot-core/src/client/app/appSlice.ts b/packages/loot-core/src/client/app/appSlice.tsindex 312ea8ba92..db2e194b07 100644--- a/packages/loot-core/src/client/app/appSlice.ts+++ b/packages/loot-core/src/client/app/appSlice.ts@@ -1,4 +1,8 @@-import { createSlice, type PayloadAction } from '@reduxjs/toolkit';+import {+ createAction,+ createSlice,+ type PayloadAction,+} from '@reduxjs/toolkit';import { send } from '../../platform/client/fetch';import { getUploadError } from '../../shared/errors';@@ -29,6 +33,8 @@ const initialState: AppState = {managerHasInitialized: false,};+export const resetApp = createAction(`${sliceName}/resetApp`);+export const updateApp = createAppAsyncThunk(`${sliceName}/updateApp`,async (_, { dispatch }) => {@@ -146,12 +152,20 @@ const appSlice = createSlice({};},},+ extraReducers: builder => {+ builder.addCase(resetApp, state => ({+ ...initialState,+ loadingText: state.loadingText || null,+ managerHasInitialized: state.managerHasInitialized || false,+ }));+ },});export const { name, reducer, getInitialState } = appSlice;export const actions = {...appSlice.actions,+ resetApp,updateApp,resetSync,sync,diff --git a/packages/loot-core/src/client/budgets/budgetsSlice.ts b/packages/loot-core/src/client/budgets/budgetsSlice.tsindex 59253ed754..c8e44f6339 100644--- a/packages/loot-core/src/client/budgets/budgetsSlice.ts+++ b/packages/loot-core/src/client/budgets/budgetsSlice.ts@@ -7,11 +7,11 @@ import { getDownloadError, getSyncError } from '../../shared/errors';import { type Budget } from '../../types/budget';import { type File } from '../../types/file';import { type Handlers } from '../../types/handlers';-import { setAppState } from '../app/appSlice';-import * as constants from '../constants';+import { resetApp, setAppState } from '../app/appSlice';import { closeModal, pushModal } from '../modals/modalsSlice';import { loadGlobalPrefs, loadPrefs } from '../prefs/prefsSlice';import { createAppAsyncThunk } from '../redux';+import { signOut } from '../users/usersSlice';const sliceName = 'budgets';@@ -101,10 +101,7 @@ export const closeBudget = createAppAsyncThunk(async (_, { dispatch, getState }) => {const prefs = getState().prefs.local;if (prefs && prefs.id) {- // This clears out all the app state so the user starts fresh- // TODO: Change to use an action once CLOSE_BUDGET is migrated to redux toolkit.- await dispatch({ type: constants.CLOSE_BUDGET });-+ await dispatch(resetApp());await dispatch(setAppState({ loadingText: t('Closing...') }));await send('close-budget');await dispatch(setAppState({ loadingText: null }));@@ -120,8 +117,7 @@ export const closeBudgetUI = createAppAsyncThunk(async (_, { dispatch, getState }) => {const prefs = getState().prefs.local;if (prefs && prefs.id) {- // TODO: Change to use an action once CLOSE_BUDGET is migrated to redux toolkit.- await dispatch({ type: constants.CLOSE_BUDGET });+ await dispatch(resetApp());}},);@@ -445,9 +441,12 @@ const budgetsSlice = createSlice({action.payload.remoteFiles,);},- signOut(state) {+ },+ extraReducers: builder => {+ builder.addCase(signOut.fulfilled, state => {state.allFiles = null;- },+ });+ builder.addCase(resetApp, state => state || initialState);},});@@ -473,7 +472,7 @@ export const actions = {makeBackup,};-export const { setBudgets, setRemoteFiles, setAllFiles, signOut } = actions;+export const { setBudgets, setRemoteFiles, setAllFiles } = actions;function sortFiles(arr: File[]) {arr.sort((x, y) => {diff --git a/packages/loot-core/src/client/constants.ts b/packages/loot-core/src/client/constants.tsdeleted file mode 100644index c2d2de6332..0000000000--- a/packages/loot-core/src/client/constants.ts+++ /dev/null@@ -1,21 +0,0 @@-export const SET_PREFS = 'SET_PREFS';-export const MERGE_LOCAL_PREFS = 'MERGE_LOCAL_PREFS';-export const MERGE_GLOBAL_PREFS = 'MERGE_GLOBAL_PREFS';-export const MERGE_SYNCED_PREFS = 'MERGE_SYNCED_PREFS';-export const SET_BUDGETS = 'SET_BUDGETS';-export const SET_REMOTE_FILES = 'SET_REMOTE_FILES';-export const SET_ALL_FILES = 'SET_ALL_FILES';-export const CLOSE_BUDGET = 'CLOSE_BUDGET';-export const SET_APP_STATE = 'SET_APP_STATE';-export const PUSH_MODAL = 'PUSH_MODAL';-export const REPLACE_MODAL = 'REPLACE_MODAL';-export const CLOSE_MODAL = 'CLOSE_MODAL';-export const COLLAPSE_MODALS = 'COLLAPSE_MODALS';-export const POP_MODAL = 'POP_MODAL';-export const ADD_NOTIFICATION = 'ADD_NOTIFICATION';-export const REMOVE_NOTIFICATION = 'REMOVE_NOTIFICATION';-export const SET_NOTIFICATION_INSET = 'SET_NOTIFICATION_INSET';-export const GET_USER_DATA = 'GET_USER_DATA';-export const SET_ACCOUNTS_SYNCING = 'SET_ACCOUNTS_SYNCING';-export const ACCOUNT_SYNC_STATUS = 'ACCOUNT_SYNC_STATUS';-export const SIGN_OUT = 'SIGN_OUT';diff --git a/packages/loot-core/src/client/modals/modalsSlice.ts b/packages/loot-core/src/client/modals/modalsSlice.tsindex 57664ef55b..9222879ada 100644--- a/packages/loot-core/src/client/modals/modalsSlice.ts+++ b/packages/loot-core/src/client/modals/modalsSlice.ts@@ -17,9 +17,9 @@ import {type NewUserEntity,type NoteEntity,} from '../../types/models';-import { setAppState } from '../app/appSlice';-import { signOut } from '../budgets/budgetsSlice';+import { resetApp, setAppState } from '../app/appSlice';import { createAppAsyncThunk } from '../redux';+import { signOut } from '../users/usersSlice';const sliceName = 'modals';@@ -626,11 +626,11 @@ const modalsSlice = createSlice({},},extraReducers: builder => {- builder- .addCase(setAppState, (state, action) => {- state.isHidden = action.payload.loadingText !== null;- })- .addCase(signOut, () => initialState);+ builder.addCase(setAppState, (state, action) => {+ state.isHidden = action.payload.loadingText !== null;+ });+ builder.addCase(signOut.fulfilled, () => initialState);+ builder.addCase(resetApp, () => initialState);},});diff --git a/packages/loot-core/src/client/notifications/notificationsSlice.ts b/packages/loot-core/src/client/notifications/notificationsSlice.tsindex d171ec356f..b5304dbf33 100644--- a/packages/loot-core/src/client/notifications/notificationsSlice.ts+++ b/packages/loot-core/src/client/notifications/notificationsSlice.ts@@ -2,6 +2,8 @@ import { createSlice, type PayloadAction } from '@reduxjs/toolkit';import { t } from 'i18next';import { v4 as uuidv4 } from 'uuid';+import { resetApp } from '../app/appSlice';+const sliceName = 'notifications';export type Notification = {@@ -90,6 +92,9 @@ const notificationsSlice = createSlice({state.inset = action.payload?.inset ? action.payload.inset : {};},},+ extraReducers: builder => {+ builder.addCase(resetApp, () => initialState);+ },});export const { name, reducer, getInitialState } = notificationsSlice;diff --git a/packages/loot-core/src/client/prefs/prefsSlice.ts b/packages/loot-core/src/client/prefs/prefsSlice.tsindex 0fca3c48f0..736c38e6b0 100644--- a/packages/loot-core/src/client/prefs/prefsSlice.ts+++ b/packages/loot-core/src/client/prefs/prefsSlice.ts@@ -9,6 +9,7 @@ import {type MetadataPrefs,type SyncedPrefs,} from '../../types/prefs';+import { resetApp } from '../app/appSlice';import { closeModal } from '../modals/modalsSlice';import { createAppAsyncThunk } from '../redux';@@ -153,6 +154,12 @@ const prefsSlice = createSlice({state.synced = { ...state.synced, ...action.payload };},},+ extraReducers: builder => {+ builder.addCase(resetApp, state => ({+ ...initialState,+ global: state.global || initialState.global,+ }));+ },});export const { name, reducer, getInitialState } = prefsSlice;diff --git a/packages/loot-core/src/client/queries/queriesSlice.ts b/packages/loot-core/src/client/queries/queriesSlice.tsindex 7c0ee453ed..85d1bfcd28 100644--- a/packages/loot-core/src/client/queries/queriesSlice.ts+++ b/packages/loot-core/src/client/queries/queriesSlice.ts@@ -12,6 +12,7 @@ import {type AccountEntity,type PayeeEntity,} from '../../types/models';+import { resetApp } from '../app/appSlice';import {addGenericErrorNotification,addNotification,@@ -154,6 +155,10 @@ const queriesSlice = createSlice({state.payees = action.payload;state.payeesLoaded = true;});++ // App++ builder.addCase(resetApp, () => initialState);},});diff --git a/packages/loot-core/src/client/reducers/index.ts b/packages/loot-core/src/client/reducers/index.tsdeleted file mode 100644index 0c45afd7b4..0000000000--- a/packages/loot-core/src/client/reducers/index.ts+++ /dev/null@@ -1,5 +0,0 @@-import { update as user } from './user';--export const reducers = {- user,-};diff --git a/packages/loot-core/src/client/reducers/user.ts b/packages/loot-core/src/client/reducers/user.tsdeleted file mode 100644index 03c1746034..0000000000--- a/packages/loot-core/src/client/reducers/user.ts+++ /dev/null@@ -1,16 +0,0 @@-import * as constants from '../constants';-import type { UserActions, UserState } from '../state-types/user';--export const initialState: UserState = {- data: null,-};--export function update(state = initialState, action: UserActions): UserState {- switch (action.type) {- case constants.GET_USER_DATA:- return { ...state, data: action.data };- default:- }-- return state;-}diff --git a/packages/loot-core/src/client/shared-listeners.ts b/packages/loot-core/src/client/shared-listeners.tsindex 3561c39ed4..77393e71f4 100644--- a/packages/loot-core/src/client/shared-listeners.ts+++ b/packages/loot-core/src/client/shared-listeners.ts@@ -3,7 +3,6 @@ import { t } from 'i18next';import { listen, send } from '../platform/client/fetch';-import { signOut } from './actions';import { resetSync, sync } from './app/appSlice';import { closeAndDownloadBudget, uploadBudget } from './budgets/budgetsSlice';import { pushModal } from './modals/modalsSlice';@@ -14,6 +13,7 @@ import {import { loadPrefs } from './prefs/prefsSlice';import { getAccounts, getCategories, getPayees } from './queries/queriesSlice';import { type AppStore } from './store';+import { signOut } from './users/usersSlice';export function listenForSyncEvent(store: AppStore) {// TODO: Should this run on mobile too?@@ -347,7 +347,9 @@ export function listenForSyncEvent(store: AppStore) {id: 'login-expired',button: {title: 'Go to login',- action: () => store.dispatch(signOut()),+ action: () => {+ store.dispatch(signOut());+ },},};break;diff --git a/packages/loot-core/src/client/state-types/index.d.ts b/packages/loot-core/src/client/state-types/index.d.tsdeleted file mode 100644index 9e9d966570..0000000000--- a/packages/loot-core/src/client/state-types/index.d.ts+++ /dev/null@@ -1,13 +0,0 @@-import type * as constants from '../constants';--import type { UserActions, UserState } from './user';--export type CloseBudgetAction = {- type: typeof constants.CLOSE_BUDGET;-};--export type Action = UserActions | CloseBudgetAction;--export type State = {- user: UserState;-};diff --git a/packages/loot-core/src/client/state-types/user.d.ts b/packages/loot-core/src/client/state-types/user.d.tsdeleted file mode 100644index 289aadaf95..0000000000--- a/packages/loot-core/src/client/state-types/user.d.ts+++ /dev/null@@ -1,13 +0,0 @@-import type { Handlers } from '../../types/handlers';-import type * as constants from '../constants';--export type UserState = {- data: Awaited<ReturnType<Handlers['subscribe-get-user']>>;-};--export type GetUserDataAction = {- type: typeof constants.GET_USER_DATA;- data: State['data'];-};--export type UserActions = GetUserDataAction;diff --git a/packages/loot-core/src/client/store/index.ts b/packages/loot-core/src/client/store/index.tsindex d3af684749..2cb3ee3fb3 100644--- a/packages/loot-core/src/client/store/index.ts+++ b/packages/loot-core/src/client/store/index.ts@@ -8,45 +8,38 @@ import {import {name as accountsSliceName,reducer as accountsSliceReducer,- getInitialState as getInitialAccountsState,} from '../accounts/accountsSlice';import {name as appSliceName,reducer as appSliceReducer,- getInitialState as getInitialAppState,} from '../app/appSlice';import {name as budgetsSliceName,reducer as budgetsSliceReducer,- getInitialState as getInitialBudgetsState,} from '../budgets/budgetsSlice';-import * as constants from '../constants';import {name as modalsSliceName,reducer as modalsSliceReducer,- getInitialState as getInitialModalsState,} from '../modals/modalsSlice';import {name as notificationsSliceName,reducer as notificationsSliceReducer,- getInitialState as getInitialNotificationsState,addNotification,} from '../notifications/notificationsSlice';import {name as prefsSliceName,reducer as prefsSliceReducer,- getInitialState as getInitialPrefsState,} from '../prefs/prefsSlice';import {name as queriesSliceName,reducer as queriesSliceReducer,- getInitialState as getInitialQueriesState,} from '../queries/queriesSlice';-import { reducers } from '../reducers';-import { initialState as initialUserState } from '../reducers/user';+import {+ name as usersSliceName,+ reducer as usersSliceReducer,+} from '../users/usersSlice';-const appReducer = combineReducers({- ...reducers,+const rootReducer = combineReducers({[accountsSliceName]: accountsSliceReducer,[appSliceName]: appSliceReducer,[budgetsSliceName]: budgetsSliceReducer,@@ -54,32 +47,8 @@ const appReducer = combineReducers({[notificationsSliceName]: notificationsSliceReducer,[prefsSliceName]: prefsSliceReducer,[queriesSliceName]: queriesSliceReducer,+ [usersSliceName]: usersSliceReducer,});-const rootReducer: typeof appReducer = (state, action) => {- if (action.type === constants.CLOSE_BUDGET) {- // Reset the state and only keep around things intentionally. This- // blows away everything else- state = {- account: getInitialAccountsState(),- modals: getInitialModalsState(),- notifications: getInitialNotificationsState(),- queries: getInitialQueriesState(),- budgets: state?.budgets || getInitialBudgetsState(),- user: state?.user || initialUserState,- prefs: {- ...getInitialPrefsState(),- global: state?.prefs?.global || getInitialPrefsState().global,- },- app: {- ...getInitialAppState(),- managerHasInitialized: state?.app?.managerHasInitialized || false,- loadingText: state?.app?.loadingText || null,- },- };- }-- return appReducer(state, action);-};const notifyOnRejectedActionsMiddleware = createListenerMiddleware();notifyOnRejectedActionsMiddleware.startListening({diff --git a/packages/loot-core/src/client/store/mock.ts b/packages/loot-core/src/client/store/mock.tsindex 461d345c88..3961379802 100644--- a/packages/loot-core/src/client/store/mock.ts+++ b/packages/loot-core/src/client/store/mock.ts@@ -28,12 +28,14 @@ import {name as queriesSliceName,reducer as queriesSliceReducer,} from '../queries/queriesSlice';-import { reducers } from '../reducers';+import {+ name as usersSliceName,+ reducer as usersSliceReducer,+} from '../users/usersSlice';import { type store as realStore } from './index';const appReducer = combineReducers({- ...reducers,[accountsSliceName]: accountsSliceReducer,[appSliceName]: appSliceReducer,[budgetsSliceName]: budgetsSliceReducer,@@ -41,6 +43,7 @@ const appReducer = combineReducers({[notificationsSliceName]: notificationsSliceReducer,[prefsSliceName]: prefsSliceReducer,[queriesSliceName]: queriesSliceReducer,+ [usersSliceName]: usersSliceReducer,});export let mockStore: typeof realStore = configureStore({diff --git a/packages/loot-core/src/client/users/usersSlice.ts b/packages/loot-core/src/client/users/usersSlice.tsnew file mode 100644index 0000000000..853df41bd0--- /dev/null+++ b/packages/loot-core/src/client/users/usersSlice.ts@@ -0,0 +1,91 @@+import { createSlice, type PayloadAction } from '@reduxjs/toolkit';++import { send } from '../../platform/client/fetch';+import { type Handlers } from '../../types/handlers';+import { resetApp } from '../app/appSlice';+import { closeBudget, loadAllFiles } from '../budgets/budgetsSlice';+import { loadGlobalPrefs } from '../prefs/prefsSlice';+import { createAppAsyncThunk } from '../redux';++const sliceName = 'user';++export const getUserData = createAppAsyncThunk(+ `${sliceName}/getUserData`,+ async (_, { dispatch }) => {+ const data = await send('subscribe-get-user');+ dispatch(loadUserData({ data }));+ return data;+ },+);++export const loggedIn = createAppAsyncThunk(+ `${sliceName}/loggedIn`,+ async (_, { dispatch }) => {+ await dispatch(getUserData());++ // We want to be careful about how we call loadAllFiles. It will+ // turn the files state from null into an array, indicating that+ // we've loaded the files at least once. This first attempt at+ // loading files is important - the manager uses files to decide+ // if it should create a new file automatically (if there are no+ // files). If we call it before we have a properly logged in+ // user, later on it could trigger the codepath which+ // automatically creates a file, but we really haven't loaded+ // their files yet.+ //+ // Since the above comment was written, We let in users even if+ // their account is invalid. So we should list all their files+ // regardless. Previously, we kicked users out to a "need payment+ // info" screen and we didn't want to call this.+ dispatch(loadAllFiles());+ },+);++export const signOut = createAppAsyncThunk(+ `${sliceName}/signOut`,+ async (_, { dispatch }) => {+ await send('subscribe-sign-out');++ dispatch(getUserData());+ dispatch(loadGlobalPrefs());+ dispatch(closeBudget());+ // Handled in budgetSlice+ // dispatch({ type: constants.SIGN_OUT });+ },+);++type UsersState = {+ data: Awaited<ReturnType<Handlers['subscribe-get-user']>>;+};++const initialState: UsersState = {+ data: null,+};++type GetUserDataPayload = {+ data: UsersState['data'];+};++const usersSlice = createSlice({+ name: sliceName,+ initialState,+ reducers: {+ loadUserData(state, action: PayloadAction<GetUserDataPayload>) {+ state.data = action.payload.data;+ },+ },+ extraReducers: builder => {+ builder.addCase(resetApp, state => state || initialState);+ },+});++export const { name, reducer, getInitialState } = usersSlice;++export const actions = {+ ...usersSlice.actions,+ getUserData,+ loggedIn,+ signOut,+};++export const { loadUserData } = actions;diff --git a/upcoming-release-notes/4128.md b/upcoming-release-notes/4128.mdnew file mode 100644index 0000000000..c63fd79f96--- /dev/null+++ b/upcoming-release-notes/4128.md@@ -0,0 +1,6 @@+---+category: Maintenance+authors: [joel-jeremy]+---++Phase 2 - Redux Toolkit Migration - users slice