From 9ad7f637dd0e25553452720a470beb93c1ded5f7 Mon Sep 17 00:00:00 2001 From: Paul Sukow <21179028+paulsukow@users.noreply.github.com> Date: Tue, 21 Nov 2023 09:15:18 -0700 Subject: [PATCH] Fix switching budget from rollover budget to report budget and immediately back does not work (#1933) * Fix switching budget from rollover budget to report budget and immediately back does not work * Pr Changes --- .../src/components/Titlebar.tsx | 30 +++++++++++++++---- .../src/components/budget/MobileBudget.js | 9 ++++-- .../src/components/budget/index.tsx | 15 ++++++---- .../src/components/budget/util.ts | 9 ++---- upcoming-release-notes/1933.md | 6 ++++ 5 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 upcoming-release-notes/1933.md diff --git a/packages/desktop-client/src/components/Titlebar.tsx b/packages/desktop-client/src/components/Titlebar.tsx index 816753b0c3b..6eb123d3c69 100644 --- a/packages/desktop-client/src/components/Titlebar.tsx +++ b/packages/desktop-client/src/components/Titlebar.tsx @@ -12,6 +12,7 @@ import { Routes, Route, useLocation } from 'react-router-dom'; import * as Platform from 'loot-core/src/client/platform'; import * as queries from 'loot-core/src/client/queries'; import { listen } from 'loot-core/src/platform/client/fetch'; +import { type LocalPrefs } from 'loot-core/src/types/prefs'; import { useActions } from '../hooks/useActions'; import useFeatureFlag from '../hooks/useFeatureFlag'; @@ -41,9 +42,20 @@ import useSheetValue from './spreadsheet/useSheetValue'; import { ThemeSelector } from './ThemeSelector'; import { Tooltip } from './tooltips'; +export const SWITCH_BUDGET_MESSAGE_TYPE = 'budget/switch-type'; + +type SwitchBudgetTypeMessage = { + type: typeof SWITCH_BUDGET_MESSAGE_TYPE; + payload: { + newBudgetType: LocalPrefs['budgetType']; + }; +}; +export type TitlebarMessage = SwitchBudgetTypeMessage; + +type Listener = (msg: TitlebarMessage) => void; export type TitlebarContextValue = { - sendEvent: (msg: string) => void; - subscribe: (listener) => () => void; + sendEvent: (msg: TitlebarMessage) => void; + subscribe: (listener: Listener) => () => void; }; export let TitlebarContext = createContext(null); @@ -53,13 +65,13 @@ type TitlebarProviderProps = { }; export function TitlebarProvider({ children }: TitlebarProviderProps) { - let listeners = useRef([]); + let listeners = useRef([]); - function sendEvent(msg: string) { + function sendEvent(msg: TitlebarMessage) { listeners.current.forEach(func => func(msg)); } - function subscribe(listener) { + function subscribe(listener: Listener) { listeners.current.push(listener); return () => (listeners.current = listeners.current.filter(func => func !== listener)); @@ -268,7 +280,13 @@ function BudgetTitlebar() { function onSwitchType() { setLoading(true); if (!loading) { - sendEvent('budget/switch-type'); + const newBudgetType = budgetType === 'rollover' ? 'report' : 'rollover'; + sendEvent({ + type: SWITCH_BUDGET_MESSAGE_TYPE, + payload: { + newBudgetType, + }, + }); } } diff --git a/packages/desktop-client/src/components/budget/MobileBudget.js b/packages/desktop-client/src/components/budget/MobileBudget.js index f937f5f9a1b..f624771d643 100644 --- a/packages/desktop-client/src/components/budget/MobileBudget.js +++ b/packages/desktop-client/src/components/budget/MobileBudget.js @@ -328,8 +328,13 @@ class Budget extends Component { this.setState({ initialized: false }); - await switchBudgetType(budgetType, spreadsheet, bounds, currentMonth, () => - loadPrefs(), + const newBudgetType = budgetType === 'rollover' ? 'report' : 'rollover'; + await switchBudgetType( + newBudgetType, + spreadsheet, + bounds, + currentMonth, + () => loadPrefs(), ); this.setState({ initialized: true }); diff --git a/packages/desktop-client/src/components/budget/index.tsx b/packages/desktop-client/src/components/budget/index.tsx index 81ab8b88b0d..d72b15de959 100644 --- a/packages/desktop-client/src/components/budget/index.tsx +++ b/packages/desktop-client/src/components/budget/index.tsx @@ -36,7 +36,12 @@ import useFeatureFlag from '../../hooks/useFeatureFlag'; import useNavigate from '../../hooks/useNavigate'; import { styles } from '../../style'; import View from '../common/View'; -import { TitlebarContext, type TitlebarContextValue } from '../Titlebar'; +import { + SWITCH_BUDGET_MESSAGE_TYPE, + TitlebarContext, + type TitlebarContextValue, + type TitlebarMessage, +} from '../Titlebar'; import DynamicBudgetTable from './DynamicBudgetTable'; import * as report from './report/ReportComponents'; @@ -406,11 +411,11 @@ function Budget(props: BudgetProps) { props.savePrefs({ 'budget.summaryCollapsed': collapsed }); }; - const onTitlebarEvent = async msg => { - switch (msg) { - case 'budget/switch-type': { + const onTitlebarEvent = async ({ type, payload }: TitlebarMessage) => { + switch (type) { + case SWITCH_BUDGET_MESSAGE_TYPE: { await switchBudgetType( - props.budgetType, + payload.newBudgetType, props.spreadsheet, bounds, prewarmStartMonth, diff --git a/packages/desktop-client/src/components/budget/util.ts b/packages/desktop-client/src/components/budget/util.ts index ba590982b1b..6e9f7dd1427 100644 --- a/packages/desktop-client/src/components/budget/util.ts +++ b/packages/desktop-client/src/components/budget/util.ts @@ -165,18 +165,15 @@ export async function prewarmAllMonths( } export async function switchBudgetType( - budgetType: LocalPrefs['budgetType'], + newBudgetType: LocalPrefs['budgetType'], spreadsheet: ReturnType, bounds: { start: string; end: string }, startMonth: string, onSuccess: () => Promise | undefined, ) { - let newType: 'rollover' | 'report' = - budgetType === 'rollover' ? 'report' : 'rollover'; - spreadsheet.disableObservers(); - await send('budget-set-type', { type: newType }); - await prewarmAllMonths(newType, spreadsheet, bounds, startMonth); + await send('budget-set-type', { type: newBudgetType }); + await prewarmAllMonths(newBudgetType, spreadsheet, bounds, startMonth); spreadsheet.enableObservers(); await onSuccess?.(); } diff --git a/upcoming-release-notes/1933.md b/upcoming-release-notes/1933.md new file mode 100644 index 00000000000..12b52dec1d8 --- /dev/null +++ b/upcoming-release-notes/1933.md @@ -0,0 +1,6 @@ +--- +category: Bugfix +authors: [paulsukow] +--- + +Fix switching budget from rollover budget to report budget and immediately back does not work