feat(story-dialog): conform aan dialog-pattern + AlertDialog delete
Story 4 van PBI "Alle dialogen conform docs/patterns/dialog.md". - lib/schemas/story.ts — gedeeld zod-schema - actions/stories.ts — code+fieldErrors voor 422; code: 403 voor auth/demo - StoryDialog adopt useDirtyCloseGuard, useDialogSubmitShortcut, entityDialog* layout-classes - Inline delete-confirm vervangen door AlertDialog (§10.4) - docs/specs/dialogs/story.md — gaps weggewerkt; alleen bewuste afwijkingen blijven (header met badges, geen char-counter) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
97dc4ee553
commit
01e77fc560
5 changed files with 240 additions and 167 deletions
|
|
@ -3,18 +3,20 @@
|
|||
import { revalidatePath } from 'next/cache'
|
||||
import { cookies } from 'next/headers'
|
||||
import { getIronSession } from 'iron-session'
|
||||
import { z } from 'zod'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { SessionData, sessionOptions } from '@/lib/session'
|
||||
import { getAccessibleProduct, productAccessFilter } from '@/lib/product-access'
|
||||
import { requireProductWriter } from '@/lib/auth'
|
||||
import { isValidCode, MAX_CODE_LENGTH, normalizeCode } from '@/lib/code'
|
||||
import { isValidCode, normalizeCode } from '@/lib/code'
|
||||
import { createWithCodeRetry, generateNextStoryCode } from '@/lib/code-server'
|
||||
import { createStorySchema, updateStorySchema } from '@/lib/schemas/story'
|
||||
|
||||
async function getSession() {
|
||||
return getIronSession<SessionData>(await cookies(), sessionOptions)
|
||||
}
|
||||
|
||||
type StoryFieldErrors = Record<string, string[]>
|
||||
|
||||
async function verifyStoryAccess(storyId: string, userId: string) {
|
||||
return prisma.story.findFirst({
|
||||
where: { id: storyId, product: productAccessFilter(userId) },
|
||||
|
|
@ -26,31 +28,10 @@ function hasDuplicateIds(ids: string[]) {
|
|||
return new Set(ids).size !== ids.length
|
||||
}
|
||||
|
||||
const codeField = z.string().max(MAX_CODE_LENGTH).optional()
|
||||
|
||||
const createStorySchema = z.object({
|
||||
pbiId: z.string(),
|
||||
productId: z.string(),
|
||||
code: codeField,
|
||||
title: z.string().min(1, 'Titel is verplicht').max(200),
|
||||
description: z.string().max(2000).optional(),
|
||||
acceptance_criteria: z.string().max(2000).optional(),
|
||||
priority: z.coerce.number().int().min(1).max(4),
|
||||
})
|
||||
|
||||
const updateStorySchema = z.object({
|
||||
id: z.string(),
|
||||
code: codeField,
|
||||
title: z.string().min(1, 'Titel is verplicht').max(200),
|
||||
description: z.string().max(2000).optional(),
|
||||
acceptance_criteria: z.string().max(2000).optional(),
|
||||
priority: z.coerce.number().int().min(1).max(4),
|
||||
})
|
||||
|
||||
export async function createStoryAction(_prevState: unknown, formData: FormData) {
|
||||
const session = await getSession()
|
||||
if (!session.userId) return { error: 'Niet ingelogd' }
|
||||
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus' }
|
||||
if (!session.userId) return { error: 'Niet ingelogd', code: 403 }
|
||||
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus', code: 403 }
|
||||
|
||||
const parsed = createStorySchema.safeParse({
|
||||
pbiId: formData.get('pbiId'),
|
||||
|
|
@ -61,20 +42,36 @@ export async function createStoryAction(_prevState: unknown, formData: FormData)
|
|||
acceptance_criteria: formData.get('acceptance_criteria') || undefined,
|
||||
priority: formData.get('priority') ?? 2,
|
||||
})
|
||||
if (!parsed.success) return { error: parsed.error.flatten().fieldErrors }
|
||||
if (!parsed.success) {
|
||||
return {
|
||||
error: 'Validatie mislukt',
|
||||
code: 422,
|
||||
fieldErrors: parsed.error.flatten().fieldErrors as StoryFieldErrors,
|
||||
}
|
||||
}
|
||||
|
||||
const pbi = await prisma.pbi.findFirst({
|
||||
where: { id: parsed.data.pbiId, product: productAccessFilter(session.userId) },
|
||||
})
|
||||
if (!pbi) return { error: 'PBI niet gevonden' }
|
||||
if (!pbi) return { error: 'PBI niet gevonden', code: 403 }
|
||||
|
||||
const manualCode = normalizeCode(parsed.data.code)
|
||||
if (manualCode !== null && !isValidCode(manualCode)) {
|
||||
return { error: { code: ['Code mag alleen letters, cijfers, punten, koppeltekens of underscores bevatten'] } }
|
||||
return {
|
||||
error: 'Validatie mislukt',
|
||||
code: 422,
|
||||
fieldErrors: { code: ['Alleen letters, cijfers, punten, koppeltekens of underscores'] } as StoryFieldErrors,
|
||||
}
|
||||
}
|
||||
if (manualCode) {
|
||||
const dup = await prisma.story.findFirst({ where: { product_id: pbi.product_id, code: manualCode } })
|
||||
if (dup) return { error: { code: ['Deze code is al in gebruik binnen dit product'] } }
|
||||
if (dup) {
|
||||
return {
|
||||
error: 'Validatie mislukt',
|
||||
code: 422,
|
||||
fieldErrors: { code: ['Deze code is al in gebruik binnen dit product'] } as StoryFieldErrors,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const last = await prisma.story.findFirst({
|
||||
|
|
@ -107,7 +104,11 @@ export async function createStoryAction(_prevState: unknown, formData: FormData)
|
|||
(code) => insert(code),
|
||||
)
|
||||
} catch {
|
||||
return { error: { code: ['Kon geen unieke code genereren — probeer opnieuw'] } }
|
||||
return {
|
||||
error: 'Validatie mislukt',
|
||||
code: 422,
|
||||
fieldErrors: { code: ['Kon geen unieke code genereren — probeer opnieuw'] } as StoryFieldErrors,
|
||||
}
|
||||
}
|
||||
|
||||
revalidatePath(`/products/${pbi.product_id}`)
|
||||
|
|
@ -116,8 +117,8 @@ export async function createStoryAction(_prevState: unknown, formData: FormData)
|
|||
|
||||
export async function updateStoryAction(_prevState: unknown, formData: FormData) {
|
||||
const session = await getSession()
|
||||
if (!session.userId) return { error: 'Niet ingelogd' }
|
||||
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus' }
|
||||
if (!session.userId) return { error: 'Niet ingelogd', code: 403 }
|
||||
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus', code: 403 }
|
||||
|
||||
const parsed = updateStorySchema.safeParse({
|
||||
id: formData.get('id'),
|
||||
|
|
@ -127,20 +128,36 @@ export async function updateStoryAction(_prevState: unknown, formData: FormData)
|
|||
acceptance_criteria: formData.get('acceptance_criteria') || undefined,
|
||||
priority: formData.get('priority'),
|
||||
})
|
||||
if (!parsed.success) return { error: parsed.error.flatten().fieldErrors }
|
||||
if (!parsed.success) {
|
||||
return {
|
||||
error: 'Validatie mislukt',
|
||||
code: 422,
|
||||
fieldErrors: parsed.error.flatten().fieldErrors as StoryFieldErrors,
|
||||
}
|
||||
}
|
||||
|
||||
const story = await verifyStoryAccess(parsed.data.id, session.userId)
|
||||
if (!story) return { error: 'Story niet gevonden' }
|
||||
if (!story) return { error: 'Story niet gevonden', code: 403 }
|
||||
|
||||
const code = normalizeCode(parsed.data.code)
|
||||
if (code !== null && !isValidCode(code)) {
|
||||
return { error: { code: ['Code mag alleen letters, cijfers, punten, koppeltekens of underscores bevatten'] } }
|
||||
return {
|
||||
error: 'Validatie mislukt',
|
||||
code: 422,
|
||||
fieldErrors: { code: ['Alleen letters, cijfers, punten, koppeltekens of underscores'] } as StoryFieldErrors,
|
||||
}
|
||||
}
|
||||
if (code) {
|
||||
const dup = await prisma.story.findFirst({
|
||||
where: { product_id: story.product_id, code, NOT: { id: parsed.data.id } },
|
||||
})
|
||||
if (dup) return { error: { code: ['Deze code is al in gebruik binnen dit product'] } }
|
||||
if (dup) {
|
||||
return {
|
||||
error: 'Validatie mislukt',
|
||||
code: 422,
|
||||
fieldErrors: { code: ['Deze code is al in gebruik binnen dit product'] } as StoryFieldErrors,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await prisma.story.update({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue