-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
851dd30
commit c1bdef3
Showing
4 changed files
with
324 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type { | ||
NewTablePropSet, | ||
TablePlugin | ||
} from 'svelte-headless-table/lib/types/TablePlugin'; | ||
import { type Readable, type Writable, derived, writable } from 'svelte/store'; | ||
|
||
export type EditRowState = { | ||
active: Writable<string | null>; | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-interface, unused-imports/no-unused-vars | ||
export interface EditRowColumnOptions<Item> {} | ||
|
||
export type EditRowPropSet = NewTablePropSet<{ | ||
'tbody.tr': { | ||
editing: boolean; | ||
edit: () => void; | ||
cancel: () => void; | ||
}; | ||
}>; | ||
|
||
export function addEditRow<Item>(): TablePlugin< | ||
Item, | ||
EditRowState, | ||
EditRowColumnOptions<Item>, | ||
EditRowPropSet | ||
> { | ||
return () => { | ||
const active = writable<string | null>(null); | ||
const pluginState: EditRowState = { active }; | ||
|
||
return { | ||
pluginState, | ||
hooks: { | ||
'tbody.tr': ({ id }) => { | ||
const props: Readable<EditRowPropSet['tbody.tr']> = derived( | ||
[active], | ||
([$active]) => ({ | ||
editing: $active === id, | ||
edit: () => active.set(id), | ||
cancel: () => active.set(null) | ||
}) | ||
); | ||
return { props }; | ||
} | ||
} | ||
}; | ||
}; | ||
} |
144 changes: 144 additions & 0 deletions
144
src/lib/components/transactions/TransactionInlineEdit.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<script lang="ts"> | ||
import { createMutation } from '@tanstack/svelte-query'; | ||
import { slide } from 'svelte/transition'; | ||
import { superForm, superValidateSync } from 'sveltekit-superforms/client'; | ||
import { | ||
type Transaction, | ||
type TransactionSplitUpdate, | ||
TransactionsApi | ||
} from '$lib/api'; | ||
import { queryClient } from '$lib/client'; | ||
import { transactionSplitSchema } from '$lib/schemas/transaction'; | ||
import { useService } from '$lib/services'; | ||
import Button from '$lib/components/Button.svelte'; | ||
import StatusButton from '$lib/components/StatusButton.svelte'; | ||
import SelectField from '$lib/components/form/SelectField.svelte'; | ||
import TagsField from '$lib/components/form/TagsField.svelte'; | ||
import TextAreaField from '$lib/components/form/TextAreaField.svelte'; | ||
import TextField from '$lib/components/form/TextField.svelte'; | ||
export let id: string; | ||
export let transaction: Transaction; | ||
export let close: () => void; | ||
const transactionsService = useService(TransactionsApi); | ||
const updateTransactionMutation = createMutation({ | ||
mutationFn: (transaction: TransactionSplitUpdate) => | ||
transactionsService.updateTransaction({ | ||
id, | ||
transactionUpdate: { | ||
transactions: [transaction] | ||
} | ||
}), | ||
onSuccess() { | ||
queryClient.invalidateQueries({ | ||
queryKey: ['transactions'] | ||
}); | ||
setTimeout(close, 500); | ||
} | ||
}); | ||
const validated = superValidateSync( | ||
transaction.transactions[0], | ||
transactionSplitSchema | ||
); | ||
const form = superForm(validated, { | ||
SPA: true, | ||
dataType: 'json', | ||
validators: transactionSplitSchema, | ||
onUpdate({ form }) { | ||
if (!form.valid) return; | ||
$updateTransactionMutation.mutate(form.data); | ||
} | ||
}); | ||
const { enhance, errors } = form; | ||
$: console.error($errors); | ||
</script> | ||
|
||
<div class="flex flex-col gap-4" transition:slide|global={{ duration: 300 }}> | ||
<h1 class="text-lg font-bold">Edit transaction</h1> | ||
|
||
<form | ||
use:enhance | ||
method="POST" | ||
id="transaction_update_form" | ||
class="grid grid-cols-12 gap-4" | ||
> | ||
<TextField | ||
{form} | ||
field="description" | ||
label="Description" | ||
placeholder="Description" | ||
class="col-span-4" | ||
/> | ||
<TextField | ||
{form} | ||
type="number" | ||
step="0.01" | ||
field="amount" | ||
label="Amount" | ||
placeholder="Amount" | ||
class="col-span-1" | ||
/> | ||
<TextField {form} field="date" label="Date" placeholder="Date" class="col-span-3" /> | ||
<TextField | ||
{form} | ||
field="sourceName" | ||
label="Source Account" | ||
placeholder="Source Account" | ||
class="col-span-2" | ||
/> | ||
<TextField | ||
{form} | ||
field="destinationName" | ||
label="Destination Account" | ||
placeholder="Destination Account" | ||
class="col-span-2" | ||
/> | ||
|
||
<TextField | ||
{form} | ||
field="categoryName" | ||
label="Category" | ||
placeholder="Category" | ||
class="col-span-2" | ||
/> | ||
<TagsField {form} field="tags" label="Tags" placeholder="Tag" class="col-span-4" /> | ||
<TextAreaField | ||
{form} | ||
field="notes" | ||
class="col-span-6 row-span-3 flex flex-col" | ||
unWrappedClass="flex-grow resize-none" | ||
label="Notes" | ||
/> | ||
<SelectField {form} field="budgetId" label="Budget" class="col-span-2" /> | ||
|
||
<div class="col-span-4 row-span-2"> | ||
<p>Attachments</p> | ||
<p>Not yet implemented :(</p> | ||
</div> | ||
|
||
<SelectField {form} field="billId" label="Bill" class="col-span-2" /> | ||
</form> | ||
|
||
<div class="footer flex gap-2"> | ||
<Button color="red">Delete</Button> | ||
<div class="mx-auto" /> | ||
<Button color="alternative" on:click={close}>Cancel</Button> | ||
<StatusButton | ||
status={$updateTransactionMutation.status} | ||
color="primary" | ||
type="submit" | ||
form="transaction_update_form" | ||
icon="bxs:save" | ||
> | ||
Save | ||
</StatusButton> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { z } from 'zod'; | ||
|
||
export const transactionSplitSchema = z.object({ | ||
transactionJournalId: z.string(), | ||
// type: z.enum([ | ||
// 'withdrawal', | ||
// 'deposit', | ||
// 'transfer', | ||
// 'reconciliation', | ||
// 'opening balance' | ||
// ]), | ||
date: z.date(), | ||
amount: z.string(), | ||
description: z.string(), | ||
// order: z.number().nullish(), | ||
// currencyId: z.string().nullish(), | ||
// currencyCode: z.string().nullish(), | ||
// foreignAmount: z.string().nullish(), | ||
// foreignCurrencyId: z.string().nullish(), | ||
// foreignCurrencyCode: z.string().nullish(), | ||
budgetId: z.string().nullish(), | ||
// categoryId: z.string().nullish(), | ||
categoryName: z.string().nullish(), | ||
// sourceId: z.string().nullish(), | ||
sourceName: z.string().nullish(), | ||
// sourceIban: z.string().nullish(), | ||
// destinationId: z.string().nullish(), | ||
destinationName: z.string().nullish(), | ||
// destinationIban: z.string().nullish(), | ||
// reconciled: z.boolean(), | ||
billId: z.string().nullish(), | ||
// billName: z.string().nullish(), | ||
tags: z.array(z.string()).nullish(), | ||
notes: z.string().nullish() | ||
// internalReference: z.string().nullish(), | ||
// externalId: z.string().nullish(), | ||
// externalUrl: z.string().nullish(), | ||
// bunqPaymentId: z.string().nullish(), | ||
// sepaCc: z.string().nullish(), | ||
// sepaCtOp: z.string().nullish(), | ||
// sepaCtId: z.string().nullish(), | ||
// sepaDb: z.string().nullish(), | ||
// sepaCountry: z.string().nullish(), | ||
// sepaEp: z.string().nullish(), | ||
// sepaCi: z.string().nullish(), | ||
// sepaBatchId: z.string().nullish(), | ||
// interestDate: z.date().nullish(), | ||
// bookDate: z.date().nullish(), | ||
// processDate: z.date().nullish(), | ||
// dueDate: z.date().nullish(), | ||
// paymentDate: z.date().nullish(), | ||
// invoiceDate: z.date().nullish() | ||
}); | ||
|
||
export const transactionSchema = z.object({ | ||
groupTitle: z.string().optional(), | ||
transactions: z.array(transactionSplitSchema) | ||
}); |