setActiveSprintCookie werd direct aangeroepen in app/(app)/products/[id]/sprint/[sprintId]/page.tsx,
wat in Next.js 16 een runtime-error oplevert ('Cookies can only be modified in a Server Action
or Route Handler'). Vervangen door een client-side bridge die syncActiveSprintCookieAction
aanroept na mount, zodat de active-sprint cookie nog steeds gesynced blijft met de URL.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
63 lines
2 KiB
TypeScript
63 lines
2 KiB
TypeScript
'use server'
|
|
|
|
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 { productAccessFilter } from '@/lib/product-access'
|
|
import { setActiveSprintCookie } from '@/lib/active-sprint'
|
|
|
|
async function getSession() {
|
|
return getIronSession<SessionData>(await cookies(), sessionOptions)
|
|
}
|
|
|
|
const setSchema = z.object({
|
|
productId: z.string().min(1),
|
|
sprintId: z.string().min(1),
|
|
})
|
|
|
|
export async function setActiveSprintAction(productId: string, sprintId: string) {
|
|
const session = await getSession()
|
|
if (!session.userId) return { error: 'Niet ingelogd' }
|
|
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus' }
|
|
|
|
const parsed = setSchema.safeParse({ productId, sprintId })
|
|
if (!parsed.success) return { error: 'Ongeldig product- of sprint-id' }
|
|
|
|
const sprint = await prisma.sprint.findFirst({
|
|
where: {
|
|
id: parsed.data.sprintId,
|
|
product_id: parsed.data.productId,
|
|
product: productAccessFilter(session.userId),
|
|
},
|
|
select: { id: true },
|
|
})
|
|
if (!sprint) return { error: 'Sprint niet gevonden of niet toegankelijk' }
|
|
|
|
await setActiveSprintCookie(parsed.data.productId, parsed.data.sprintId)
|
|
revalidatePath('/', 'layout')
|
|
return { success: true, sprintId: parsed.data.sprintId }
|
|
}
|
|
|
|
export async function syncActiveSprintCookieAction(productId: string, sprintId: string) {
|
|
const session = await getSession()
|
|
if (!session.userId) return
|
|
if (session.isDemo) return
|
|
|
|
const parsed = setSchema.safeParse({ productId, sprintId })
|
|
if (!parsed.success) return
|
|
|
|
const sprint = await prisma.sprint.findFirst({
|
|
where: {
|
|
id: parsed.data.sprintId,
|
|
product_id: parsed.data.productId,
|
|
product: productAccessFilter(session.userId),
|
|
},
|
|
select: { id: true },
|
|
})
|
|
if (!sprint) return
|
|
|
|
await setActiveSprintCookie(parsed.data.productId, parsed.data.sprintId)
|
|
}
|