Skip to content

Commit

Permalink
refactor: refactor type definition to data-hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
muhsinkamil committed Sep 16, 2023
1 parent a47e53e commit d45ed47
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 47 deletions.
71 changes: 40 additions & 31 deletions packages/desktop-client/src/components/schedules/SchedulesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ type SchedulesTableProps = {
type CompletedScheduleItem = { id: 'show-completed' };
type SchedulesTableItem = ScheduleEntity | CompletedScheduleItem;

export enum ScheduleItemAction {
PostTransaction = 'post-transaction',
SkipSchedule = 'skip',
CompleteSchedule = 'complete',
RestartSchedule = 'restart',
DeleteSchedule = 'delete',
}
export type ScheduleItemAction =
| 'post-transaction'
| 'skip'
| 'complete'
| 'restart'
| 'delete';

export const ROW_HEIGHT = 43;

Expand All @@ -62,6 +61,39 @@ function OverflowMenu({
}) {
const [open, setOpen] = useState(false);

const getMenuItems = () => {
const menuItems: { name: ScheduleItemAction; text: string }[] = [];

if (status === 'due') {
menuItems.push({
name: 'post-transaction',
text: 'Post transaction',
});
}

if (status === 'completed') {
menuItems.push({
name: 'restart',
text: 'Restart',
});
} else {
menuItems.push(
{
name: 'skip',
text: 'Skip next date',
},
{
name: 'complete',
text: 'Complete',
},
);
}

menuItems.push({ name: 'delete', text: 'Delete' });

return menuItems;
};

return (
<View>
<Button
Expand Down Expand Up @@ -89,30 +121,7 @@ function OverflowMenu({
onAction(name, schedule.id);
setOpen(false);
}}
items={[
status === 'due' && {
name: ScheduleItemAction.PostTransaction,
text: 'Post transaction',
},
...(schedule.completed
? [
{
name: ScheduleItemAction.RestartSchedule,
text: 'Restart',
},
]
: [
{
name: ScheduleItemAction.SkipSchedule,
text: 'Skip next date',
},
{
name: ScheduleItemAction.CompleteSchedule,
text: 'Complete',
},
]),
{ name: ScheduleItemAction.DeleteSchedule, text: 'Delete' },
]}
items={getMenuItems()}
/>
</Tooltip>
)}
Expand Down
12 changes: 6 additions & 6 deletions packages/desktop-client/src/components/schedules/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Page } from '../Page';
import {
SchedulesTable,
ROW_HEIGHT,
ScheduleItemAction,
type ScheduleItemAction,
} from './SchedulesTable';

export default function Schedules() {
Expand Down Expand Up @@ -43,24 +43,24 @@ export default function Schedules() {

async function onAction(name: ScheduleItemAction, id: ScheduleEntity['id']) {
switch (name) {
case ScheduleItemAction.PostTransaction:
case 'post-transaction':
await send('schedule/post-transaction', { id });
break;
case ScheduleItemAction.SkipSchedule:
case 'skip':
await send('schedule/skip-next-date', { id });
break;
case ScheduleItemAction.CompleteSchedule:
case 'complete':
await send('schedule/update', {
schedule: { id, completed: true },
});
break;
case ScheduleItemAction.RestartSchedule:
case 'restart':
await send('schedule/update', {
schedule: { id, completed: false },
resetNextDate: true,
});
break;
case ScheduleItemAction.DeleteSchedule:
case 'delete':
await send('schedule/delete', { id });
break;
default:
Expand Down
10 changes: 5 additions & 5 deletions packages/loot-core/src/client/data-hooks/accounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import q from '../query-helpers';
import { useLiveQuery } from '../query-hooks';
import { getAccountsById } from '../reducers/queries';

function useAccounts() {
function useAccounts(): AccountEntity[] {
return useLiveQuery(() => q('accounts').select('*'), []);
}

let AccountsContext = createContext(null);
const AccountsContext = createContext<AccountEntity[]>(null);

export function AccountsProvider({ children }) {
const data: AccountEntity[] = useAccounts();
const data = useAccounts();
return <AccountsContext.Provider value={data} children={children} />;
}

export function CachedAccounts({ children, idKey }) {
let data = useCachedAccounts({ idKey });
const data = useCachedAccounts({ idKey });
return children(data);
}

Expand All @@ -28,6 +28,6 @@ export function useCachedAccounts({
idKey: boolean;
}): Record<AccountEntity['id'], AccountEntity>;
export function useCachedAccounts({ idKey }: { idKey?: boolean } = {}) {
const data: AccountEntity[] = useContext(AccountsContext);
const data = useContext(AccountsContext);
return idKey && data ? getAccountsById(data) : data;
}
10 changes: 5 additions & 5 deletions packages/loot-core/src/client/data-hooks/payees.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import q from '../query-helpers';
import { useLiveQuery } from '../query-hooks';
import { getPayeesById } from '../reducers/queries';

function usePayees() {
function usePayees(): PayeeEntity[] {
return useLiveQuery(() => q('payees').select('*'), []);
}

let PayeesContext = createContext(null);
const PayeesContext = createContext<PayeeEntity[]>(null);

export function PayeesProvider({ children }) {
const data: PayeeEntity[] = usePayees();
const data = usePayees();
return <PayeesContext.Provider value={data} children={children} />;
}

export function CachedPayees({ children, idKey }) {
let data = useCachedPayees({ idKey });
const data = useCachedPayees({ idKey });
return children(data);
}

Expand All @@ -28,6 +28,6 @@ export function useCachedPayees({
idKey: boolean;
}): Record<PayeeEntity['id'], PayeeEntity>;
export function useCachedPayees({ idKey }: { idKey?: boolean } = {}) {
const data: PayeeEntity[] = useContext(PayeesContext);
const data = useContext(PayeesContext);
return idKey && data ? getPayeesById(data) : data;
}

0 comments on commit d45ed47

Please sign in to comment.