fix: les 6
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getPollById } from "@/lib/data";
|
||||
|
||||
interface RouteParams {
|
||||
params: Promise<{ id: string }>;
|
||||
}
|
||||
|
||||
// GET /api/polls/[id] — enkele poll ophalen
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: RouteParams
|
||||
): Promise<NextResponse> {
|
||||
const { id } = await params;
|
||||
const poll = getPollById(id);
|
||||
|
||||
if (!poll) {
|
||||
return NextResponse.json(
|
||||
{ error: "Poll niet gevonden" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(poll);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { votePoll } from "@/lib/data";
|
||||
|
||||
interface RouteParams {
|
||||
params: Promise<{ id: string }>;
|
||||
}
|
||||
|
||||
interface VoteBody {
|
||||
optionIndex: number;
|
||||
}
|
||||
|
||||
// POST /api/polls/[id]/vote — stem uitbrengen
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{ params }: RouteParams
|
||||
): Promise<NextResponse> {
|
||||
const { id } = await params;
|
||||
const body: VoteBody = await request.json();
|
||||
|
||||
if (typeof body.optionIndex !== "number") {
|
||||
return NextResponse.json(
|
||||
{ error: "optionIndex is verplicht" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const updatedPoll = votePoll(id, body.optionIndex);
|
||||
|
||||
if (!updatedPoll) {
|
||||
return NextResponse.json(
|
||||
{ error: "Poll niet gevonden of ongeldige optie" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(updatedPoll);
|
||||
}
|
||||
24
Les05-NextJS-Basics/quickpoll 2/src/app/api/polls/route.ts
Normal file
24
Les05-NextJS-Basics/quickpoll 2/src/app/api/polls/route.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getPolls, createPoll } from "@/lib/data";
|
||||
import type { Poll, CreatePollBody } from "@/types";
|
||||
|
||||
// GET /api/polls — alle polls ophalen
|
||||
export async function GET(): Promise<NextResponse<Poll[]>> {
|
||||
const polls = getPolls();
|
||||
return NextResponse.json(polls);
|
||||
}
|
||||
|
||||
// POST /api/polls — nieuwe poll aanmaken
|
||||
export async function POST(request: Request): Promise<NextResponse> {
|
||||
const body: CreatePollBody = await request.json();
|
||||
|
||||
if (!body.question || !body.options || body.options.length < 2) {
|
||||
return NextResponse.json(
|
||||
{ error: "Vraag en minstens 2 opties zijn verplicht" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const newPoll = createPoll(body.question, body.options);
|
||||
return NextResponse.json(newPoll, { status: 201 });
|
||||
}
|
||||
144
Les05-NextJS-Basics/quickpoll 2/src/app/create/page.tsx
Normal file
144
Les05-NextJS-Basics/quickpoll 2/src/app/create/page.tsx
Normal file
@@ -0,0 +1,144 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
export default function CreatePollPage() {
|
||||
const [question, setQuestion] = useState<string>("");
|
||||
const [options, setOptions] = useState<string[]>(["", ""]);
|
||||
const [isSubmitting, setIsSubmitting] = useState<boolean>(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const router = useRouter();
|
||||
|
||||
function addOption(): void {
|
||||
if (options.length < 6) {
|
||||
setOptions([...options, ""]);
|
||||
}
|
||||
}
|
||||
|
||||
function removeOption(index: number): void {
|
||||
if (options.length > 2) {
|
||||
setOptions(options.filter((_, i) => i !== index));
|
||||
}
|
||||
}
|
||||
|
||||
function updateOption(index: number, value: string): void {
|
||||
const newOptions = [...options];
|
||||
newOptions[index] = value;
|
||||
setOptions(newOptions);
|
||||
}
|
||||
|
||||
async function handleSubmit(e: React.FormEvent<HTMLFormElement>): Promise<void> {
|
||||
e.preventDefault();
|
||||
setError(null);
|
||||
|
||||
const filledOptions = options.filter((opt) => opt.trim() !== "");
|
||||
if (!question.trim() || filledOptions.length < 2) {
|
||||
setError("Vul een vraag in en minstens 2 opties");
|
||||
return;
|
||||
}
|
||||
|
||||
setIsSubmitting(true);
|
||||
|
||||
const response = await fetch("/api/polls", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
question: question.trim(),
|
||||
options: filledOptions,
|
||||
}),
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
router.push("/");
|
||||
} else {
|
||||
setError("Er ging iets mis bij het aanmaken van de poll");
|
||||
}
|
||||
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="max-w-2xl mx-auto">
|
||||
<h1 className="text-2xl font-bold text-gray-900 mb-6">
|
||||
Nieuwe Poll Aanmaken
|
||||
</h1>
|
||||
|
||||
<form onSubmit={handleSubmit} className="space-y-6">
|
||||
<div>
|
||||
<label
|
||||
htmlFor="question"
|
||||
className="block text-sm font-medium text-gray-700 mb-2"
|
||||
>
|
||||
Vraag
|
||||
</label>
|
||||
<input
|
||||
id="question"
|
||||
type="text"
|
||||
value={question}
|
||||
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
|
||||
setQuestion(e.target.value)
|
||||
}
|
||||
placeholder="Stel je vraag..."
|
||||
className="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent outline-none"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
Opties (minimaal 2, maximaal 6)
|
||||
</label>
|
||||
<div className="space-y-3">
|
||||
{options.map((option, index) => (
|
||||
<div key={index} className="flex gap-2">
|
||||
<input
|
||||
type="text"
|
||||
value={option}
|
||||
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
|
||||
updateOption(index, e.target.value)
|
||||
}
|
||||
placeholder={`Optie ${index + 1}`}
|
||||
className="flex-1 px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent outline-none"
|
||||
/>
|
||||
{options.length > 2 && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => removeOption(index)}
|
||||
className="px-3 text-red-500 hover:bg-red-50 rounded-lg transition-colors"
|
||||
>
|
||||
✕
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{options.length < 6 && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={addOption}
|
||||
className="mt-3 text-sm text-purple-600 hover:text-purple-800 font-medium"
|
||||
>
|
||||
+ Optie toevoegen
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<p className="text-red-600 text-sm bg-red-50 p-3 rounded-lg">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isSubmitting}
|
||||
className="w-full bg-purple-600 text-white py-3 rounded-lg font-medium hover:bg-purple-700 disabled:bg-gray-300 disabled:cursor-not-allowed transition-colors"
|
||||
>
|
||||
{isSubmitting ? "Bezig met aanmaken..." : "Poll Aanmaken"}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
24
Les05-NextJS-Basics/quickpoll 2/src/app/error.tsx
Normal file
24
Les05-NextJS-Basics/quickpoll 2/src/app/error.tsx
Normal file
@@ -0,0 +1,24 @@
|
||||
"use client";
|
||||
|
||||
export default function Error({
|
||||
error,
|
||||
reset,
|
||||
}: {
|
||||
error: Error;
|
||||
reset: () => void;
|
||||
}) {
|
||||
return (
|
||||
<div className="text-center py-16">
|
||||
<h2 className="text-2xl font-bold text-red-600 mb-4">
|
||||
Er ging iets mis!
|
||||
</h2>
|
||||
<p className="text-gray-600 mb-6">{error.message}</p>
|
||||
<button
|
||||
onClick={() => reset()}
|
||||
className="bg-purple-600 text-white px-6 py-3 rounded-lg hover:bg-purple-700 transition-colors"
|
||||
>
|
||||
Probeer opnieuw
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
BIN
Les05-NextJS-Basics/quickpoll 2/src/app/favicon.ico
Normal file
BIN
Les05-NextJS-Basics/quickpoll 2/src/app/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 25 KiB |
1
Les05-NextJS-Basics/quickpoll 2/src/app/globals.css
Normal file
1
Les05-NextJS-Basics/quickpoll 2/src/app/globals.css
Normal file
@@ -0,0 +1 @@
|
||||
@import "tailwindcss";
|
||||
46
Les05-NextJS-Basics/quickpoll 2/src/app/layout.tsx
Normal file
46
Les05-NextJS-Basics/quickpoll 2/src/app/layout.tsx
Normal file
@@ -0,0 +1,46 @@
|
||||
import type { Metadata } from "next";
|
||||
import Link from "next/link";
|
||||
import "./globals.css";
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "QuickPoll — Stem op alles",
|
||||
description: "Maak en deel polls met je vrienden",
|
||||
};
|
||||
|
||||
export default function RootLayout({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
return (
|
||||
<html lang="nl">
|
||||
<body className="min-h-screen bg-gray-50 text-gray-900">
|
||||
<nav className="bg-white border-b border-gray-200 shadow-sm">
|
||||
<div className="max-w-4xl mx-auto px-4 py-4 flex items-center justify-between">
|
||||
<Link href="/" className="text-xl font-bold text-purple-600">
|
||||
🗳️ QuickPoll
|
||||
</Link>
|
||||
<div className="flex gap-4 items-center">
|
||||
<Link
|
||||
href="/"
|
||||
className="text-gray-600 hover:text-purple-600 transition-colors"
|
||||
>
|
||||
Polls
|
||||
</Link>
|
||||
<Link
|
||||
href="/create"
|
||||
className="bg-purple-600 text-white px-4 py-2 rounded-lg hover:bg-purple-700 transition-colors text-sm font-medium"
|
||||
>
|
||||
Nieuwe Poll
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<main className="max-w-4xl mx-auto px-4 py-8">{children}</main>
|
||||
<footer className="text-center text-gray-400 text-sm py-8">
|
||||
© 2025 QuickPoll — NOVI Hogeschool Les 5
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
}
|
||||
24
Les05-NextJS-Basics/quickpoll 2/src/app/loading.tsx
Normal file
24
Les05-NextJS-Basics/quickpoll 2/src/app/loading.tsx
Normal file
@@ -0,0 +1,24 @@
|
||||
export default function Loading() {
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="animate-pulse">
|
||||
<div className="h-8 bg-gray-200 rounded w-1/3 mb-2" />
|
||||
<div className="h-4 bg-gray-200 rounded w-1/2 mb-8" />
|
||||
</div>
|
||||
{[1, 2, 3].map((i) => (
|
||||
<div
|
||||
key={i}
|
||||
className="animate-pulse bg-white rounded-xl border border-gray-200 p-6"
|
||||
>
|
||||
<div className="h-5 bg-gray-200 rounded w-3/4 mb-3" />
|
||||
<div className="h-4 bg-gray-200 rounded w-1/4 mb-3" />
|
||||
<div className="flex gap-2">
|
||||
<div className="h-6 bg-gray-100 rounded-full w-20" />
|
||||
<div className="h-6 bg-gray-100 rounded-full w-24" />
|
||||
<div className="h-6 bg-gray-100 rounded-full w-16" />
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
18
Les05-NextJS-Basics/quickpoll 2/src/app/not-found.tsx
Normal file
18
Les05-NextJS-Basics/quickpoll 2/src/app/not-found.tsx
Normal file
@@ -0,0 +1,18 @@
|
||||
import Link from "next/link";
|
||||
|
||||
export default function NotFound() {
|
||||
return (
|
||||
<div className="text-center py-16">
|
||||
<h2 className="text-4xl font-bold text-gray-900 mb-4">404</h2>
|
||||
<p className="text-gray-600 mb-6">
|
||||
Deze pagina bestaat niet (meer).
|
||||
</p>
|
||||
<Link
|
||||
href="/"
|
||||
className="bg-purple-600 text-white px-6 py-3 rounded-lg hover:bg-purple-700 transition-colors inline-block"
|
||||
>
|
||||
Terug naar home
|
||||
</Link>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
57
Les05-NextJS-Basics/quickpoll 2/src/app/page.tsx
Normal file
57
Les05-NextJS-Basics/quickpoll 2/src/app/page.tsx
Normal file
@@ -0,0 +1,57 @@
|
||||
import Link from "next/link";
|
||||
import { getPolls } from "@/lib/data";
|
||||
import type { Poll } from "@/types";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export default function HomePage() {
|
||||
const polls: Poll[] = getPolls();
|
||||
|
||||
const totalVotes = (poll: Poll): number =>
|
||||
poll.votes.reduce((sum, v) => sum + v, 0);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold text-gray-900 mb-2">Actieve Polls</h1>
|
||||
<p className="text-gray-500 mb-8">Klik op een poll om te stemmen</p>
|
||||
|
||||
<div className="grid gap-4">
|
||||
{polls.map((poll) => (
|
||||
<Link
|
||||
key={poll.id}
|
||||
href={`/poll/${poll.id}`}
|
||||
className="block bg-white rounded-xl border border-gray-200 p-6 hover:border-purple-300 hover:shadow-md transition-all"
|
||||
>
|
||||
<h2 className="text-lg font-semibold text-gray-900 mb-2">
|
||||
{poll.question}
|
||||
</h2>
|
||||
<div className="flex items-center gap-4 text-sm text-gray-500">
|
||||
<span>{poll.options.length} opties</span>
|
||||
<span>·</span>
|
||||
<span>{totalVotes(poll)} stemmen</span>
|
||||
</div>
|
||||
<div className="flex flex-wrap gap-2 mt-3">
|
||||
{poll.options.map((option, index) => (
|
||||
<span
|
||||
key={index}
|
||||
className="bg-gray-100 text-gray-600 px-3 py-1 rounded-full text-sm"
|
||||
>
|
||||
{option}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
</Link>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{polls.length === 0 && (
|
||||
<div className="text-center py-16 text-gray-400">
|
||||
<p className="text-lg">Nog geen polls</p>
|
||||
<Link href="/create" className="text-purple-600 hover:underline">
|
||||
Maak de eerste!
|
||||
</Link>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import Link from "next/link";
|
||||
|
||||
export default function PollNotFound() {
|
||||
return (
|
||||
<div className="text-center py-16">
|
||||
<h2 className="text-2xl font-bold text-gray-900 mb-4">
|
||||
Poll niet gevonden
|
||||
</h2>
|
||||
<p className="text-gray-600 mb-6">
|
||||
Deze poll bestaat niet of is verwijderd.
|
||||
</p>
|
||||
<Link
|
||||
href="/"
|
||||
className="bg-purple-600 text-white px-6 py-3 rounded-lg hover:bg-purple-700 transition-colors inline-block"
|
||||
>
|
||||
Bekijk alle polls
|
||||
</Link>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
40
Les05-NextJS-Basics/quickpoll 2/src/app/poll/[id]/page.tsx
Normal file
40
Les05-NextJS-Basics/quickpoll 2/src/app/poll/[id]/page.tsx
Normal file
@@ -0,0 +1,40 @@
|
||||
import { notFound } from "next/navigation";
|
||||
import { getPollById } from "@/lib/data";
|
||||
import VoteForm from "@/components/VoteForm";
|
||||
import type { Metadata } from "next";
|
||||
|
||||
interface PageProps {
|
||||
params: Promise<{ id: string }>;
|
||||
}
|
||||
|
||||
export async function generateMetadata({ params }: PageProps): Promise<Metadata> {
|
||||
const { id } = await params;
|
||||
const poll = getPollById(id);
|
||||
|
||||
if (!poll) {
|
||||
return { title: "Poll niet gevonden" };
|
||||
}
|
||||
|
||||
return {
|
||||
title: `${poll.question} — QuickPoll`,
|
||||
description: `Stem op: ${poll.options.join(", ")}`,
|
||||
};
|
||||
}
|
||||
|
||||
export default async function PollPage({ params }: PageProps) {
|
||||
const { id } = await params;
|
||||
const poll = getPollById(id);
|
||||
|
||||
if (!poll) {
|
||||
notFound();
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="max-w-2xl mx-auto">
|
||||
<h1 className="text-2xl font-bold text-gray-900 mb-6">
|
||||
{poll.question}
|
||||
</h1>
|
||||
<VoteForm poll={poll} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
128
Les05-NextJS-Basics/quickpoll 2/src/components/VoteForm.tsx
Normal file
128
Les05-NextJS-Basics/quickpoll 2/src/components/VoteForm.tsx
Normal file
@@ -0,0 +1,128 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import type { Poll } from "@/types";
|
||||
|
||||
interface VoteFormProps {
|
||||
poll: Poll;
|
||||
}
|
||||
|
||||
export default function VoteForm({ poll }: VoteFormProps) {
|
||||
const [selectedOption, setSelectedOption] = useState<number | null>(null);
|
||||
const [hasVoted, setHasVoted] = useState<boolean>(false);
|
||||
const [isSubmitting, setIsSubmitting] = useState<boolean>(false);
|
||||
const [currentPoll, setCurrentPoll] = useState<Poll>(poll);
|
||||
const router = useRouter();
|
||||
|
||||
const totalVotes: number = currentPoll.votes.reduce(
|
||||
(sum, v) => sum + v,
|
||||
0
|
||||
);
|
||||
|
||||
async function handleVote(): Promise<void> {
|
||||
if (selectedOption === null || isSubmitting) return;
|
||||
|
||||
setIsSubmitting(true);
|
||||
|
||||
const response = await fetch(`/api/polls/${currentPoll.id}/vote`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ optionIndex: selectedOption }),
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const updatedPoll: Poll = await response.json();
|
||||
setCurrentPoll(updatedPoll);
|
||||
setHasVoted(true);
|
||||
}
|
||||
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
|
||||
function getPercentage(votes: number): number {
|
||||
if (totalVotes === 0) return 0;
|
||||
return Math.round((votes / totalVotes) * 100);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
{currentPoll.options.map((option, index) => {
|
||||
const percentage = getPercentage(currentPoll.votes[index]);
|
||||
const isSelected = selectedOption === index;
|
||||
|
||||
return (
|
||||
<button
|
||||
key={index}
|
||||
onClick={() => !hasVoted && setSelectedOption(index)}
|
||||
disabled={hasVoted}
|
||||
className={`w-full text-left p-4 rounded-lg border-2 transition-all relative overflow-hidden ${
|
||||
hasVoted
|
||||
? "border-gray-200 cursor-default"
|
||||
: isSelected
|
||||
? "border-purple-500 bg-purple-50"
|
||||
: "border-gray-200 hover:border-purple-300 cursor-pointer"
|
||||
}`}
|
||||
>
|
||||
{hasVoted && (
|
||||
<div
|
||||
className="absolute inset-0 bg-purple-100 transition-all duration-500"
|
||||
style={{ width: `${percentage}%` }}
|
||||
/>
|
||||
)}
|
||||
<div className="relative flex justify-between items-center">
|
||||
<div className="flex items-center gap-3">
|
||||
{!hasVoted && (
|
||||
<div
|
||||
className={`w-5 h-5 rounded-full border-2 flex items-center justify-center ${
|
||||
isSelected
|
||||
? "border-purple-500 bg-purple-500"
|
||||
: "border-gray-300"
|
||||
}`}
|
||||
>
|
||||
{isSelected && (
|
||||
<div className="w-2 h-2 rounded-full bg-white" />
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
<span className="font-medium">{option}</span>
|
||||
</div>
|
||||
{hasVoted && (
|
||||
<span className="text-sm font-semibold text-purple-700">
|
||||
{percentage}% ({currentPoll.votes[index]} stemmen)
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
|
||||
{!hasVoted && (
|
||||
<button
|
||||
onClick={handleVote}
|
||||
disabled={selectedOption === null || isSubmitting}
|
||||
className="w-full bg-purple-600 text-white py-3 rounded-lg font-medium hover:bg-purple-700 disabled:bg-gray-300 disabled:cursor-not-allowed transition-colors mt-4"
|
||||
>
|
||||
{isSubmitting ? "Bezig met stemmen..." : "Stem!"}
|
||||
</button>
|
||||
)}
|
||||
|
||||
{hasVoted && (
|
||||
<div className="text-center pt-4">
|
||||
<p className="text-green-600 font-medium mb-2">
|
||||
Bedankt voor je stem!
|
||||
</p>
|
||||
<p className="text-sm text-gray-500">
|
||||
Totaal: {totalVotes} stemmen
|
||||
</p>
|
||||
<button
|
||||
onClick={() => router.push("/")}
|
||||
className="mt-4 text-purple-600 hover:underline text-sm"
|
||||
>
|
||||
← Terug naar alle polls
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
55
Les05-NextJS-Basics/quickpoll 2/src/lib/data.ts
Normal file
55
Les05-NextJS-Basics/quickpoll 2/src/lib/data.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { Poll } from "@/types";
|
||||
|
||||
export const polls: Poll[] = [
|
||||
{
|
||||
id: "1",
|
||||
question: "Wat is de beste code editor?",
|
||||
options: ["VS Code", "Cursor", "Vim", "WebStorm"],
|
||||
votes: [12, 25, 5, 3],
|
||||
},
|
||||
{
|
||||
id: "2",
|
||||
question: "Wat is de beste programmeertaal?",
|
||||
options: ["TypeScript", "Python", "Rust", "Go"],
|
||||
votes: [18, 15, 8, 4],
|
||||
},
|
||||
{
|
||||
id: "3",
|
||||
question: "Welk framework heeft de toekomst?",
|
||||
options: ["Next.js", "Remix", "Astro", "SvelteKit"],
|
||||
votes: [22, 6, 10, 7],
|
||||
},
|
||||
];
|
||||
|
||||
let nextId = 4;
|
||||
|
||||
export function getPolls(): Poll[] {
|
||||
return polls;
|
||||
}
|
||||
|
||||
export function getPollById(id: string): Poll | undefined {
|
||||
return polls.find((poll) => poll.id === id);
|
||||
}
|
||||
|
||||
export function createPoll(question: string, options: string[]): Poll {
|
||||
const newPoll: Poll = {
|
||||
id: String(nextId++),
|
||||
question,
|
||||
options,
|
||||
votes: new Array(options.length).fill(0),
|
||||
};
|
||||
polls.push(newPoll);
|
||||
return newPoll;
|
||||
}
|
||||
|
||||
export function votePoll(
|
||||
pollId: string,
|
||||
optionIndex: number
|
||||
): Poll | undefined {
|
||||
const poll = polls.find((p) => p.id === pollId);
|
||||
if (!poll || optionIndex < 0 || optionIndex >= poll.options.length) {
|
||||
return undefined;
|
||||
}
|
||||
poll.votes[optionIndex]++;
|
||||
return poll;
|
||||
}
|
||||
17
Les05-NextJS-Basics/quickpoll 2/src/middleware.ts
Normal file
17
Les05-NextJS-Basics/quickpoll 2/src/middleware.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
|
||||
export function middleware(request: NextRequest): NextResponse {
|
||||
const start = Date.now();
|
||||
|
||||
console.log(`[${request.method}] ${request.nextUrl.pathname}`);
|
||||
|
||||
const response = NextResponse.next();
|
||||
response.headers.set("x-request-time", String(Date.now() - start));
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: ["/api/:path*", "/poll/:path*"],
|
||||
};
|
||||
11
Les05-NextJS-Basics/quickpoll 2/src/types/index.ts
Normal file
11
Les05-NextJS-Basics/quickpoll 2/src/types/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export interface Poll {
|
||||
id: string;
|
||||
question: string;
|
||||
options: string[];
|
||||
votes: number[];
|
||||
}
|
||||
|
||||
export interface CreatePollBody {
|
||||
question: string;
|
||||
options: string[];
|
||||
}
|
||||
Reference in New Issue
Block a user