From 7829734b06d548d43964b55eb3b49e2cee1eaafa Mon Sep 17 00:00:00 2001 From: mewdev Date: Thu, 9 Jan 2025 14:43:11 +0100 Subject: [PATCH] wip: implement dynamic question fetching and update URL handling --- apps/web/app/abc/layout.tsx | 6 ++++-- apps/web/app/abc/utils/getQuestions.tsx | 5 +++++ apps/web/app/abc/utils/urlUpdater.tsx | 3 +++ 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 apps/web/app/abc/utils/getQuestions.tsx diff --git a/apps/web/app/abc/layout.tsx b/apps/web/app/abc/layout.tsx index ce58b3a..43dd640 100644 --- a/apps/web/app/abc/layout.tsx +++ b/apps/web/app/abc/layout.tsx @@ -3,15 +3,17 @@ import "@repo/design-system/styles"; import "@repo/design-system/themes/theme-default"; import { StoreProvider } from "./providers/storeProvider"; import UrlUpdater from "./utils/urlUpdater"; +import getQuestions from "./utils/getQuestions"; -const collection = ["Collection 1", "Collection 2", "Collection 3"]; +const baseUrl = "https://dummyjson.com/c/7ee4-a7f4-4977-bb54"; + +const collection = await getQuestions(baseUrl); export default async function RootLayout({ children, }: { children: React.ReactNode; }) { - console.log(collection); return ( diff --git a/apps/web/app/abc/utils/getQuestions.tsx b/apps/web/app/abc/utils/getQuestions.tsx new file mode 100644 index 0000000..aaa0d44 --- /dev/null +++ b/apps/web/app/abc/utils/getQuestions.tsx @@ -0,0 +1,5 @@ +export default async function getQuestions(baseUrl: string) { + const res = await fetch(baseUrl); + const data = await res.json(); + return data; +} diff --git a/apps/web/app/abc/utils/urlUpdater.tsx b/apps/web/app/abc/utils/urlUpdater.tsx index cc6e335..f6f6efa 100644 --- a/apps/web/app/abc/utils/urlUpdater.tsx +++ b/apps/web/app/abc/utils/urlUpdater.tsx @@ -10,12 +10,15 @@ export default function UrlUpdater({ children }: Props) { const currentQuestion = useQuestionsStore((state) => state.currentQuestion); useEffect(() => { + // cleanups ? // change url function changeUrl() { + // insert conditionals here for edge cases? history.replaceState({}, "", `/abc/${currentQuestion}`); } // change title function changeTitle() { + // insert conditionals here for edge cases? document.title = `Otázka ${currentQuestion}`; } changeTitle();