From 73b10e6f8c405cb3e75c19549940316e4a449ec8 Mon Sep 17 00:00:00 2001 From: Marcel Date: Thu, 11 Jun 2026 08:33:41 +0200 Subject: [PATCH] refactor(journey-editor): extract appendItem from the two add handlers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit handleAddDocument/handleAddInterlude were identical except the POST body. Behavior unchanged — all 35 editor specs stay green. Review round 3: Felix suggestion. Co-Authored-By: Claude Fable 5 --- .../src/lib/geschichte/JourneyEditor.svelte | 31 ++++++------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/frontend/src/lib/geschichte/JourneyEditor.svelte b/frontend/src/lib/geschichte/JourneyEditor.svelte index 82ff80d7..516aaa8e 100644 --- a/frontend/src/lib/geschichte/JourneyEditor.svelte +++ b/frontend/src/lib/geschichte/JourneyEditor.svelte @@ -125,13 +125,14 @@ async function handleReorder(itemIds: string[]) { } } -async function handleAddDocument(doc: DocumentOption) { +/** Pessimistic append shared by both add paths — items update only on API success. */ +async function appendItem(body: { documentId?: string; note?: string }) { mutationError = ''; try { const res = await csrfFetch(`/api/geschichten/${geschichte.id}/items`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ documentId: doc.id }) + body: JSON.stringify(body) }); if (!res.ok) { mutationError = await failureMessage(res); @@ -142,31 +143,17 @@ async function handleAddDocument(doc: DocumentOption) { // Move-up is disabled on the first row — fall back to the remove button then. await focusRowControl(newItem.id, '[data-move-up]:not([disabled]), [data-remove-btn]'); } catch (e) { - console.error('Journey add document failed', e); + console.error('Journey item append failed', e); mutationError = m.journey_mutation_error_reload(); } } +async function handleAddDocument(doc: DocumentOption) { + await appendItem({ documentId: doc.id }); +} + async function handleAddInterlude(text: string) { - mutationError = ''; - try { - const res = await csrfFetch(`/api/geschichten/${geschichte.id}/items`, { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ note: text }) - }); - if (!res.ok) { - mutationError = await failureMessage(res); - return; - } - const newItem: JourneyItemView = await res.json(); - items = [...items, newItem]; - // Move-up is disabled on the first row — fall back to the remove button then. - await focusRowControl(newItem.id, '[data-move-up]:not([disabled]), [data-remove-btn]'); - } catch (e) { - console.error('Journey add interlude failed', e); - mutationError = m.journey_mutation_error_reload(); - } + await appendItem({ note: text }); } async function handleRemove(itemId: string) {