simplify entry creation logic
This commit is contained in:
@@ -6,14 +6,20 @@
|
|||||||
|
|
||||||
let { newEntryDate, entry = $bindable(), edit = $bindable(false) } = $props();
|
let { newEntryDate, entry = $bindable(), edit = $bindable(false) } = $props();
|
||||||
|
|
||||||
let newEntry = $state({ date: newEntryDate, image: "", content: "" });
|
let isNewEntry = !entry && newEntryDate
|
||||||
|
|
||||||
|
let newEntry = $state({
|
||||||
|
date: isNewEntry ? newEntryDate : entry.date,
|
||||||
|
image: isNewEntry ? "" : entry.image,
|
||||||
|
content: isNewEntry ? "" : entry.content
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
async function saveEntry() {
|
async function saveEntry() {
|
||||||
if (entry) {
|
if (isNewEntry) {
|
||||||
await updateEntry(entry);
|
|
||||||
} else {
|
|
||||||
await createEntry(newEntry);
|
await createEntry(newEntry);
|
||||||
|
} else {
|
||||||
|
await updateEntry(newEntry);
|
||||||
}
|
}
|
||||||
edit = false;
|
edit = false;
|
||||||
}
|
}
|
||||||
@@ -68,10 +74,5 @@
|
|||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{#if entry}
|
|
||||||
<ImageTarget bind:image={entry.image} bind:edit />
|
|
||||||
<TextEditor bind:content={entry.content} bind:edit />
|
|
||||||
{:else}
|
|
||||||
<ImageTarget bind:image={newEntry.image} bind:edit />
|
<ImageTarget bind:image={newEntry.image} bind:edit />
|
||||||
<TextEditor bind:content={newEntry.content} bind:edit />
|
<TextEditor bind:content={newEntry.content} bind:edit />
|
||||||
{/if}
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { httpResponse } from "$lib/server/http";
|
|||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
|
|
||||||
export async function POST({ request }) {
|
export async function POST({ request }) {
|
||||||
const { id, content, image } = await request.json();
|
const { id } = await request.json();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await db.delete(entryTable).where(eq(entryTable.id, id)).execute();
|
await db.delete(entryTable).where(eq(entryTable.id, id)).execute();
|
||||||
|
|||||||
Reference in New Issue
Block a user