Skip to content

Commit

Permalink
wip: dynamic url test
Browse files Browse the repository at this point in the history
  • Loading branch information
mewdev committed Dec 25, 2024
1 parent 2895c55 commit cbfb930
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ export default function Page({ params }) {
const prevQuestion = useQuestionsStore((state) => state.prevQuestion);
const skipQuestion = useQuestionsStore((state) => state.skipQuestion);
const router = useRouter();
const paramsNumber = Number(params.index);

const prevClick = () => {
if (currentQuestion < 1) {
router.push("/xyz/navod");
} else {
prevQuestion();
router.push(`/xyz/${currentQuestion - 1}`);
router.push(`/xyz/${paramsNumber - 1}`);
}
};

Expand All @@ -24,7 +25,7 @@ export default function Page({ params }) {
router.push("/xyz/rekapitulace");
} else {
skipQuestion();
router.push(`/xyz/${currentQuestion + 1}`);
router.push(`/xyz/${paramsNumber + 1}`);
}
};

Expand All @@ -37,7 +38,7 @@ export default function Page({ params }) {
<QuestionWrapper
key={`Question card id:${question.id}`}
question={question}
currentQuestion={params.index}
currentQuestion={currentQuestion}
questionCount={questions.length}
skipQuestion={skipClick}
prevQuestion={prevClick}
Expand Down
11 changes: 11 additions & 0 deletions apps/web/app/xyz/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useQuestionsStore } from "./store";
import { QuestionWrapper, BottomBar } from "@repo/design-system/ui";
import { useRouter } from "next/navigation";
import type { ExtendedQuestions } from "./store";
import { useEffect } from "react";

export default function Page() {
// cleanup and better naming
Expand All @@ -17,6 +18,16 @@ export default function Page() {
const answerNo = useQuestionsStore((state) => state.answerNo);
const router = useRouter();

// understand this approach more!
useEffect(() => {
if (currentQuestion) {
const slug = `/xyz/${currentQuestion}`;
if (window.location.pathname !== slug) {
window.history.replaceState(null, "", slug);
}
}
}, [currentQuestion]);

const prevClick = () => {
if (currentQuestion === 1) {
router.push("/xyz/navod");
Expand Down

0 comments on commit cbfb930

Please sign in to comment.