import React from 'react' import { Head, Link, router, useForm, usePage } from '@inertiajs/react' function getCsrfToken() { if (typeof document === 'undefined') return '' return document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || '' } async function requestJson(url, { method = 'POST', body } = {}) { const response = await fetch(url, { method, credentials: 'same-origin', headers: { Accept: 'application/json', 'Content-Type': 'application/json', 'X-CSRF-TOKEN': getCsrfToken(), 'X-Requested-With': 'XMLHttpRequest', }, body: body ? JSON.stringify(body) : undefined, }) const payload = await response.json().catch(() => ({})) if (!response.ok) { throw new Error(payload?.message || payload?.errors?.story?.[0] || Object.values(payload?.errors || {})?.[0]?.[0] || 'Request failed.') } return payload } function replacePagePattern(pattern, pageId) { return String(pattern || '').replace('__PAGE__', String(pageId)) } function Field({ label, children, hint }) { return ( ) } function StoryPageCard({ page, endpoints, onChanged }) { const [localPage, setLocalPage] = React.useState(page) const [busy, setBusy] = React.useState(false) const [error, setError] = React.useState('') React.useEffect(() => { setLocalPage(page) }, [page]) async function save() { setBusy(true) setError('') try { await requestJson(replacePagePattern(endpoints.pagesUpdatePattern, page.id), { method: 'PATCH', body: { ...localPage, overlay_strength: Number(localPage.overlay_strength || 35), active: Boolean(localPage.active), }, }) onChanged() } catch (requestError) { setError(requestError.message || 'Unable to save page.') } finally { setBusy(false) } } async function destroy() { setBusy(true) setError('') try { await requestJson(replacePagePattern(endpoints.pagesDestroyPattern, page.id), { method: 'DELETE' }) onChanged() } catch (requestError) { setError(requestError.message || 'Unable to delete page.') } finally { setBusy(false) } } return (
Page {page.position}
{page.headline || 'Untitled page'}
{error ?
{error}
: null}
setLocalPage((current) => ({ ...current, headline: event.target.value }))} className="w-full rounded-xl border border-white/10 bg-slate-950/70 px-3 py-2 text-white outline-none" /> setLocalPage((current) => ({ ...current, caption: event.target.value }))} className="w-full rounded-xl border border-white/10 bg-slate-950/70 px-3 py-2 text-white outline-none" />