Skip to content

A local-first personal finance app

[Redux] Move account states from queriesSlice to accountsSlice (#5581)

Joel Jeremy Marquez165be3d0df20
diff --git a/packages/desktop-client/src/accounts/accountsSlice.ts b/packages/desktop-client/src/accounts/accountsSlice.ts
index ceed6a6f26..564ee754f6 100644
--- a/packages/desktop-client/src/accounts/accountsSlice.ts
+++ b/packages/desktop-client/src/accounts/accountsSlice.ts
@@ -1,19 +1,21 @@
import { createSlice, type PayloadAction } from '@reduxjs/toolkit';
+import memoizeOne from 'memoize-one';
import { send } from 'loot-core/platform/client/fetch';
import { type SyncResponseWithErrors } from 'loot-core/server/accounts/app';
+import { groupById } from 'loot-core/shared/util';
import {
type SyncServerGoCardlessAccount,
type AccountEntity,
type TransactionEntity,
type SyncServerSimpleFinAccount,
type SyncServerPluggyAiAccount,
+ type CategoryEntity,
} from 'loot-core/types/models';
import { resetApp } from '@desktop-client/app/appSlice';
import { addNotification } from '@desktop-client/notifications/notificationsSlice';
import {
- markAccountsDirty,
markPayeesDirty,
setNewTransactions,
} from '@desktop-client/queries/queriesSlice';
@@ -27,11 +29,21 @@ type AccountState = {
[key: AccountEntity['id']]: { type: string; code: string };
};
accountsSyncing: Array<AccountEntity['id']>;
+ updatedAccounts: Array<AccountEntity['id']>;
+ accounts: AccountEntity[];
+ isAccountsLoading: boolean;
+ isAccountsLoaded: boolean;
+ isAccountsDirty: boolean;
};
const initialState: AccountState = {
failedAccounts: {},
accountsSyncing: [],
+ updatedAccounts: [],
+ accounts: [],
+ isAccountsLoading: false,
+ isAccountsLoaded: false,
+ isAccountsDirty: false,
};
type SetAccountsSyncingPayload = {
@@ -48,6 +60,14 @@ type MarkAccountSuccessPayload = {
id: AccountEntity['id'];
};
+type MarkUpdatedAccountsPayload = {
+ ids: AccountState['updatedAccounts'];
+};
+
+type MarkAccountReadPayload = {
+ id: AccountEntity['id'];
+};
+
const accountsSlice = createSlice({
name: sliceName,
initialState,
@@ -70,11 +90,149 @@ const accountsSlice = createSlice({
) {
delete state.failedAccounts[action.payload.id];
},
+ markUpdatedAccounts(
+ state,
+ action: PayloadAction<MarkUpdatedAccountsPayload>,
+ ) {
+ state.updatedAccounts = action.payload.ids
+ ? [...state.updatedAccounts, ...action.payload.ids]
+ : state.updatedAccounts;
+ },
+ markAccountRead(state, action: PayloadAction<MarkAccountReadPayload>) {
+ state.updatedAccounts = state.updatedAccounts.filter(
+ id => id !== action.payload.id,
+ );
+ },
+ markAccountsDirty(state) {
+ _markAccountsDirty(state);
+ },
},
extraReducers: builder => {
builder.addCase(resetApp, () => initialState);
+
+ builder.addCase(createAccount.fulfilled, _markAccountsDirty);
+ builder.addCase(updateAccount.fulfilled, _markAccountsDirty);
+ builder.addCase(closeAccount.fulfilled, _markAccountsDirty);
+ builder.addCase(reopenAccount.fulfilled, _markAccountsDirty);
+
+ builder.addCase(reloadAccounts.fulfilled, (state, action) => {
+ _loadAccounts(state, action.payload);
+ });
+
+ builder.addCase(reloadAccounts.rejected, state => {
+ state.isAccountsLoading = false;
+ });
+
+ builder.addCase(reloadAccounts.pending, state => {
+ state.isAccountsLoading = true;
+ });
+
+ builder.addCase(getAccounts.fulfilled, (state, action) => {
+ _loadAccounts(state, action.payload);
+ });
+
+ builder.addCase(getAccounts.rejected, state => {
+ state.isAccountsLoading = false;
+ });
+
+ builder.addCase(getAccounts.pending, state => {
+ state.isAccountsLoading = true;
+ });
},
});
+type CreateAccountPayload = {
+ name: string;
+ balance: number;
+ offBudget: boolean;
+};
+
+export const createAccount = createAppAsyncThunk(
+ `${sliceName}/createAccount`,
+ async ({ name, balance, offBudget }: CreateAccountPayload) => {
+ const id = await send('account-create', {
+ name,
+ balance,
+ offBudget,
+ });
+ return id;
+ },
+);
+
+type CloseAccountPayload = {
+ id: AccountEntity['id'];
+ transferAccountId?: AccountEntity['id'];
+ categoryId?: CategoryEntity['id'];
+ forced?: boolean;
+};
+
+export const closeAccount = createAppAsyncThunk(
+ `${sliceName}/closeAccount`,
+ async ({
+ id,
+ transferAccountId,
+ categoryId,
+ forced,
+ }: CloseAccountPayload) => {
+ await send('account-close', {
+ id,
+ transferAccountId: transferAccountId || undefined,
+ categoryId: categoryId || undefined,
+ forced,
+ });
+ },
+);
+
+type ReopenAccountPayload = {
+ id: AccountEntity['id'];
+};
+
+export const reopenAccount = createAppAsyncThunk(
+ `${sliceName}/reopenAccount`,
+ async ({ id }: ReopenAccountPayload) => {
+ await send('account-reopen', { id });
+ },
+);
+
+type UpdateAccountPayload = {
+ account: AccountEntity;
+};
+
+export const updateAccount = createAppAsyncThunk(
+ `${sliceName}/updateAccount`,
+ async ({ account }: UpdateAccountPayload) => {
+ await send('account-update', account);
+ return account;
+ },
+);
+
+export const getAccounts = createAppAsyncThunk(
+ `${sliceName}/getAccounts`,
+ async () => {
+ // TODO: Force cast to AccountEntity.
+ // Server is currently returning the DB model it should return the entity model instead.
+ const accounts = (await send('accounts-get')) as unknown as AccountEntity[];
+ return accounts;
+ },
+ {
+ condition: (_, { getState }) => {
+ const { account } = getState();
+ return (
+ !account.isAccountsLoading &&
+ (account.isAccountsDirty || !account.isAccountsLoaded)
+ );
+ },
+ },
+);
+
+export const reloadAccounts = createAppAsyncThunk(
+ `${sliceName}/reloadAccounts`,
+ async () => {
+ // TODO: Force cast to AccountEntity.
+ // Server is currently returning the DB model it should return the entity model instead.
+ const accounts = (await send('accounts-get')) as unknown as AccountEntity[];
+ return accounts;
+ },
+);
type UnlinkAccountPayload = {
id: AccountEntity['id'];
@@ -83,10 +241,9 @@ type UnlinkAccountPayload = {
export const unlinkAccount = createAppAsyncThunk(
`${sliceName}/unlinkAccount`,
async ({ id }: UnlinkAccountPayload, { dispatch }) => {
- const { markAccountSuccess } = accountsSlice.actions;
await send('account-unlink', { id });
- dispatch(markAccountSuccess({ id }));
- dispatch(markAccountsDirty());
+ dispatch(actions.markAccountSuccess({ id }));
+ dispatch(actions.markAccountsDirty());
},
);
@@ -241,11 +398,11 @@ export const syncAccounts = createAppAsyncThunk(
return false;
}
- const queriesState = getState().queries;
+ const { accounts } = getState().account;
let accountIdsToSync: string[];
if (id === 'offbudget' || id === 'onbudget') {
const targetOffbudget = id === 'offbudget' ? 1 : 0;
- accountIdsToSync = queriesState.accounts
+ accountIdsToSync = accounts
.filter(
({ bank, closed, tombstone, offbudget }) =>
!!bank && !closed && !tombstone && offbudget === targetOffbudget,
@@ -256,7 +413,7 @@ export const syncAccounts = createAppAsyncThunk(
accountIdsToSync = [id];
} else {
// Default: all accounts
- accountIdsToSync = queriesState.accounts
+ accountIdsToSync = accounts
.filter(
({ bank, closed, tombstone }) => !!bank && !closed && !tombstone,
)
@@ -339,10 +496,11 @@ export const syncAccounts = createAppAsyncThunk(
setNewTransactions({
newTransactions,
matchedTransactions,
- updatedAccounts,
}),
);
+ dispatch(markUpdatedAccounts({ ids: updatedAccounts }));
+
// Reset the sync state back to empty (fallback in case something breaks
// in the logic above)
dispatch(setAccountsSyncing({ ids: [] }));
@@ -364,9 +522,19 @@ export const moveAccount = createAppAsyncThunk(
},
);
+export const getAccountsById = memoizeOne(
+ (accounts: AccountEntity[] | null | undefined) => groupById(accounts),
+);
+
export const { name, reducer, getInitialState } = accountsSlice;
export const actions = {
...accountsSlice.actions,
+ createAccount,
+ updateAccount,
+ getAccounts,
+ reloadAccounts,
+ closeAccount,
+ reopenAccount,
linkAccount,
linkAccountSimpleFin,
linkAccountPluggyAi,
@@ -374,3 +542,26 @@ export const actions = {
unlinkAccount,
syncAccounts,
};
+
+export const {
+ markAccountRead,
+ markAccountFailed,
+ markAccountSuccess,
+ markAccountsDirty,
+ markUpdatedAccounts,
+ setAccountsSyncing,
+} = accountsSlice.actions;
+
+function _loadAccounts(
+ state: AccountState,
+ accounts: AccountState['accounts'],
+) {
+ state.accounts = accounts;
+ state.isAccountsLoading = false;
+ state.isAccountsLoaded = true;
+ state.isAccountsDirty = false;
+}
+
+function _markAccountsDirty(state: AccountState) {
+ state.isAccountsDirty = true;
+}
diff --git a/packages/desktop-client/src/components/FinancesApp.tsx b/packages/desktop-client/src/components/FinancesApp.tsx
index d0c505526e..016d1322e2 100644
--- a/packages/desktop-client/src/components/FinancesApp.tsx
+++ b/packages/desktop-client/src/components/FinancesApp.tsx
@@ -86,7 +86,7 @@ export function FinancesApp() {
const { t } = useTranslation();
const accounts = useAccounts();
- const isAccountsLoaded = useSelector(state => state.queries.isAccountsLoaded);
+ const isAccountsLoaded = useSelector(state => state.account.isAccountsLoaded);
const [lastUsedVersion, setLastUsedVersion] = useLocalPref(
'flags.updateNotificationShownForVersion',
diff --git a/packages/desktop-client/src/components/accounts/Account.tsx b/packages/desktop-client/src/components/accounts/Account.tsx
index 705698b8ff..bb1cc751b8 100644
--- a/packages/desktop-client/src/components/accounts/Account.tsx
+++ b/packages/desktop-client/src/components/accounts/Account.tsx
@@ -42,7 +42,12 @@ import {
import { AccountEmptyMessage } from './AccountEmptyMessage';
import { AccountHeader } from './Header';
-import { unlinkAccount } from '@desktop-client/accounts/accountsSlice';
+import {
+ unlinkAccount,
+ reopenAccount,
+ updateAccount,
+ markAccountRead,
+} from '@desktop-client/accounts/accountsSlice';
import { syncAndDownload } from '@desktop-client/app/appSlice';
import { type SavedFilter } from '@desktop-client/components/filters/SavedFilterMenuButton';
import { TransactionList } from '@desktop-client/components/transactions/TransactionList';
@@ -82,9 +87,6 @@ import {
import {
createPayee,
getPayees,
- markAccountRead,
- reopenAccount,
- updateAccount,
updateNewTransactions,
} from '@desktop-client/queries/queriesSlice';
import { useSelector, useDispatch } from '@desktop-client/redux';
diff --git a/packages/desktop-client/src/components/mobile/accounts/AccountPage.tsx b/packages/desktop-client/src/components/mobile/accounts/AccountPage.tsx
index d55d248da1..798350fd91 100644
--- a/packages/desktop-client/src/components/mobile/accounts/AccountPage.tsx
+++ b/packages/desktop-client/src/components/mobile/accounts/AccountPage.tsx
@@ -16,6 +16,10 @@ import { AllAccountTransactions } from './AllAccountTransactions';
import { OffBudgetAccountTransactions } from './OffBudgetAccountTransactions';
import { OnBudgetAccountTransactions } from './OnBudgetAccountTransactions';
+import {
+ reopenAccount,
+ updateAccount,
+} from '@desktop-client/accounts/accountsSlice';
import { MobileBackButton } from '@desktop-client/components/mobile/MobileBackButton';
import { AddTransactionButton } from '@desktop-client/components/mobile/transactions/AddTransactionButton';
import { MobilePageHeader, Page } from '@desktop-client/components/Page';
@@ -26,10 +30,6 @@ import {
openAccountCloseModal,
pushModal,
} from '@desktop-client/modals/modalsSlice';
-import {
- reopenAccount,
- updateAccount,
-} from '@desktop-client/queries/queriesSlice';
import { useDispatch, useSelector } from '@desktop-client/redux';
export function AccountPage() {
diff --git a/packages/desktop-client/src/components/mobile/accounts/AccountTransactions.tsx b/packages/desktop-client/src/components/mobile/accounts/AccountTransactions.tsx
index 36d7e5796a..97357d7c01 100644
--- a/packages/desktop-client/src/components/mobile/accounts/AccountTransactions.tsx
+++ b/packages/desktop-client/src/components/mobile/accounts/AccountTransactions.tsx
@@ -9,6 +9,7 @@ import {
type TransactionEntity,
} from 'loot-core/types/models';
+import { markAccountRead } from '@desktop-client/accounts/accountsSlice';
import { syncAndDownload } from '@desktop-client/app/appSlice';
import { TransactionListWithBalances } from '@desktop-client/components/mobile/transactions/TransactionListWithBalances';
import { useAccountPreviewTransactions } from '@desktop-client/hooks/useAccountPreviewTransactions';
@@ -20,7 +21,6 @@ import { useTransactions } from '@desktop-client/hooks/useTransactions';
import { useTransactionsSearch } from '@desktop-client/hooks/useTransactionsSearch';
import { collapseModals, pushModal } from '@desktop-client/modals/modalsSlice';
import * as queries from '@desktop-client/queries';
-import { markAccountRead } from '@desktop-client/queries/queriesSlice';
import { useDispatch } from '@desktop-client/redux';
import * as bindings from '@desktop-client/spreadsheet/bindings';
diff --git a/packages/desktop-client/src/components/mobile/accounts/AccountsPage.tsx b/packages/desktop-client/src/components/mobile/accounts/AccountsPage.tsx
index 1b3fd1216d..916d622882 100644
--- a/packages/desktop-client/src/components/mobile/accounts/AccountsPage.tsx
+++ b/packages/desktop-client/src/components/mobile/accounts/AccountsPage.tsx
@@ -403,7 +403,7 @@ const AccountList = forwardRef<HTMLDivElement, AccountListProps>(
const syncingAccountIds = useSelector(
state => state.account.accountsSyncing,
);
- const updatedAccounts = useSelector(state => state.queries.updatedAccounts);
+ const updatedAccounts = useSelector(state => state.account.updatedAccounts);
const dispatch = useDispatch();
const { dragAndDropHooks } = useDragAndDrop({
diff --git a/packages/desktop-client/src/components/modals/CloseAccountModal.tsx b/packages/desktop-client/src/components/modals/CloseAccountModal.tsx
index 54922e7386..f1da73b1da 100644
--- a/packages/desktop-client/src/components/modals/CloseAccountModal.tsx
+++ b/packages/desktop-client/src/components/modals/CloseAccountModal.tsx
@@ -16,6 +16,7 @@ import { integerToCurrency } from 'loot-core/shared/util';
import { type AccountEntity } from 'loot-core/types/models';
import { type TransObjectLiteral } from 'loot-core/types/util';
+import { closeAccount } from '@desktop-client/accounts/accountsSlice';
import { AccountAutocomplete } from '@desktop-client/components/autocomplete/AccountAutocomplete';
import { CategoryAutocomplete } from '@desktop-client/components/autocomplete/CategoryAutocomplete';
import { Link } from '@desktop-client/components/common/Link';
@@ -30,7 +31,6 @@ import {
type Modal as ModalType,
pushModal,
} from '@desktop-client/modals/modalsSlice';
-import { closeAccount } from '@desktop-client/queries/queriesSlice';
import { useDispatch } from '@desktop-client/redux';
function needsCategory(
diff --git a/packages/desktop-client/src/components/modals/CreateLocalAccountModal.tsx b/packages/desktop-client/src/components/modals/CreateLocalAccountModal.tsx
index 01bdb587c8..f310d84079 100644
--- a/packages/desktop-client/src/components/modals/CreateLocalAccountModal.tsx
+++ b/packages/desktop-client/src/components/modals/CreateLocalAccountModal.tsx
@@ -14,6 +14,7 @@ import { View } from '@actual-app/components/view';
import { toRelaxedNumber } from 'loot-core/shared/util';
+import { createAccount } from '@desktop-client/accounts/accountsSlice';
import { Link } from '@desktop-client/components/common/Link';
import {
Modal,
@@ -27,7 +28,6 @@ import { validateAccountName } from '@desktop-client/components/util/accountVali
import * as useAccounts from '@desktop-client/hooks/useAccounts';
import { useNavigate } from '@desktop-client/hooks/useNavigate';
import { closeModal } from '@desktop-client/modals/modalsSlice';
-import { createAccount } from '@desktop-client/queries/queriesSlice';
import { useDispatch } from '@desktop-client/redux';
export function CreateLocalAccountModal() {
diff --git a/packages/desktop-client/src/components/sidebar/Account.tsx b/packages/desktop-client/src/components/sidebar/Account.tsx
index 152f215c0a..d5a12b953e 100644
--- a/packages/desktop-client/src/components/sidebar/Account.tsx
+++ b/packages/desktop-client/src/components/sidebar/Account.tsx
@@ -25,6 +25,10 @@ import { type AccountEntity } from 'loot-core/types/models';
import { BalanceHistoryGraph } from './BalanceHistoryGraph';
+import {
+ reopenAccount,
+ updateAccount,
+} from '@desktop-client/accounts/accountsSlice';
import { Link } from '@desktop-client/components/common/Link';
import { Notes } from '@desktop-client/components/Notes';
import {
@@ -40,10 +44,6 @@ import { useDragRef } from '@desktop-client/hooks/useDragRef';
import { useNotes } from '@desktop-client/hooks/useNotes';
import { useSyncedPref } from '@desktop-client/hooks/useSyncedPref';
import { openAccountCloseModal } from '@desktop-client/modals/modalsSlice';
-import {
- reopenAccount,
- updateAccount,
-} from '@desktop-client/queries/queriesSlice';
import { useDispatch } from '@desktop-client/redux';
import { type SheetFields, type Binding } from '@desktop-client/spreadsheet';
diff --git a/packages/desktop-client/src/components/transactions/TransactionsTable.tsx b/packages/desktop-client/src/components/transactions/TransactionsTable.tsx
index 105ac7520b..6448bbc13e 100644
--- a/packages/desktop-client/src/components/transactions/TransactionsTable.tsx
+++ b/packages/desktop-client/src/components/transactions/TransactionsTable.tsx
@@ -82,6 +82,7 @@ import {
} from './table/utils';
import { TransactionMenu } from './TransactionMenu';
+import { getAccountsById } from '@desktop-client/accounts/accountsSlice';
import { AccountAutocomplete } from '@desktop-client/components/autocomplete/AccountAutocomplete';
import { CategoryAutocomplete } from '@desktop-client/components/autocomplete/CategoryAutocomplete';
import { PayeeAutocomplete } from '@desktop-client/components/autocomplete/PayeeAutocomplete';
@@ -126,7 +127,6 @@ import { pushModal } from '@desktop-client/modals/modalsSlice';
import { NotesTagFormatter } from '@desktop-client/notes/NotesTagFormatter';
import { addNotification } from '@desktop-client/notifications/notificationsSlice';
import {
- getAccountsById,
getPayeesById,
getCategoriesById,
} from '@desktop-client/queries/queriesSlice';
diff --git a/packages/desktop-client/src/global-events.ts b/packages/desktop-client/src/global-events.ts
index e2b27c96e6..cf119dd88f 100644
--- a/packages/desktop-client/src/global-events.ts
+++ b/packages/desktop-client/src/global-events.ts
@@ -2,6 +2,7 @@
import { listen } from 'loot-core/platform/client/fetch';
import * as undo from 'loot-core/platform/client/undo';
+import { reloadAccounts } from './accounts/accountsSlice';
import { setAppState } from './app/appSlice';
import { closeBudgetUI } from './budgets/budgetsSlice';
import { closeModal, pushModal, replaceModal } from './modals/modalsSlice';
@@ -10,11 +11,7 @@ import {
addNotification,
} from './notifications/notificationsSlice';
import { loadPrefs } from './prefs/prefsSlice';
-import {
- reloadAccounts,
- reloadCategories,
- reloadPayees,
-} from './queries/queriesSlice';
+import { reloadCategories, reloadPayees } from './queries/queriesSlice';
import { type AppStore } from './redux/store';
import * as syncEvents from './sync-events';
diff --git a/packages/desktop-client/src/hooks/useAccounts.ts b/packages/desktop-client/src/hooks/useAccounts.ts
index 3af2a17a72..f528f7b937 100644
--- a/packages/desktop-client/src/hooks/useAccounts.ts
+++ b/packages/desktop-client/src/hooks/useAccounts.ts
@@ -2,13 +2,13 @@ import { useEffect } from 'react';
import { useInitialMount } from './useInitialMount';
-import { getAccounts } from '@desktop-client/queries/queriesSlice';
+import { getAccounts } from '@desktop-client/accounts/accountsSlice';
import { useSelector, useDispatch } from '@desktop-client/redux';
export function useAccounts() {
const dispatch = useDispatch();
const isInitialMount = useInitialMount();
- const isAccountsDirty = useSelector(state => state.queries.isAccountsDirty);
+ const isAccountsDirty = useSelector(state => state.account.isAccountsDirty);
useEffect(() => {
if (isInitialMount || isAccountsDirty) {
@@ -16,5 +16,5 @@ export function useAccounts() {
}
}, [dispatch, isInitialMount, isAccountsDirty]);
- return useSelector(state => state.queries.accounts);
+ return useSelector(state => state.account.accounts);
}
diff --git a/packages/desktop-client/src/hooks/useUpdatedAccounts.ts b/packages/desktop-client/src/hooks/useUpdatedAccounts.ts
index 88e00fe972..b0e29dcb69 100644
--- a/packages/desktop-client/src/hooks/useUpdatedAccounts.ts
+++ b/packages/desktop-client/src/hooks/useUpdatedAccounts.ts
@@ -1,5 +1,5 @@
import { useSelector } from '@desktop-client/redux';
export function useUpdatedAccounts() {
- return useSelector(state => state.queries.updatedAccounts);
+ return useSelector(state => state.account.updatedAccounts);
}
diff --git a/packages/desktop-client/src/modals/modalsSlice.ts b/packages/desktop-client/src/modals/modalsSlice.ts
index 82d0bfa5c9..4d6dfc82f4 100644
--- a/packages/desktop-client/src/modals/modalsSlice.ts
+++ b/packages/desktop-client/src/modals/modalsSlice.ts
@@ -566,7 +566,7 @@ export const openAccountCloseModal = createAppAsyncThunk(
id: accountId,
},
);
- const account = getState().queries.accounts.find(
+ const account = getState().account.accounts.find(
acct => acct.id === accountId,
);
diff --git a/packages/desktop-client/src/queries/queriesSlice.ts b/packages/desktop-client/src/queries/queriesSlice.ts
index 344321e685..e94badc742 100644
--- a/packages/desktop-client/src/queries/queriesSlice.ts
+++ b/packages/desktop-client/src/queries/queriesSlice.ts
@@ -14,6 +14,10 @@ import {
type Tag,
} from 'loot-core/types/models';
+import {
+ getAccountsById,
+ markUpdatedAccounts,
+} from '@desktop-client/accounts/accountsSlice';
import { resetApp } from '@desktop-client/app/appSlice';
import {
addGenericErrorNotification,
@@ -32,11 +36,6 @@ type QueriesState = {
newTransactions: Array<TransactionEntity['id']>;
matchedTransactions: Array<TransactionEntity['id']>;
lastTransaction: TransactionEntity | null;
- updatedAccounts: Array<AccountEntity['id']>;
- accounts: AccountEntity[];
- isAccountsLoading: boolean;
- isAccountsLoaded: boolean;
- isAccountsDirty: boolean;
categories: CategoryViews;
isCategoriesLoading: boolean;
isCategoriesLoaded: boolean;
@@ -59,11 +58,6 @@ const initialState: QueriesState = {
newTransactions: [],
matchedTransactions: [],
lastTransaction: null,
- updatedAccounts: [],
- accounts: [],
- isAccountsLoading: false,
- isAccountsLoaded: false,
- isAccountsDirty: false,
categories: {
grouped: [],
list: [],
@@ -86,9 +80,8 @@ const initialState: QueriesState = {
};
type SetNewTransactionsPayload = {
- newTransactions: QueriesState['newTransactions'];
- matchedTransactions: QueriesState['matchedTransactions'];
- updatedAccounts: QueriesState['updatedAccounts'];
+ newTransactions: Array<TransactionEntity['id']>;
+ matchedTransactions: Array<TransactionEntity['id']>;
};
type UpdateNewTransactionsPayload = {
@@ -99,10 +92,6 @@ type SetLastTransactionPayload = {
transaction: TransactionEntity;
};
-type MarkAccountReadPayload = {
- id: AccountEntity['id'];
-};
-
const queriesSlice = createSlice({
name: sliceName,
initialState,
@@ -118,10 +107,6 @@ const queriesSlice = createSlice({
state.matchedTransactions = action.payload.matchedTransactions
? [...state.matchedTransactions, ...action.payload.matchedTransactions]
: state.matchedTransactions;
-
- state.updatedAccounts = action.payload.updatedAccounts
- ? [...state.updatedAccounts, ...action.payload.updatedAccounts]
- : state.updatedAccounts;
},
updateNewTransactions(
state,
@@ -140,14 +125,6 @@ const queriesSlice = createSlice({
) {
state.lastTransaction = action.payload.transaction;
},
- markAccountRead(state, action: PayloadAction<MarkAccountReadPayload>) {
- state.updatedAccounts = state.updatedAccounts.filter(
- id => id !== action.payload.id,
- );
- },
- markAccountsDirty(state) {
- _markAccountsDirty(state);
- },
markCategoriesDirty(state) {
_markCategoriesDirty(state);
},
@@ -159,37 +136,6 @@ const queriesSlice = createSlice({
},
},
extraReducers: builder => {
- // Accounts
-
- builder.addCase(createAccount.fulfilled, _markAccountsDirty);
- builder.addCase(updateAccount.fulfilled, _markAccountsDirty);
- builder.addCase(closeAccount.fulfilled, _markAccountsDirty);
- builder.addCase(reopenAccount.fulfilled, _markAccountsDirty);
-
- builder.addCase(reloadAccounts.fulfilled, (state, action) => {
- _loadAccounts(state, action.payload);
- });
-
- builder.addCase(reloadAccounts.rejected, state => {
- state.isAccountsLoading = false;
- });
-
- builder.addCase(reloadAccounts.pending, state => {
- state.isAccountsLoading = true;
- });
-
- builder.addCase(getAccounts.fulfilled, (state, action) => {
- _loadAccounts(state, action.payload);
- });
-
- builder.addCase(getAccounts.rejected, state => {
- state.isAccountsLoading = false;
- });
-
- builder.addCase(getAccounts.pending, state => {
- state.isAccountsLoading = true;
- });
-
// Categories
builder.addCase(createCategoryGroup.fulfilled, _markCategoriesDirty);
@@ -326,102 +272,6 @@ const queriesSlice = createSlice({
},
});
-// Account actions
-
-type CreateAccountPayload = {
- name: AccountEntity['name'];
- balance: AccountEntity['balance_current'];
- offBudget: boolean;
-};
-
-export const createAccount = createAppAsyncThunk(
- `${sliceName}/createAccount`,
- async ({ name, balance, offBudget }: CreateAccountPayload) => {
- const id: AccountEntity['id'] = await send('account-create', {
- name,
- balance,
- offBudget,
- });
- return id;
- },
-);
-
-type CloseAccountPayload = {
- id: AccountEntity['id'];
- transferAccountId?: AccountEntity['id'];
- categoryId?: CategoryEntity['id'];
- forced?: boolean;
-};
-
-export const closeAccount = createAppAsyncThunk(
- `${sliceName}/closeAccount`,
- async ({
- id,
- transferAccountId,
- categoryId,
- forced,
- }: CloseAccountPayload) => {
- await send('account-close', {
- id,
- transferAccountId: transferAccountId || null,
- categoryId: categoryId || null,
- forced,
- });
- },
-);
-
-type ReopenAccountPayload = {
- id: AccountEntity['id'];
-};
-
-export const reopenAccount = createAppAsyncThunk(
- `${sliceName}/reopenAccount`,
- async ({ id }: ReopenAccountPayload) => {
- await send('account-reopen', { id });
- },
-);
-
-type UpdateAccountPayload = {
- account: AccountEntity;
-};
-
-export const updateAccount = createAppAsyncThunk(
- `${sliceName}/updateAccount`,
- async ({ account }: UpdateAccountPayload) => {
- await send('account-update', account);
- return account;
- },
-);
-
-export const getAccounts = createAppAsyncThunk(
- `${sliceName}/getAccounts`,
- async () => {
- // TODO: Force cast to AccountEntity.
- // Server is currently returning the DB model it should return the entity model instead.
- const accounts = (await send('accounts-get')) as AccountEntity[];
- return accounts;
- },
- {
- condition: (_, { getState }) => {
- const { queries } = getState();
- return (
- !queries.isAccountsLoading &&
- (queries.isAccountsDirty || !queries.isAccountsLoaded)
- );
- },
- },
-);
-
-export const reloadAccounts = createAppAsyncThunk(
- `${sliceName}/reloadAccounts`,
- async () => {
- // TODO: Force cast to AccountEntity.
- // Server is currently returning the DB model it should return the entity model instead.
- const accounts = (await send('accounts-get')) as AccountEntity[];
- return accounts;
- },
-);
-
// Category actions
type CreateCategoryGroupPayload = {
@@ -1101,7 +951,12 @@ export const importTransactions = createAppAsyncThunk(
setNewTransactions({
newTransactions: added,
matchedTransactions: updated,
- updatedAccounts: added.length > 0 ? [accountId] : [],
+ }),
+ );
+
+ dispatch(
+ markUpdatedAccounts({
+ ids: added.length > 0 ? [accountId] : [],
}),
);
@@ -1125,9 +980,6 @@ export const getActivePayees = memoizeOne(
},
);
-export const getAccountsById = memoizeOne(
- (accounts: AccountEntity[] | null | undefined) => groupById(accounts),
-);
export const getPayeesById = memoizeOne(
(payees: PayeeEntity[] | null | undefined) => groupById(payees),
);
@@ -1148,19 +1000,16 @@ export const getCategoriesById = memoizeOne(
export const { name, reducer, getInitialState } = queriesSlice;
export const actions = {
...queriesSlice.actions,
- updateAccount,
- getAccounts,
- reloadAccounts,
- closeAccount,
- reopenAccount,
- getCategories,
createPayee,
getCommonPayees,
+ reloadCommonPayees,
getPayees,
+ reloadPayees,
importPreviewTransactions,
importTransactions,
applyBudgetAction,
- createAccount,
+ getCategories,
+ reloadCategories,
createCategoryGroup,
updateCategoryGroup,
deleteCategoryGroup,
@@ -1170,32 +1019,21 @@ export const actions = {
moveCategory,
moveCategoryGroup,
getTags,
+ createTag,
+ updateTag,
+ deleteTag,
+ deleteAllTags,
+ findTags,
};
export const {
- markAccountRead,
- markAccountsDirty,
+ setNewTransactions,
+ updateNewTransactions,
+ setLastTransaction,
markCategoriesDirty,
markPayeesDirty,
markTagsDirty,
- setLastTransaction,
- updateNewTransactions,
- setNewTransactions,
-} = actions;
-
-function _loadAccounts(
- state: QueriesState,
- accounts: QueriesState['accounts'],
-) {
- state.accounts = accounts;
- state.isAccountsLoading = false;
- state.isAccountsLoaded = true;
- state.isAccountsDirty = false;
-}
-
-function _markAccountsDirty(state: QueriesState) {
- state.isAccountsDirty = true;
-}
+} = queriesSlice.actions;
function _loadCategories(
state: QueriesState,
diff --git a/packages/desktop-client/src/sync-events.ts b/packages/desktop-client/src/sync-events.ts
index 841b974473..3fd1767d36 100644
--- a/packages/desktop-client/src/sync-events.ts
+++ b/packages/desktop-client/src/sync-events.ts
@@ -3,6 +3,7 @@ import { t } from 'i18next';
import { listen, send } from 'loot-core/platform/client/fetch';
+import { reloadAccounts } from './accounts/accountsSlice';
import { resetSync, sync } from './app/appSlice';
import { closeAndDownloadBudget, uploadBudget } from './budgets/budgetsSlice';
import { pushModal } from './modals/modalsSlice';
@@ -11,11 +12,7 @@ import {
type Notification,
} from './notifications/notificationsSlice';
import { loadPrefs } from './prefs/prefsSlice';
-import {
- reloadAccounts,
- reloadCategories,
- reloadPayees,
-} from './queries/queriesSlice';
+import { reloadCategories, reloadPayees } from './queries/queriesSlice';
import { type AppStore } from './redux/store';
import { signOut } from './users/usersSlice';
diff --git a/upcoming-release-notes/5581.md b/upcoming-release-notes/5581.md
new file mode 100644
index 0000000000..5a1cc0d444
--- /dev/null
+++ b/upcoming-release-notes/5581.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [joel-jeremy]
+---
+
+Move account states from queriesSlice to accountsSlice