Skip to content

Commit

Permalink
Fix switching budget from rollover budget to report budget and immedi…
Browse files Browse the repository at this point in the history
…ately back does not work
  • Loading branch information
paulsukow committed Nov 19, 2023
1 parent 6527312 commit 4379c81
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
17 changes: 10 additions & 7 deletions packages/desktop-client/src/components/Titlebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -41,9 +42,10 @@ import useSheetValue from './spreadsheet/useSheetValue';
import { ThemeSelector } from './ThemeSelector';
import { Tooltip } from './tooltips';

type Listener = (msg: string, newBudgetType?: LocalPrefs['budgetType']) => void;
export type TitlebarContextValue = {
sendEvent: (msg: string) => void;
subscribe: (listener) => () => void;
sendEvent: (msg: string, newBudgetType?: LocalPrefs['budgetType']) => void;
subscribe: (listener: Listener) => () => void;
};

export let TitlebarContext = createContext<TitlebarContextValue>(null);
Expand All @@ -53,13 +55,13 @@ type TitlebarProviderProps = {
};

export function TitlebarProvider({ children }: TitlebarProviderProps) {
let listeners = useRef([]);
let listeners = useRef<Listener[]>([]);

function sendEvent(msg: string) {
listeners.current.forEach(func => func(msg));
function sendEvent(msg: string, newBudgetType?: LocalPrefs['budgetType']) {
listeners.current.forEach(func => func(msg, newBudgetType));
}

function subscribe(listener) {
function subscribe(listener: Listener) {
listeners.current.push(listener);
return () =>
(listeners.current = listeners.current.filter(func => func !== listener));
Expand Down Expand Up @@ -268,7 +270,8 @@ function BudgetTitlebar() {
function onSwitchType() {
setLoading(true);
if (!loading) {
sendEvent('budget/switch-type');
const newBudgetType = budgetType === 'rollover' ? 'report' : 'rollover';
sendEvent('budget/switch-type', newBudgetType);
}
}

Expand Down
9 changes: 7 additions & 2 deletions packages/desktop-client/src/components/budget/MobileBudget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
7 changes: 5 additions & 2 deletions packages/desktop-client/src/components/budget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,14 @@ function Budget(props: BudgetProps) {
props.savePrefs({ 'budget.summaryCollapsed': collapsed });
};

const onTitlebarEvent = async msg => {
const onTitlebarEvent = async (
msg: string,
newBudgetType: LocalPrefs['budgetType'],
) => {
switch (msg) {
case 'budget/switch-type': {
await switchBudgetType(
props.budgetType,
newBudgetType,
props.spreadsheet,
bounds,
prewarmStartMonth,
Expand Down
9 changes: 3 additions & 6 deletions packages/desktop-client/src/components/budget/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,15 @@ export async function prewarmAllMonths(
}

export async function switchBudgetType(
budgetType: LocalPrefs['budgetType'],
newBudgetType: LocalPrefs['budgetType'],
spreadsheet: ReturnType<typeof useSpreadsheet>,
bounds: { start: string; end: string },
startMonth: string,
onSuccess: () => Promise<void> | 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?.();
}
6 changes: 6 additions & 0 deletions upcoming-release-notes/1933.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [paulsukow]
---

Fix switching budget from rollover budget to report budget and immediately back does not work

0 comments on commit 4379c81

Please sign in to comment.