Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 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,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<TitlebarContextValue>(null);
Expand All @@ -53,13 +65,13 @@ type TitlebarProviderProps = {
};

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

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));
Expand Down Expand Up @@ -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,
},
});
}
}

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
15 changes: 10 additions & 5 deletions packages/desktop-client/src/components/budget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
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