Load real scheduled transactions.
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
export const serverHostname = 'http://localhost';
|
||||||
|
export const serverPort = 3000;
|
||||||
|
export const serverBasePath = `${serverHostname}:${serverPort}/api`;
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import { serverBasePath } from './index';
|
||||||
|
import type { ScheduledTransaction } from '../models/scheduled-transaction';
|
||||||
|
|
||||||
|
export async function listScheduledTransactions(f: typeof fetch): Promise<ScheduledTransaction[]> {
|
||||||
|
return await f(`${serverBasePath}/scheduled-transactions`).then((res) => res.json());
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
export type Direction = 'in' | 'out';
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import type { UUID } from 'crypto';
|
||||||
|
import type { Direction } from './direction';
|
||||||
|
|
||||||
|
export type RecurringTransaction = {
|
||||||
|
type: 'recurring';
|
||||||
|
id: UUID;
|
||||||
|
name: string;
|
||||||
|
account: AccountResponse;
|
||||||
|
amount_cents: number;
|
||||||
|
direction: Direction;
|
||||||
|
schedule: ScheduleResponse;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type AccountResponse = {
|
||||||
|
id: UUID;
|
||||||
|
name: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ScheduleResponse = {
|
||||||
|
start: Date;
|
||||||
|
end?: Date;
|
||||||
|
recurrence: Recurrence;
|
||||||
|
weekend_adjustment?: WeekendAdjustment;
|
||||||
|
next?: Date;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type Recurrence = 'day' | 'week' | 'month' | 'year';
|
||||||
|
|
||||||
|
export type WeekendAdjustment = 'previous_business_day' | 'next_business_day';
|
||||||
|
|
||||||
|
export type ScheduledTransaction = RecurringTransaction;
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
export function renderAmount(cents: number, direction: 'in' | 'out'): string {
|
||||||
|
const euros = (cents / 100).toFixed(2);
|
||||||
|
const sign = direction === 'in' ? '' : '-';
|
||||||
|
return `${sign}€${euros}`;
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import type { PageServerLoad } from './$types';
|
||||||
|
import { listScheduledTransactions } from '#lib/data/scheduled-transactions.ts';
|
||||||
|
|
||||||
|
export const load: PageServerLoad = async ({ fetch }) => {
|
||||||
|
return {
|
||||||
|
scheduledTransactions: listScheduledTransactions(fetch)
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -1,74 +1,35 @@
|
|||||||
<script lang="ts" module>
|
<script lang="ts" module>
|
||||||
type ScheduledTransaction = {
|
import type { ScheduledTransaction } from '#lib/models/scheduled-transaction.ts';
|
||||||
checked: boolean;
|
import { renderAmount } from '#lib/render/amount.ts';
|
||||||
id: number;
|
|
||||||
name: string;
|
|
||||||
description: string;
|
|
||||||
account: string;
|
|
||||||
amount: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
const columns: Column<ScheduledTransaction>[] = [
|
const columns: Column<ScheduledTransaction>[] = [
|
||||||
{ key: 'checked' },
|
|
||||||
{ key: 'id', label: '#' },
|
|
||||||
{ key: 'name', label: 'Name', ratio: 1 },
|
{ key: 'name', label: 'Name', ratio: 1 },
|
||||||
{ key: 'account', label: 'Account' },
|
{ key: 'account', label: 'Account' },
|
||||||
{ key: 'amount', label: 'Amount' }
|
{ key: 'amount_cents', label: 'Amount' }
|
||||||
];
|
];
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import Checkbox from '#lib/components/checkbox.svelte';
|
|
||||||
import Table, { type Column } from '#lib/components/table.svelte';
|
import Table, { type Column } from '#lib/components/table.svelte';
|
||||||
|
import type { PageProps } from './$types';
|
||||||
|
|
||||||
const data: ScheduledTransaction[] = $state([
|
let { data }: PageProps = $props();
|
||||||
{
|
|
||||||
checked: true,
|
|
||||||
id: 1,
|
|
||||||
name: 'Some transaction',
|
|
||||||
description: 'Some description',
|
|
||||||
account: 'Some account',
|
|
||||||
amount: 100
|
|
||||||
},
|
|
||||||
{
|
|
||||||
checked: false,
|
|
||||||
id: 2,
|
|
||||||
name: 'Another transaction',
|
|
||||||
description: 'Another description',
|
|
||||||
account: 'Another account',
|
|
||||||
amount: 200
|
|
||||||
},
|
|
||||||
{
|
|
||||||
checked: false,
|
|
||||||
id: 3,
|
|
||||||
name: 'Yet another transaction',
|
|
||||||
description: 'Yet another description, but longer than the others',
|
|
||||||
account: 'Yet another account',
|
|
||||||
amount: 300
|
|
||||||
}
|
|
||||||
]);
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:head>
|
<svelte:head>
|
||||||
<title>Scheduled transactions | Expenses</title>
|
<title>Scheduled transactions | Expenses</title>
|
||||||
</svelte:head>
|
</svelte:head>
|
||||||
|
|
||||||
<Table {columns} {data} style={({ row }) => (row?.checked ? ['highlight'] : [])}>
|
{#await data.scheduledTransactions then scheduledTransactions}
|
||||||
{#snippet header({ key, label })}
|
<Table {columns} data={scheduledTransactions}>
|
||||||
{#if key === 'checked'}
|
{#snippet cell({ key, value, row })}
|
||||||
<Checkbox
|
{#if key === 'account'}
|
||||||
onchange={(e) => data.forEach((row) => (row.checked = e.currentTarget.checked))}
|
{value.name}
|
||||||
checked={data.every((row) => row.checked)}
|
{:else if key === 'amount_cents'}
|
||||||
/>
|
{renderAmount(value, row.direction)}
|
||||||
{:else}
|
{:else}
|
||||||
{label}
|
{value}
|
||||||
{/if}
|
{/if}
|
||||||
{/snippet}
|
{/snippet}
|
||||||
{#snippet cell({ key, value, row })}
|
</Table>
|
||||||
{#if key === 'checked'}
|
{/await}
|
||||||
<Checkbox bind:checked={row.checked} />
|
|
||||||
{:else}
|
|
||||||
{value}
|
|
||||||
{/if}
|
|
||||||
{/snippet}
|
|
||||||
</Table>
|
|
||||||
|
|||||||
Reference in New Issue
Block a user