Skip to content

Commit

Permalink
wip: testing next dynamic router with questions data
Browse files Browse the repository at this point in the history
  • Loading branch information
mewdev committed Dec 16, 2024
1 parent 01869f8 commit 93aad31
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
41 changes: 41 additions & 0 deletions apps/web/app/[lang]/otazky/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import OtazkaComponent from "../../../temp/OtazkaComponent";

export async function generateStaticParams() {
const res = await fetch(
"https://www.volebnikalkulacka.cz/data/instance/volebnikalkulacka.cz/krajske-2024/10-jihomoravsky/questions.json",
);
const data = await res.json();

return data.map((question) => ({
id: question.id,
}));
}

async function getQuestion(id) {
const res = await fetch(
"https://www.volebnikalkulacka.cz/data/instance/volebnikalkulacka.cz/krajske-2024/10-jihomoravsky/questions.json",
);
const data = await res.json();
const question = data.filter((question) => question.id === id);
return question;
}

export default async function Page({ params }) {
const questionArray = await getQuestion(params.id);
// convert object to array
const convertArrayToObject = (array) => {
if (array.length === 1) {
return array[0];
}
throw "Array containt more than 1 item!";
};
const question = await convertArrayToObject(questionArray);
console.log(questionArray);
return (
<OtazkaComponent
key={question.id}
statement={question.statement}
detail={question.detail}
/>
);
}
35 changes: 35 additions & 0 deletions apps/web/app/[lang]/otazky/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { Question } from "@repo/schema/dist";
import OtazkaComponent from "../../temp/OtazkaComponent";

type Props = {
params: { otazkaId: string };
};

async function getData() {
const res = await fetch(
"https://www.volebnikalkulacka.cz/data/instance/volebnikalkulacka.cz/krajske-2024/10-jihomoravsky/questions.json",
);
const data = await res.json();
return data;
}
export default async function Page({ params }: Props) {
const questions = await getData();
return (
<>
<h1>Otazky page {params.otazkaId}</h1>
<span>ID otázky:</span>
{questions.length > 0 &&
questions.map((item: any) => {
return (
// understand type error here
<OtazkaComponent
key={item.id}
statement={item.statement}
detail={item.detail}
id={item.id}
/>
);
})}
</>
);
}
14 changes: 14 additions & 0 deletions apps/web/app/temp/OtazkaButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use client";
import { useRouter } from "next/navigation";

export default function OtazkaButton({ id }: any) {
const router = useRouter();
function handleClick() {
router.push(`/cs/otazky/${id}`);
}
return (
<button className="block bg-blue-600 p-4 text-white" onClick={handleClick}>
Go to question
</button>
);
}
11 changes: 11 additions & 0 deletions apps/web/app/temp/OtazkaComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import OtazkaButton from "./OtazkaButton";

export default function OtazkaComponent({ id, statement, detail }: any) {
return (
<div className="mb-2 border-4 border-red-500 bg-slate-800 p-4 text-white">
<h1>{statement}</h1>
<span>{detail}</span>
<OtazkaButton id={id} />
</div>
);
}

0 comments on commit 93aad31

Please sign in to comment.