Rename patterns/server-action.md to docs/patterns/server-action.md
This commit is contained in:
parent
a72944ecc3
commit
7c41508a24
1 changed files with 0 additions and 0 deletions
53
docs/patterns/server-action.md
Normal file
53
docs/patterns/server-action.md
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
# Patroon: Server Action
|
||||
|
||||
Altijd in `actions/[domein].ts`. Nooit inline in page.tsx.
|
||||
|
||||
```ts
|
||||
'use server'
|
||||
|
||||
import { revalidatePath } from 'next/cache'
|
||||
import { getIronSession } from 'iron-session'
|
||||
import { cookies } from 'next/headers'
|
||||
import { z } from 'zod'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { SessionData, sessionOptions } from '@/lib/session'
|
||||
|
||||
const schema = z.object({
|
||||
productId: z.string().cuid(),
|
||||
title: z.string().min(1).max(200),
|
||||
priority: z.number().int().min(1).max(4),
|
||||
})
|
||||
|
||||
export async function createPbi(formData: FormData) {
|
||||
// 1. Auth
|
||||
const session = await getIronSession<SessionData>(await cookies(), sessionOptions)
|
||||
if (!session.userId) return { error: 'Niet ingelogd' }
|
||||
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus' }
|
||||
|
||||
// 2. Validatie
|
||||
const parsed = schema.safeParse({
|
||||
productId: formData.get('productId'),
|
||||
title: formData.get('title'),
|
||||
priority: Number(formData.get('priority')),
|
||||
})
|
||||
if (!parsed.success) return { error: parsed.error.flatten().fieldErrors }
|
||||
|
||||
// 3. Eigenaarschap controleren
|
||||
const product = await prisma.product.findFirst({
|
||||
where: { id: parsed.data.productId, user_id: session.userId }
|
||||
})
|
||||
if (!product) return { error: 'Product niet gevonden' }
|
||||
|
||||
// 4. Schrijven
|
||||
const last = await prisma.pbi.findFirst({
|
||||
where: { product_id: parsed.data.productId, priority: parsed.data.priority },
|
||||
orderBy: { sort_order: 'desc' },
|
||||
})
|
||||
const pbi = await prisma.pbi.create({
|
||||
data: { ...parsed.data, product_id: parsed.data.productId, sort_order: (last?.sort_order ?? 0) + 1.0 },
|
||||
})
|
||||
|
||||
revalidatePath(`/products/${parsed.data.productId}`)
|
||||
return { success: true, pbi }
|
||||
}
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue