refactor(geschichte): extract delete handler to [id]/+page.svelte, pass via ondelete prop
Moves the confirm-then-delete flow out of StoryReader and JourneyReader into the single [id]/+page.svelte owner. Both reader components gain an optional ondelete prop — the delete button calls ondelete?.() so the handler is opt-in and never duplicated. Tests verify the prop is called on click. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
<script lang="ts">
|
||||
import { goto } from '$app/navigation';
|
||||
import { m } from '$lib/paraglide/messages.js';
|
||||
import { formatDate } from '$lib/shared/utils/date';
|
||||
import { formatAuthorDisplayName } from '$lib/geschichte/utils';
|
||||
import { getConfirmService } from '$lib/shared/services/confirm.svelte';
|
||||
import { csrfFetch } from '$lib/shared/cookies';
|
||||
import BackButton from '$lib/shared/primitives/BackButton.svelte';
|
||||
import StoryReader from '$lib/geschichte/StoryReader.svelte';
|
||||
import JourneyReader from '$lib/geschichte/JourneyReader.svelte';
|
||||
@@ -16,8 +20,23 @@ const publishedAt = $derived.by(() => {
|
||||
return formatDate(g.publishedAt.slice(0, 10), 'long');
|
||||
});
|
||||
|
||||
function authorName(): string {
|
||||
return g.author?.displayName ?? '';
|
||||
const authorName = $derived(formatAuthorDisplayName(g.author));
|
||||
|
||||
const confirm = getConfirmService();
|
||||
|
||||
async function handleDelete() {
|
||||
const ok = await confirm.confirm({
|
||||
title: m.geschichte_delete_confirm_title(),
|
||||
body: m.geschichte_delete_confirm_body(),
|
||||
confirmLabel: m.btn_delete(),
|
||||
cancelLabel: m.btn_cancel(),
|
||||
destructive: true
|
||||
});
|
||||
if (!ok) return;
|
||||
const res = await csrfFetch(`/api/geschichten/${g.id}`, { method: 'DELETE' });
|
||||
if (res.ok) {
|
||||
goto('/geschichten');
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -41,15 +60,15 @@ function authorName(): string {
|
||||
{/if}
|
||||
</div>
|
||||
<p class="font-sans text-sm text-ink-3">
|
||||
{authorName()}
|
||||
{authorName}
|
||||
{#if publishedAt}· {m.geschichten_published_on({ date: publishedAt })}{/if}
|
||||
</p>
|
||||
</header>
|
||||
|
||||
{#if isJourney}
|
||||
<JourneyReader geschichte={g} canBlogWrite={data.canBlogWrite} />
|
||||
<JourneyReader geschichte={g} canBlogWrite={data.canBlogWrite} ondelete={handleDelete} />
|
||||
{:else}
|
||||
<StoryReader geschichte={g} canBlogWrite={data.canBlogWrite} />
|
||||
<StoryReader geschichte={g} canBlogWrite={data.canBlogWrite} ondelete={handleDelete} />
|
||||
{/if}
|
||||
</article>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user