| 1 |
<script lang="ts"> |
| 2 |
import { saveEntries } from "../api"; |
| 3 |
import { Balance, Note, Transaction } from "../entries"; |
| 4 |
import Entry from "../entry-forms/Entry.svelte"; |
| 5 |
import { todayAsString } from "../format"; |
| 6 |
import { _ } from "../i18n"; |
| 7 |
import { addEntryContinue } from "../stores/editor"; |
| 8 |
import { closeOverlay, urlHash } from "../stores/url"; |
| 9 |
import ModalBase from "./ModalBase.svelte"; |
| 10 |
|
| 11 |
/** The entry types which have support adding in the modal. */ |
| 12 |
const entryTypes = [ |
| 13 |
[Transaction, _("Transaction")], |
| 14 |
[Balance, _("Balance")], |
| 15 |
[Note, _("Note")], |
| 16 |
] as const; |
| 17 |
|
| 18 |
// For the first entry to be added, use today as the default date. |
| 19 |
let entry: Transaction | Balance | Note = $state.raw( |
| 20 |
Transaction.empty(todayAsString()), |
| 21 |
); |
| 22 |
|
| 23 |
async function submit(event: SubmitEvent) { |
| 24 |
event.preventDefault(); |
| 25 |
await saveEntries([entry]); |
| 26 |
const added_entry_date = entry.date; |
| 27 |
// Reuse the date of the entry that was just added. |
| 28 |
// @ts-expect-error all these entries have that static method, but TS is not able to determine that |
| 29 |
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call |
| 30 |
entry = entry.constructor.empty(added_entry_date); |
| 31 |
if (!$addEntryContinue) { |
| 32 |
closeOverlay(); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
let shown = $derived($urlHash === "add-transaction"); |
| 37 |
</script> |
| 38 |
|
| 39 |
<ModalBase {shown} focus=".payee input"> |
| 40 |
<form onsubmit={submit}> |
| 41 |
<h3> |
| 42 |
{_("Add")} |
| 43 |
{#each entryTypes as [Cls, displayName]} |
| 44 |
<button |
| 45 |
type="button" |
| 46 |
class:muted={!(entry instanceof Cls)} |
| 47 |
onclick={() => { |
| 48 |
// when switching between entry types, keep the date. |
| 49 |
entry = Cls.empty(entry.date); |
| 50 |
}} |
| 51 |
> |
| 52 |
{displayName} |
| 53 |
</button> |
| 54 |
<!-- eslint-disable-next-line svelte/no-useless-mustaches --> |
| 55 |
{" "} |
| 56 |
{/each} |
| 57 |
</h3> |
| 58 |
<Entry bind:entry /> |
| 59 |
<div class="flex-row"> |
| 60 |
<span class="spacer"></span> |
| 61 |
<label> |
| 62 |
<input type="checkbox" bind:checked={$addEntryContinue} /> |
| 63 |
<span>{_("continue")}</span> |
| 64 |
</label> |
| 65 |
<button type="submit">{_("Save")}</button> |
| 66 |
</div> |
| 67 |
</form> |
| 68 |
</ModalBase> |
| 69 |
|
| 70 |
<style> |
| 71 |
label span { |
| 72 |
margin-right: 1rem; |
| 73 |
} |
| 74 |
</style> |