import { useEffect, useState } from 'react'; import { useClient } from 'hooks/useClient'; import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; import { useAutosave } from '../useAutosave'; import type { Collection } from './collections'; /** * What somebody says about a collection, in the editor column. * * ── Everything here is what a FOLDER cannot tell you ── * * Which photographs are in a collection is a question about `originals/COLLECTIONS//`, answered * by reading it. This is the other half: the name it goes by, the address it lives at, and the specs * from the client's sitemap. None of it can be derived, so all of it is typed. * * The gallery is not here. It is chosen beside the photographs it puts on screen, in the panel, where * the consequence of choosing it is visible — a dropdown in a column of text fields would be the one * control whose effect you cannot see. * * ── One draft, one save ── * * Every field writes into one draft object and `useAutosave` sends it whole. Per-field saves would be * eight requests for somebody filling in a form, and the last one to land would win in an order nobody * controls. */ /** The fields, in the order the client's sitemap lists them. */ const SPECS: [keyof Collection, string, string][] = [ ['descriptive_text', 'Descriptive text', 'What this collection is'], ['measurement', 'Measurement', '45cm x 45cm'], ['material', 'Raw material', 'Acrylic yarn'], ['colors', 'Colours', ''], ['delivery_time', 'Delivery time', '4-6 weeks'], ]; export const CollectionFields = ({ collection, onSaved, }: { collection: Collection; onSaved: () => void; }) => { const client = useClient(); const [draft, setDraft] = useState>({}); // Keyed on the ID, not the values: keyed on the values it would fight the typing, pulling every // keystroke back to the last saved one until the save landed. useEffect(() => setDraft({}), [collection.id]); const value = (key: keyof Collection): string => (draft[key] as string | null | undefined) ?? (collection[key] as string | null) ?? ''; const dirty = Object.keys(draft).length > 0; useAutosave(dirty ? JSON.stringify(draft) : null, false, async () => { await client.patch(`/fofos-admin/collections/${collection.id}`, draft); setDraft({}); onSaved(); }); const set = (key: keyof Collection) => (next: string) => setDraft((was) => ({ ...was, [key]: next })); return (
set('title')(event.target.value)} />
set('slug')(event.target.value)} />

/collections/{value('slug')}