diff --git a/actions/todos.ts b/actions/todos.ts index 6a7be3f..2561d9f 100644 --- a/actions/todos.ts +++ b/actions/todos.ts @@ -14,11 +14,20 @@ async function getSession() { export async function createTodoAction(_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' } const title = (formData.get('title') as string)?.trim() - if (!title) return { error: 'Titel is verplicht' } + const productId = (formData.get('productId') as string)?.trim() - await prisma.todo.create({ data: { user_id: session.userId, title } }) + if (!title) return { error: 'Titel is verplicht' } + if (!productId) return { error: 'Product is verplicht' } + + const product = await prisma.product.findFirst({ + where: { id: productId, user_id: session.userId, archived: false }, + }) + if (!product) return { error: 'Product niet gevonden' } + + await prisma.todo.create({ data: { user_id: session.userId, product_id: productId, title } }) revalidatePath('/todos') return { success: true } } diff --git a/app/(app)/todos/page.tsx b/app/(app)/todos/page.tsx index 9aebb39..8c63e09 100644 --- a/app/(app)/todos/page.tsx +++ b/app/(app)/todos/page.tsx @@ -10,6 +10,7 @@ export default async function TodosPage() { const todos = await prisma.todo.findMany({ where: { user_id: session.userId, archived: false }, orderBy: { created_at: 'asc' }, + include: { product: { select: { name: true } } }, }) const products = await prisma.product.findMany({ @@ -24,7 +25,14 @@ export default async function TodosPage() {
Maak eerst een product aan.
) : ( -