Refactor schedule handling in ManageRules component (#5600)

- Updated the ManageRules component to use ScheduleEntity type for schedules.
- Improved the describeSchedule function to include type annotations for better clarity.
- Added a conditional check to handle cases where the schedule may not exist, ensuring robust functionality.
This commit is contained in:
Matiss Janis Aboltins
2025-08-21 20:09:21 +01:00
committed by GitHub
parent fe32bf14c6
commit c69142f58e
3 changed files with 24 additions and 12 deletions

View File

@@ -21,7 +21,11 @@ import { getNormalisedString } from 'loot-core/shared/normalisation';
import { q } from 'loot-core/shared/query'; import { q } from 'loot-core/shared/query';
import { mapField, friendlyOp } from 'loot-core/shared/rules'; import { mapField, friendlyOp } from 'loot-core/shared/rules';
import { describeSchedule } from 'loot-core/shared/schedules'; import { describeSchedule } from 'loot-core/shared/schedules';
import { type RuleEntity, type NewRuleEntity } from 'loot-core/types/models'; import {
type RuleEntity,
type NewRuleEntity,
type ScheduleEntity,
} from 'loot-core/types/models';
import { InfiniteScrollWrapper } from './common/InfiniteScrollWrapper'; import { InfiniteScrollWrapper } from './common/InfiniteScrollWrapper';
import { Link } from './common/Link'; import { Link } from './common/Link';
@@ -45,12 +49,7 @@ export type FilterData = {
payees?: Array<{ id: string; name: string }>; payees?: Array<{ id: string; name: string }>;
categories?: Array<{ id: string; name: string }>; categories?: Array<{ id: string; name: string }>;
accounts?: Array<{ id: string; name: string }>; accounts?: Array<{ id: string; name: string }>;
schedules?: readonly { schedules?: readonly ScheduleEntity[];
id: string;
rule: string;
_payee: string;
completed: boolean;
}[];
}; };
export function mapValue( export function mapValue(
@@ -98,10 +97,12 @@ export function ruleToString(rule: RuleEntity, data: FilterData) {
const schedule = data.schedules?.find(s => s.id === String(action.value)); const schedule = data.schedules?.find(s => s.id === String(action.value));
return [ return [
friendlyOp(action.op), friendlyOp(action.op),
describeSchedule( schedule
schedule, ? describeSchedule(
data.payees?.find(p => p.id === schedule?._payee), schedule,
), data.payees?.find(p => p.id === schedule._payee),
)
: '-',
]; ];
} else if (action.op === 'prepend-notes' || action.op === 'append-notes') { } else if (action.op === 'prepend-notes' || action.op === 'append-notes') {
const noteValue = String(action.value || ''); const noteValue = String(action.value || '');

View File

@@ -4,6 +4,8 @@ import * as d from 'date-fns';
import { Locale } from 'date-fns'; import { Locale } from 'date-fns';
import { t } from 'i18next'; import { t } from 'i18next';
import { type PayeeEntity, type ScheduleEntity } from 'loot-core/types/models';
import { Condition } from '../server/rules'; import { Condition } from '../server/rules';
import * as monthUtils from './months'; import * as monthUtils from './months';
@@ -394,7 +396,10 @@ export function getScheduledAmount(
return inverse ? -Math.round(avg) : Math.round(avg); return inverse ? -Math.round(avg) : Math.round(avg);
} }
export function describeSchedule(schedule, payee) { export function describeSchedule(
schedule: ScheduleEntity,
payee?: PayeeEntity,
) {
if (payee) { if (payee) {
return `${payee.name} (${schedule.next_date})`; return `${payee.name} (${schedule.next_date})`;
} else { } else {

View File

@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [MatissJanis]
---
Add missing conditional for describing schedules in rules page