diff --git a/src/app/cards/[id]/edit/page.tsx b/src/app/cards/[id]/edit/page.tsx new file mode 100644 index 0000000..7cc784f --- /dev/null +++ b/src/app/cards/[id]/edit/page.tsx @@ -0,0 +1,45 @@ +import type { Metadata } from 'next' +import Link from 'next/link' +import { notFound } from 'next/navigation' +import { CardForm } from '@/components/card-form' +import type { Card, CardInput } from '@/domain/card' +import { ApiClientError, cardsApi } from '@/lib/api-client' + +export const metadata: Metadata = { title: 'Modifier la carte' } + +export default async function EditCardPage({ + params, +}: { + params: Promise<{ id: string }> +}) { + const { id } = await params + let card: Card | null = null + try { + card = await cardsApi.get(id) + } catch (e) { + if (e instanceof ApiClientError && e.status === 404) notFound() + throw e + } + + const initial: CardInput = { + question: card.question, + response: card.response, + theme: card.theme, + tags: card.tags, + } + + return ( +
+
+ + ← Retour au détail + +

{card.question}

+
+ +
+ ) +} diff --git a/src/components/card-form.test.tsx b/src/components/card-form.test.tsx index e846901..6631281 100644 --- a/src/components/card-form.test.tsx +++ b/src/components/card-form.test.tsx @@ -36,3 +36,25 @@ it('crée une carte puis navigue vers son détail (F-05)', async () => { }) await screen.findByText('Enregistrement…') }) + +it('pré-remplit le formulaire et enregistre une modification (F-06 édition)', async () => { + vi.mocked(cardsApi.update).mockResolvedValue({ id: 'c1' } as never) + render( + , + ) + const question = screen.getByLabelText('Question') + expect(question).toHaveValue('Ancienne question') + await userEvent.clear(question) + await userEvent.type(question, 'Nouvelle question') + await userEvent.click(screen.getByRole('button', { name: 'Enregistrer' })) + expect(vi.mocked(cardsApi.update)).toHaveBeenCalledWith('c1', { + question: 'Nouvelle question', + response: 'Ancienne réponse', + theme: 'git', + }) + expect(routerMock.push).toHaveBeenCalledWith('/cards/c1') +})