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

[refactor] Schedules component in tsx #1644

Merged
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
6 changes: 3 additions & 3 deletions packages/desktop-client/src/components/common/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import Button from './Button';
import InputWithContent from './InputWithContent';

type SearchProps = {
inputRef: Ref<HTMLInputElement>;
inputRef?: Ref<HTMLInputElement>;
value: string;
onChange: (value: string) => unknown;
placeholder: string;
isInModal: boolean;
isInModal?: boolean;
width?: number;
};

Expand All @@ -21,7 +21,7 @@ export default function Search({
value,
onChange,
placeholder,
isInModal,
isInModal = false,
width = 250,
}: SearchProps) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState } from 'react';

import { useSchedules } from 'loot-core/src/client/data-hooks/schedules';
import { send } from 'loot-core/src/platform/client/fetch';
import { type ScheduleEntity } from 'loot-core/src/types/models';

import { useActions } from '../../hooks/useActions';
import Button from '../common/Button';
Expand All @@ -12,18 +13,18 @@ import { Page } from '../Page';
import { SchedulesTable, ROW_HEIGHT } from './SchedulesTable';

export default function Schedules() {
let { pushModal } = useActions();
let [filter, setFilter] = useState('');
const { pushModal } = useActions();
const [filter, setFilter] = useState('');

let scheduleData = useSchedules();
const scheduleData = useSchedules();

if (scheduleData == null) {
return null;
}

let { schedules, statuses } = scheduleData;
const { schedules, statuses } = scheduleData;

function onEdit(id) {
function onEdit(id: ScheduleEntity['id']) {
pushModal('schedule-edit', { id });
}

Expand All @@ -35,7 +36,8 @@ export default function Schedules() {
pushModal('schedules-discover');
}

async function onAction(name, id) {
// @todo: replace name: string with enum
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will make a follow-up PR for SchedulesTable and take care of it

async function onAction(name: string, id: ScheduleEntity['id']) {
switch (name) {
case 'post-transaction':
await send('schedule/post-transaction', { id });
Expand All @@ -44,7 +46,9 @@ export default function Schedules() {
await send('schedule/skip-next-date', { id });
break;
case 'complete':
await send('schedule/update', { schedule: { id, completed: true } });
await send('schedule/update', {
schedule: { id, completed: true },
});
break;
case 'restart':
await send('schedule/update', {
Expand Down Expand Up @@ -83,6 +87,9 @@ export default function Schedules() {
onSelect={onEdit}
onAction={onAction}
style={{ backgroundColor: 'white' }}
// @todo: Remove following props after typing SchedulesTable
minimal={undefined}
tableStyle={undefined}
/>
</View>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export default function EncryptionSettings() {
const missingCryptoAPI = !(window.crypto && crypto.subtle);

function onChangeKey() {
// @ts-expect-error useActions() type does not properly handle overloads
pushModal('create-encryption-key', { recreate: true });
}

Expand Down
1 change: 0 additions & 1 deletion packages/loot-core/src/client/actions/budgets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export function loadBudget(id: string, loadingText = '', options = {}) {
);

if (showBackups) {
// @ts-expect-error manager modals are not yet typed
dispatch(pushModal('load-backup', { budgetId: id }));
}
} else {
Expand Down
7 changes: 5 additions & 2 deletions packages/loot-core/src/client/actions/modals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ export function pushModal<M extends keyof ModalWithOptions>(
options: ModalWithOptions[M],
): PushModalAction;
export function pushModal(name: OptionlessModal): PushModalAction;
export function pushModal<M extends ModalType>(
name: M,
options?: FinanceModals[M],
): PushModalAction;
Comment on lines +18 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 praise: ‏nice!

export function pushModal<M extends ModalType>(
name: M,
options?: FinanceModals[M],
): PushModalAction {
// @ts-expect-error TS is unable to determine that `name` and `options` match
let modal: M = { name, options };
const modal = { name, options };
return { type: constants.PUSH_MODAL, modal };
}

Expand Down
17 changes: 12 additions & 5 deletions packages/loot-core/src/client/data-hooks/schedules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import React, { createContext, useEffect, useState, useContext } from 'react';

import { type Query } from '../../shared/query';
import { getStatus, getHasTransactionsQuery } from '../../shared/schedules';
import { type ScheduleEntity } from '../../types/models';
import q, { liveQuery } from '../query-helpers';

function loadStatuses(schedules, onData) {
function loadStatuses(schedules: ScheduleEntity[], onData) {
return liveQuery(getHasTransactionsQuery(schedules), onData, {
mapper: data => {
let hasTrans = new Set(data.filter(Boolean).map(row => row.schedule));
Expand All @@ -20,23 +21,29 @@ function loadStatuses(schedules, onData) {
}

type UseSchedulesArgs = { transform?: (q: Query) => Query };
type UseSchedulesReturnType = {
schedules: ScheduleEntity[];
statuses: Record<string, ReturnType<typeof getStatus>>;
} | null;
export function useSchedules({ transform }: UseSchedulesArgs = {}) {
let [data, setData] = useState(null);
let [data, setData] = useState<UseSchedulesReturnType | null>(null);

useEffect(() => {
let query = q('schedules').select('*');
let scheduleQuery, statusQuery;

scheduleQuery = liveQuery(
transform ? transform(query) : query,
async schedules => {
async (schedules: ScheduleEntity[]) => {
if (scheduleQuery) {
if (statusQuery) {
statusQuery.unsubscribe();
}

statusQuery = loadStatuses(schedules, statuses =>
setData({ schedules, statuses }),
statusQuery = loadStatuses(
schedules,
(statuses: Record<string, ReturnType<typeof getStatus>>) =>
setData({ schedules, statuses }),
);
}
},
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/1644.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [muhsinkamil]
---

Refactor Schedules to tsx.