Skip to content

A local-first personal finance app

[Redux Toolkit Migration] prefsSlice (#4127)

Joel Jeremy Marquez47c0d394eebd
diff --git a/packages/desktop-client/src/browser-preload.browser.js b/packages/desktop-client/src/browser-preload.browser.js
index 30a40dda60..500acd69e7 100644
--- a/packages/desktop-client/src/browser-preload.browser.js
+++ b/packages/desktop-client/src/browser-preload.browser.js
@@ -177,7 +177,7 @@ global.Actual = {
},
setTheme: theme => {
- window.__actionsForMenu.saveGlobalPrefs({ theme });
+ window.__actionsForMenu.saveGlobalPrefs({ prefs: { theme } });
},
moveBudgetDirectory: () => {},
diff --git a/packages/desktop-client/src/components/App.tsx b/packages/desktop-client/src/components/App.tsx
index 1640fd7a11..236c7736ea 100644
--- a/packages/desktop-client/src/components/App.tsx
+++ b/packages/desktop-client/src/components/App.tsx
@@ -14,11 +14,12 @@ import { BrowserRouter } from 'react-router-dom';
import { styles } from '@actual-app/components/styles';
import { View } from '@actual-app/components/view';
-import { loadGlobalPrefs, signOut } from 'loot-core/client/actions';
+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 { init as initConnection, send } from 'loot-core/platform/client/fetch';
diff --git a/packages/desktop-client/src/components/modals/CreateEncryptionKeyModal.tsx b/packages/desktop-client/src/components/modals/CreateEncryptionKeyModal.tsx
index 50ff8f76e7..c0dc4a4020 100644
--- a/packages/desktop-client/src/components/modals/CreateEncryptionKeyModal.tsx
+++ b/packages/desktop-client/src/components/modals/CreateEncryptionKeyModal.tsx
@@ -14,10 +14,10 @@ import { theme } from '@actual-app/components/theme';
import { View } from '@actual-app/components/view';
import { css } from '@emotion/css';
-import { loadGlobalPrefs } from 'loot-core/client/actions';
import { sync } from 'loot-core/client/app/appSlice';
import { loadAllFiles } from 'loot-core/client/budgets/budgetsSlice';
import { type Modal as ModalType } from 'loot-core/client/modals/modalsSlice';
+import { loadGlobalPrefs } from 'loot-core/client/prefs/prefsSlice';
import { send } from 'loot-core/platform/client/fetch';
import { getCreateKeyError } from 'loot-core/shared/errors';
diff --git a/packages/desktop-client/src/components/settings/index.tsx b/packages/desktop-client/src/components/settings/index.tsx
index 0192639db4..ada5a29c13 100644
--- a/packages/desktop-client/src/components/settings/index.tsx
+++ b/packages/desktop-client/src/components/settings/index.tsx
@@ -10,8 +10,8 @@ import { tokens } from '@actual-app/components/tokens';
import { View } from '@actual-app/components/view';
import { css } from '@emotion/css';
-import { loadPrefs } from 'loot-core/client/actions';
import { closeBudget } from 'loot-core/client/budgets/budgetsSlice';
+import { loadPrefs } from 'loot-core/client/prefs/prefsSlice';
import { listen } from 'loot-core/platform/client/fetch';
import { isElectron } from 'loot-core/shared/environment';
diff --git a/packages/desktop-client/src/global-events.ts b/packages/desktop-client/src/global-events.ts
index d1692ef3c0..3377835f3f 100644
--- a/packages/desktop-client/src/global-events.ts
+++ b/packages/desktop-client/src/global-events.ts
@@ -1,5 +1,4 @@
// @ts-strict-ignore
-import { loadPrefs } from 'loot-core/client/actions';
import { setAppState } from 'loot-core/client/app/appSlice';
import { closeBudgetUI } from 'loot-core/client/budgets/budgetsSlice';
import {
@@ -11,6 +10,7 @@ import {
addGenericErrorNotification,
addNotification,
} from 'loot-core/client/notifications/notificationsSlice';
+import { loadPrefs } from 'loot-core/client/prefs/prefsSlice';
import {
getAccounts,
getCategories,
diff --git a/packages/desktop-client/src/hooks/useGlobalPref.ts b/packages/desktop-client/src/hooks/useGlobalPref.ts
index 2484edf495..9801e15094 100644
--- a/packages/desktop-client/src/hooks/useGlobalPref.ts
+++ b/packages/desktop-client/src/hooks/useGlobalPref.ts
@@ -1,6 +1,6 @@
import { useCallback } from 'react';
-import { saveGlobalPrefs } from 'loot-core/client/actions';
+import { saveGlobalPrefs } from 'loot-core/client/prefs/prefsSlice';
import { type GlobalPrefs } from 'loot-core/types/prefs';
import { useSelector, useDispatch } from '../redux';
@@ -17,12 +17,12 @@ export function useGlobalPref<K extends keyof GlobalPrefs>(
const setGlobalPref = useCallback<SetGlobalPrefAction<K>>(
value => {
dispatch(
- saveGlobalPrefs(
- {
+ saveGlobalPrefs({
+ prefs: {
[prefName]: value,
- } as GlobalPrefs,
+ },
onSaveGlobalPrefs,
- ),
+ }),
);
},
[prefName, dispatch, onSaveGlobalPrefs],
diff --git a/packages/desktop-client/src/hooks/useMetadataPref.ts b/packages/desktop-client/src/hooks/useMetadataPref.ts
index 7a16605996..f9a57bd091 100644
--- a/packages/desktop-client/src/hooks/useMetadataPref.ts
+++ b/packages/desktop-client/src/hooks/useMetadataPref.ts
@@ -1,6 +1,6 @@
import { useCallback } from 'react';
-import { savePrefs } from 'loot-core/client/actions';
+import { savePrefs } from 'loot-core/client/prefs/prefsSlice';
import { type MetadataPrefs } from 'loot-core/types/prefs';
import { useSelector, useDispatch } from '../redux';
@@ -15,7 +15,7 @@ export function useMetadataPref<K extends keyof MetadataPrefs>(
const dispatch = useDispatch();
const setLocalPref = useCallback<SetMetadataPrefAction<K>>(
value => {
- dispatch(savePrefs({ [prefName]: value }));
+ dispatch(savePrefs({ prefs: { [prefName]: value } }));
},
[prefName, dispatch],
);
diff --git a/packages/desktop-client/src/hooks/useSyncedPref.ts b/packages/desktop-client/src/hooks/useSyncedPref.ts
index 685f53491c..758c3ebdee 100644
--- a/packages/desktop-client/src/hooks/useSyncedPref.ts
+++ b/packages/desktop-client/src/hooks/useSyncedPref.ts
@@ -1,6 +1,6 @@
import { useCallback } from 'react';
-import { saveSyncedPrefs } from 'loot-core/client/actions';
+import { saveSyncedPrefs } from 'loot-core/client/prefs/prefsSlice';
import { type SyncedPrefs } from 'loot-core/types/prefs';
import { useSelector, useDispatch } from '../redux';
@@ -15,7 +15,7 @@ export function useSyncedPref<K extends keyof SyncedPrefs>(
const dispatch = useDispatch();
const setPref = useCallback<SetSyncedPrefAction<K>>(
value => {
- dispatch(saveSyncedPrefs({ [prefName]: value }));
+ dispatch(saveSyncedPrefs({ prefs: { [prefName]: value } }));
},
[prefName, dispatch],
);
diff --git a/packages/desktop-client/src/hooks/useSyncedPrefs.ts b/packages/desktop-client/src/hooks/useSyncedPrefs.ts
index 3d55373c0b..fc5ff1b582 100644
--- a/packages/desktop-client/src/hooks/useSyncedPrefs.ts
+++ b/packages/desktop-client/src/hooks/useSyncedPrefs.ts
@@ -1,6 +1,6 @@
import { useCallback } from 'react';
-import { saveSyncedPrefs } from 'loot-core/client/actions';
+import { saveSyncedPrefs } from 'loot-core/client/prefs/prefsSlice';
import { type SyncedPrefs } from 'loot-core/types/prefs';
import { useSelector, useDispatch } from '../redux';
@@ -12,7 +12,7 @@ export function useSyncedPrefs(): [SyncedPrefs, SetSyncedPrefsAction] {
const dispatch = useDispatch();
const setPrefs = useCallback<SetSyncedPrefsAction>(
newValue => {
- dispatch(saveSyncedPrefs(newValue));
+ dispatch(saveSyncedPrefs({ prefs: newValue }));
},
[dispatch],
);
diff --git a/packages/desktop-client/src/index.tsx b/packages/desktop-client/src/index.tsx
index fdcbf5cff2..8b026d1c6c 100644
--- a/packages/desktop-client/src/index.tsx
+++ b/packages/desktop-client/src/index.tsx
@@ -19,6 +19,7 @@ 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';
import * as notificationsSlice from 'loot-core/client/notifications/notificationsSlice';
+import * as prefsSlice from 'loot-core/client/prefs/prefsSlice';
import * as queriesSlice from 'loot-core/client/queries/queriesSlice';
import { runQuery } from 'loot-core/client/query-helpers';
import { store } from 'loot-core/client/store';
@@ -42,6 +43,7 @@ const boundActions = bindActionCreators(
...budgetsSlice.actions,
...modalsSlice.actions,
...notificationsSlice.actions,
+ ...prefsSlice.actions,
...queriesSlice.actions,
},
store.dispatch,
diff --git a/packages/desktop-electron/index.ts b/packages/desktop-electron/index.ts
index 611855f354..efd037f731 100644
--- a/packages/desktop-electron/index.ts
+++ b/packages/desktop-electron/index.ts
@@ -529,7 +529,7 @@ ipcMain.on('set-theme', (_event, theme: string) => {
const obj = { theme };
if (clientWin) {
clientWin.webContents.executeJavaScript(
- `window.__actionsForMenu && window.__actionsForMenu.saveGlobalPrefs(${JSON.stringify(obj)})`,
+ `window.__actionsForMenu && window.__actionsForMenu.saveGlobalPrefs({ prefs: ${JSON.stringify(obj)} })`,
);
}
});
diff --git a/packages/loot-core/src/client/actions/index.ts b/packages/loot-core/src/client/actions/index.ts
index 25ef30eedf..e5abc85650 100644
--- a/packages/loot-core/src/client/actions/index.ts
+++ b/packages/loot-core/src/client/actions/index.ts
@@ -1,2 +1 @@
-export * from './prefs';
export * from './user';
diff --git a/packages/loot-core/src/client/actions/prefs.ts b/packages/loot-core/src/client/actions/prefs.ts
deleted file mode 100644
index 3744d037e8..0000000000
--- a/packages/loot-core/src/client/actions/prefs.ts
+++ /dev/null
@@ -1,104 +0,0 @@
-// eslint-disable-next-line no-restricted-imports -- fix me -- do not import @actual-app/web in loot-core
-import { setI18NextLanguage } from '@actual-app/web/src/i18n';
-
-import { send } from '../../platform/client/fetch';
-import { parseNumberFormat, setNumberFormat } from '../../shared/util';
-import {
- type GlobalPrefs,
- type MetadataPrefs,
- type SyncedPrefs,
-} from '../../types/prefs';
-import * as constants from '../constants';
-import { closeModal } from '../modals/modalsSlice';
-import { type AppDispatch, type GetRootState } from '../store';
-
-export function loadPrefs() {
- return async (dispatch: AppDispatch, getState: GetRootState) => {
- const prefs = await send('load-prefs');
-
- // Remove any modal state if switching between budgets
- const currentPrefs = getState().prefs.local;
- if (prefs && prefs.id && !currentPrefs) {
- dispatch(closeModal());
- }
-
- const [globalPrefs, syncedPrefs] = await Promise.all([
- send('load-global-prefs'),
- send('preferences/get'),
- ]);
-
- dispatch({
- type: constants.SET_PREFS,
- prefs,
- globalPrefs,
- syncedPrefs,
- });
-
- // Certain loot-core utils depend on state outside of the React tree, update them
- setNumberFormat(
- parseNumberFormat({
- format: syncedPrefs.numberFormat,
- hideFraction: syncedPrefs.hideFraction,
- }),
- );
-
- // We need to load translations before the app renders
- setI18NextLanguage(globalPrefs.language ?? '');
-
- return prefs;
- };
-}
-
-export function savePrefs(prefs: MetadataPrefs) {
- return async (dispatch: AppDispatch) => {
- await send('save-prefs', prefs);
- dispatch({
- type: constants.MERGE_LOCAL_PREFS,
- prefs,
- });
- };
-}
-
-export function loadGlobalPrefs() {
- return async (dispatch: AppDispatch, getState: GetRootState) => {
- const globalPrefs = await send('load-global-prefs');
- dispatch({
- type: constants.SET_PREFS,
- prefs: getState().prefs.local,
- globalPrefs,
- syncedPrefs: getState().prefs.synced,
- });
- return globalPrefs;
- };
-}
-
-export function saveGlobalPrefs(
- prefs: GlobalPrefs,
- onSaveGlobalPrefs?: () => void,
-) {
- return async (dispatch: AppDispatch) => {
- await send('save-global-prefs', prefs);
- dispatch({
- type: constants.MERGE_GLOBAL_PREFS,
- globalPrefs: prefs,
- });
- onSaveGlobalPrefs?.();
- };
-}
-
-export function saveSyncedPrefs(prefs: SyncedPrefs) {
- return async (dispatch: AppDispatch) => {
- await Promise.all(
- Object.entries(prefs).map(([prefName, value]) =>
- send('preferences/save', {
- id: prefName as keyof SyncedPrefs,
- value,
- }),
- ),
- );
- dispatch({
- type: constants.MERGE_SYNCED_PREFS,
- syncedPrefs: prefs,
- });
- };
-}
diff --git a/packages/loot-core/src/client/actions/user.ts b/packages/loot-core/src/client/actions/user.ts
index e368a145b5..d5974207da 100644
--- a/packages/loot-core/src/client/actions/user.ts
+++ b/packages/loot-core/src/client/actions/user.ts
@@ -1,10 +1,9 @@
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';
-import { loadGlobalPrefs } from './prefs';
-
export function getUserData() {
return async (dispatch: AppDispatch) => {
const data = await send('subscribe-get-user');
diff --git a/packages/loot-core/src/client/app/appSlice.ts b/packages/loot-core/src/client/app/appSlice.ts
index e93f0c2439..312ea8ba92 100644
--- a/packages/loot-core/src/client/app/appSlice.ts
+++ b/packages/loot-core/src/client/app/appSlice.ts
@@ -5,8 +5,8 @@ import { getUploadError } from '../../shared/errors';
import { type AccountEntity } from '../../types/models';
import { type AtLeastOne } from '../../types/util';
import { syncAccounts } from '../accounts/accountsSlice';
-import { loadPrefs } from '../actions';
import { pushModal } from '../modals/modalsSlice';
+import { loadPrefs } from '../prefs/prefsSlice';
import { createAppAsyncThunk } from '../redux';
const sliceName = 'app';
diff --git a/packages/loot-core/src/client/budgets/budgetsSlice.ts b/packages/loot-core/src/client/budgets/budgetsSlice.ts
index 73a734c0c5..59253ed754 100644
--- a/packages/loot-core/src/client/budgets/budgetsSlice.ts
+++ b/packages/loot-core/src/client/budgets/budgetsSlice.ts
@@ -7,10 +7,10 @@ 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 { loadGlobalPrefs, loadPrefs } from '../actions';
import { setAppState } from '../app/appSlice';
import * as constants from '../constants';
import { closeModal, pushModal } from '../modals/modalsSlice';
+import { loadGlobalPrefs, loadPrefs } from '../prefs/prefsSlice';
import { createAppAsyncThunk } from '../redux';
const sliceName = 'budgets';
diff --git a/packages/loot-core/src/client/prefs/prefsSlice.ts b/packages/loot-core/src/client/prefs/prefsSlice.ts
new file mode 100644
index 0000000000..0fca3c48f0
--- /dev/null
+++ b/packages/loot-core/src/client/prefs/prefsSlice.ts
@@ -0,0 +1,170 @@
+// eslint-disable-next-line no-restricted-imports -- fix me -- do not import @actual-app/web in loot-core
+import { setI18NextLanguage } from '@actual-app/web/src/i18n';
+import { createSlice, type PayloadAction } from '@reduxjs/toolkit';
+
+import { send } from '../../platform/client/fetch';
+import { parseNumberFormat, setNumberFormat } from '../../shared/util';
+import {
+ type GlobalPrefs,
+ type MetadataPrefs,
+ type SyncedPrefs,
+} from '../../types/prefs';
+import { closeModal } from '../modals/modalsSlice';
+import { createAppAsyncThunk } from '../redux';
+
+const sliceName = 'prefs';
+
+type PrefsState = {
+ local: MetadataPrefs;
+ global: GlobalPrefs;
+ synced: SyncedPrefs;
+};
+
+const initialState: PrefsState = {
+ local: {},
+ global: {},
+ synced: {},
+};
+
+export const loadPrefs = createAppAsyncThunk(
+ `${sliceName}/loadPrefs`,
+ async (_, { dispatch, getState }) => {
+ const prefs = await send('load-prefs');
+
+ // Remove any modal state if switching between budgets
+ const currentPrefs = getState().prefs.local;
+ if (prefs && prefs.id && !currentPrefs) {
+ dispatch(closeModal());
+ }
+
+ const [globalPrefs, syncedPrefs] = await Promise.all([
+ send('load-global-prefs'),
+ send('preferences/get'),
+ ]);
+
+ dispatch(
+ setPrefs({ local: prefs, global: globalPrefs, synced: syncedPrefs }),
+ );
+
+ // Certain loot-core utils depend on state outside of the React tree, update them
+ setNumberFormat(
+ parseNumberFormat({
+ format: syncedPrefs.numberFormat,
+ hideFraction: syncedPrefs.hideFraction,
+ }),
+ );
+
+ // We need to load translations before the app renders
+ setI18NextLanguage(globalPrefs.language ?? '');
+
+ return prefs;
+ },
+);
+
+type SavePrefsPayload = {
+ prefs: MetadataPrefs;
+};
+
+export const savePrefs = createAppAsyncThunk(
+ `${sliceName}/savePrefs`,
+ async ({ prefs }: SavePrefsPayload, { dispatch }) => {
+ await send('save-prefs', prefs);
+ dispatch(mergeLocalPrefs(prefs));
+ },
+);
+
+export const loadGlobalPrefs = createAppAsyncThunk(
+ `${sliceName}/loadGlobalPrefs`,
+ async (_, { dispatch, getState }) => {
+ const globalPrefs = await send('load-global-prefs');
+ dispatch(
+ setPrefs({
+ local: getState().prefs.local,
+ global: globalPrefs,
+ synced: getState().prefs.synced,
+ }),
+ );
+ return globalPrefs;
+ },
+);
+
+type SaveGlobalPrefsPayload = {
+ prefs: GlobalPrefs;
+ onSaveGlobalPrefs?: () => void;
+};
+
+export const saveGlobalPrefs = createAppAsyncThunk(
+ `${sliceName}/saveGlobalPrefs`,
+ async (
+ { prefs, onSaveGlobalPrefs }: SaveGlobalPrefsPayload,
+ { dispatch },
+ ) => {
+ await send('save-global-prefs', prefs);
+ dispatch(mergeGlobalPrefs(prefs));
+ onSaveGlobalPrefs?.();
+ },
+);
+
+type SaveSyncedPrefsPayload = {
+ prefs: SyncedPrefs;
+};
+
+export const saveSyncedPrefs = createAppAsyncThunk(
+ `${sliceName}/saveSyncedPrefs`,
+ async ({ prefs }: SaveSyncedPrefsPayload, { dispatch }) => {
+ await Promise.all(
+ Object.entries(prefs).map(([prefName, value]) =>
+ send('preferences/save', {
+ id: prefName as keyof SyncedPrefs,
+ value,
+ }),
+ ),
+ );
+ dispatch(mergeSyncedPrefs(prefs));
+ },
+);
+
+type SetPrefsPayload = {
+ local: MetadataPrefs;
+ global: GlobalPrefs;
+ synced: SyncedPrefs;
+};
+
+type MergeLocalPrefsPayload = MetadataPrefs;
+type MergeGlobalPrefsPayload = GlobalPrefs;
+type MergeSyncedPrefsPayload = SyncedPrefs;
+
+const prefsSlice = createSlice({
+ name: sliceName,
+ initialState,
+ reducers: {
+ setPrefs(state, action: PayloadAction<SetPrefsPayload>) {
+ state.local = action.payload.local;
+ state.global = action.payload.global;
+ state.synced = action.payload.synced;
+ },
+ mergeLocalPrefs(state, action: PayloadAction<MergeLocalPrefsPayload>) {
+ state.local = { ...state.local, ...action.payload };
+ },
+ mergeGlobalPrefs(state, action: PayloadAction<MergeGlobalPrefsPayload>) {
+ state.global = { ...state.global, ...action.payload };
+ },
+ mergeSyncedPrefs(state, action: PayloadAction<MergeSyncedPrefsPayload>) {
+ state.synced = { ...state.synced, ...action.payload };
+ },
+ },
+});
+
+export const { name, reducer, getInitialState } = prefsSlice;
+
+export const actions = {
+ ...prefsSlice.actions,
+ loadPrefs,
+ savePrefs,
+ loadGlobalPrefs,
+ saveGlobalPrefs,
+ saveSyncedPrefs,
+};
+
+export const { mergeGlobalPrefs, mergeLocalPrefs, mergeSyncedPrefs, setPrefs } =
+ actions;
diff --git a/packages/loot-core/src/client/reducers/index.ts b/packages/loot-core/src/client/reducers/index.ts
index 9b0ab97ac5..0c45afd7b4 100644
--- a/packages/loot-core/src/client/reducers/index.ts
+++ b/packages/loot-core/src/client/reducers/index.ts
@@ -1,7 +1,5 @@
-import { update as prefs } from './prefs';
import { update as user } from './user';
export const reducers = {
- prefs,
user,
};
diff --git a/packages/loot-core/src/client/reducers/prefs.ts b/packages/loot-core/src/client/reducers/prefs.ts
deleted file mode 100644
index dd2c865993..0000000000
--- a/packages/loot-core/src/client/reducers/prefs.ts
+++ /dev/null
@@ -1,39 +0,0 @@
-import * as constants from '../constants';
-import type { Action } from '../state-types';
-import type { PrefsState } from '../state-types/prefs';
-
-export const initialState: PrefsState = {
- local: {},
- global: {},
- synced: {},
-};
-
-export function update(state = initialState, action: Action): PrefsState {
- switch (action.type) {
- case constants.SET_PREFS:
- return {
- ...state,
- local: action.prefs,
- global: action.globalPrefs,
- synced: action.syncedPrefs,
- };
- case constants.MERGE_LOCAL_PREFS:
- return {
- ...state,
- local: { ...state.local, ...action.prefs },
- };
- case constants.MERGE_GLOBAL_PREFS:
- return {
- ...state,
- global: { ...state.global, ...action.globalPrefs },
- };
- case constants.MERGE_SYNCED_PREFS:
- return {
- ...state,
- synced: { ...state.synced, ...action.syncedPrefs },
- };
-
- default:
- }
- return state;
-}
diff --git a/packages/loot-core/src/client/shared-listeners.ts b/packages/loot-core/src/client/shared-listeners.ts
index c1dbd41d8b..3561c39ed4 100644
--- a/packages/loot-core/src/client/shared-listeners.ts
+++ b/packages/loot-core/src/client/shared-listeners.ts
@@ -3,7 +3,7 @@ import { t } from 'i18next';
import { listen, send } from '../platform/client/fetch';
-import { loadPrefs, signOut } from './actions';
+import { signOut } from './actions';
import { resetSync, sync } from './app/appSlice';
import { closeAndDownloadBudget, uploadBudget } from './budgets/budgetsSlice';
import { pushModal } from './modals/modalsSlice';
@@ -11,6 +11,7 @@ import {
addNotification,
type Notification,
} from './notifications/notificationsSlice';
+import { loadPrefs } from './prefs/prefsSlice';
import { getAccounts, getCategories, getPayees } from './queries/queriesSlice';
import { type AppStore } from './store';
diff --git a/packages/loot-core/src/client/state-types/index.d.ts b/packages/loot-core/src/client/state-types/index.d.ts
index 61f4888196..9e9d966570 100644
--- a/packages/loot-core/src/client/state-types/index.d.ts
+++ b/packages/loot-core/src/client/state-types/index.d.ts
@@ -1,15 +1,13 @@
import type * as constants from '../constants';
-import type { PrefsActions, PrefsState } from './prefs';
import type { UserActions, UserState } from './user';
export type CloseBudgetAction = {
type: typeof constants.CLOSE_BUDGET;
};
-export type Action = PrefsActions | UserActions | CloseBudgetAction;
+export type Action = UserActions | CloseBudgetAction;
export type State = {
- prefs: PrefsState;
user: UserState;
};
diff --git a/packages/loot-core/src/client/state-types/prefs.d.ts b/packages/loot-core/src/client/state-types/prefs.d.ts
deleted file mode 100644
index 184dcdc5f4..0000000000
--- a/packages/loot-core/src/client/state-types/prefs.d.ts
+++ /dev/null
@@ -1,40 +0,0 @@
-import type {
- GlobalPrefs,
- MetadataPrefs,
- SyncedPrefs,
-} from '../../types/prefs';
-import type * as constants from '../constants';
-
-export type PrefsState = {
- local: MetadataPrefs;
- global: GlobalPrefs;
- synced: SyncedPrefs;
-};
-
-export type SetPrefsAction = {
- type: typeof constants.SET_PREFS;
- prefs: MetadataPrefs;
- globalPrefs: GlobalPrefs;
- syncedPrefs: SyncedPrefs;
-};
-
-export type MergeLocalPrefsAction = {
- type: typeof constants.MERGE_LOCAL_PREFS;
- prefs: MetadataPrefs;
-};
-
-export type MergeGlobalPrefsAction = {
- type: typeof constants.MERGE_GLOBAL_PREFS;
- globalPrefs: GlobalPrefs;
-};
-
-export type MergeSyncedPrefsAction = {
- type: typeof constants.MERGE_SYNCED_PREFS;
- syncedPrefs: SyncedPrefs;
-};
-
-export type PrefsActions =
- | SetPrefsAction
- | MergeLocalPrefsAction
- | MergeGlobalPrefsAction
- | MergeSyncedPrefsAction;
diff --git a/packages/loot-core/src/client/store/index.ts b/packages/loot-core/src/client/store/index.ts
index 4008c6c198..d3af684749 100644
--- a/packages/loot-core/src/client/store/index.ts
+++ b/packages/loot-core/src/client/store/index.ts
@@ -32,13 +32,17 @@ import {
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 initialPrefsState } from '../reducers/prefs';
import { initialState as initialUserState } from '../reducers/user';
const appReducer = combineReducers({
@@ -48,6 +52,7 @@ const appReducer = combineReducers({
[budgetsSliceName]: budgetsSliceReducer,
[modalsSliceName]: modalsSliceReducer,
[notificationsSliceName]: notificationsSliceReducer,
+ [prefsSliceName]: prefsSliceReducer,
[queriesSliceName]: queriesSliceReducer,
});
const rootReducer: typeof appReducer = (state, action) => {
@@ -62,9 +67,8 @@ const rootReducer: typeof appReducer = (state, action) => {
budgets: state?.budgets || getInitialBudgetsState(),
user: state?.user || initialUserState,
prefs: {
- local: initialPrefsState.local,
- global: state?.prefs?.global || initialPrefsState.global,
- synced: initialPrefsState.synced,
+ ...getInitialPrefsState(),
+ global: state?.prefs?.global || getInitialPrefsState().global,
},
app: {
...getInitialAppState(),
diff --git a/packages/loot-core/src/client/store/mock.ts b/packages/loot-core/src/client/store/mock.ts
index dcc5443acc..461d345c88 100644
--- a/packages/loot-core/src/client/store/mock.ts
+++ b/packages/loot-core/src/client/store/mock.ts
@@ -20,6 +20,10 @@ import {
name as notificationsSliceName,
reducer as notificationsSliceReducer,
} from '../notifications/notificationsSlice';
+import {
+ name as prefsSliceName,
+ reducer as prefsSliceReducer,
+} from '../prefs/prefsSlice';
import {
name as queriesSliceName,
reducer as queriesSliceReducer,
@@ -35,6 +39,7 @@ const appReducer = combineReducers({
[budgetsSliceName]: budgetsSliceReducer,
[modalsSliceName]: modalsSliceReducer,
[notificationsSliceName]: notificationsSliceReducer,
+ [prefsSliceName]: prefsSliceReducer,
[queriesSliceName]: queriesSliceReducer,
});
diff --git a/packages/loot-core/src/server/preferences/app.ts b/packages/loot-core/src/server/preferences/app.ts
index 1cea369a2f..b18b9026c3 100644
--- a/packages/loot-core/src/server/preferences/app.ts
+++ b/packages/loot-core/src/server/preferences/app.ts
@@ -43,6 +43,10 @@ async function saveSyncedPrefs({
id: keyof SyncedPrefs;
value: string | undefined;
}) {
+ if (!id) {
+ return;
+ }
+
await db.update('preferences', { id, value });
}
@@ -58,30 +62,34 @@ async function getSyncedPrefs(): Promise<SyncedPrefs> {
}
async function saveGlobalPrefs(prefs: GlobalPrefs) {
- if ('maxMonths' in prefs) {
+ if (!prefs) {
+ return 'ok';
+ }
+
+ if (prefs.maxMonths) {
await asyncStorage.setItem('max-months', '' + prefs.maxMonths);
}
- if ('documentDir' in prefs) {
+ if (prefs.documentDir) {
if (prefs.documentDir && (await fs.exists(prefs.documentDir))) {
await asyncStorage.setItem('document-dir', prefs.documentDir);
}
}
- if ('floatingSidebar' in prefs) {
+ if (prefs.floatingSidebar) {
await asyncStorage.setItem('floating-sidebar', '' + prefs.floatingSidebar);
}
- if ('language' in prefs) {
+ if (prefs.language) {
await asyncStorage.setItem('language', prefs.language);
}
- if ('theme' in prefs) {
+ if (prefs.theme) {
await asyncStorage.setItem('theme', prefs.theme);
}
- if ('preferredDarkTheme' in prefs) {
+ if (prefs.preferredDarkTheme) {
await asyncStorage.setItem(
'preferred-dark-theme',
prefs.preferredDarkTheme,
);
}
- if ('serverSelfSignedCert' in prefs) {
+ if (prefs.serverSelfSignedCert) {
await asyncStorage.setItem(
'server-self-signed-cert',
prefs.serverSelfSignedCert,
@@ -90,7 +98,7 @@ async function saveGlobalPrefs(prefs: GlobalPrefs) {
return 'ok';
}
-async function loadGlobalPrefs() {
+async function loadGlobalPrefs(): Promise<GlobalPrefs> {
const [
[, floatingSidebar],
[, maxMonths],
@@ -112,7 +120,7 @@ async function loadGlobalPrefs() {
] as const);
return {
floatingSidebar: floatingSidebar === 'true',
- maxMonths: stringToInteger(maxMonths || ''),
+ maxMonths: stringToInteger(maxMonths || '') || 1,
documentDir: documentDir || getDefaultDocumentDir(),
keyId: encryptKey && JSON.parse(encryptKey).id,
language,
@@ -133,6 +141,10 @@ async function loadGlobalPrefs() {
}
async function saveMetadataPrefs(prefsToSet: MetadataPrefs) {
+ if (!prefsToSet) {
+ return 'ok';
+ }
+
const { cloudFileId } = _getMetadataPrefs();
// Need to sync the budget name on the server as well
diff --git a/upcoming-release-notes/4127.md b/upcoming-release-notes/4127.md
new file mode 100644
index 0000000000..032b2930dc
--- /dev/null
+++ b/upcoming-release-notes/4127.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [joel-jeremy]
+---
+
+Phase 2 - Redux Toolkit Migration - preferences slice