# Les 6 — Lesopdracht ## QuickPoll Part 2 — Server Actions + polish **Duur:** ~120 minuten klassikaal --- ## Doel Aan het einde heeft QuickPoll: - Server actions voor vote submission - Een /create pagina voor nieuwe polls - Loading + error states - Optimistic updates --- ## Stap 1 — Server Action voor voting `app/poll/[id]/actions.ts`: ```typescript "use server"; import { polls } from "@/lib/data"; import { revalidatePath } from "next/cache"; export async function vote(pollId: string, optionId: string) { const poll = polls.find(p => p.id === pollId); if (!poll) throw new Error("Poll niet gevonden"); const option = poll.options.find(o => o.id === optionId); if (!option) throw new Error("Optie niet gevonden"); option.votes += 1; revalidatePath(`/poll/${pollId}`); } ``` --- ## Stap 2 — VoteForm met server action Refactor `components/VoteForm.tsx` om `vote()` server action te gebruiken in plaats van useState: ```tsx "use client"; import { vote } from "@/app/poll/[id]/actions"; import { useFormStatus } from "react-dom"; function VoteButton({ option }) { const { pending } = useFormStatus(); return ( ); } export function VoteForm({ poll }) { return (
{poll.options.map(o => (
))}
); } ``` --- ## Stap 3 — /create pagina `app/create/page.tsx`: ```tsx import { createPoll } from "./actions"; export default function CreatePage() { return (

Nieuwe poll