simplify entry creation logic
This commit is contained in:
@@ -6,14 +6,20 @@
|
||||
|
||||
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() {
|
||||
if (entry) {
|
||||
await updateEntry(entry);
|
||||
} else {
|
||||
if (isNewEntry) {
|
||||
await createEntry(newEntry);
|
||||
} else {
|
||||
await updateEntry(newEntry);
|
||||
}
|
||||
edit = false;
|
||||
}
|
||||
@@ -68,10 +74,5 @@
|
||||
|
||||
</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 />
|
||||
<TextEditor bind:content={newEntry.content} bind:edit />
|
||||
{/if}
|
||||
<ImageTarget bind:image={newEntry.image} bind:edit />
|
||||
<TextEditor bind:content={newEntry.content} bind:edit />
|
||||
|
||||
@@ -4,9 +4,9 @@ import { httpResponse } from "$lib/server/http";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
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();
|
||||
|
||||
return httpResponse({ message: "Entry deleted successfully" }, 200);
|
||||
|
||||
Reference in New Issue
Block a user