Skip to content

Commit

Permalink
wip: improve edge case handling in question and guide navigation logi…
Browse files Browse the repository at this point in the history
…c, refactored questionswrapper, improved urlUpdater
  • Loading branch information
mewdev committed Jan 20, 2025
1 parent 93a58f6 commit 1f233a7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 23 deletions.
32 changes: 27 additions & 5 deletions apps/web/app/abc/providers/storeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,40 @@ export const StoreProvider = ({ children, questions }: StoreProviderProps) => {
});
return { ...state, questions: updatedQuestion };
}),
// add edge cases
// check edge cases
prevQuestion: () =>
set((state) => ({ currentQuestion: state.currentQuestion - 1 })),
set((state) => ({
currentQuestion:
state.currentQuestion !== null && state.currentQuestion !== 0
? state.currentQuestion - 1
: null,
})),
skipQuestion: () =>
set((state) => ({ currentQuestion: state.currentQuestion + 1 })),
set((state) => ({
currentQuestion:
state.currentQuestion !== null &&
state.currentQuestion !== questions.length
? state.currentQuestion + 1
: null,
})),
guideNumber: null,
guide: guide,
setGuideNumber: (guideNumber) =>
set(() => ({ guideNumber: guideNumber })),
prevGuide: () => set((state) => ({ guideNumber: state.guideNumber - 1 })),
prevGuide: () =>
set((state) => ({
guideNumber:
state.guideNumber !== null && state.guideNumber !== 0
? state.guideNumber - 1
: null,
})),
nextGuide: () => {
set((state) => ({ guideNumber: state.guideNumber + 1 }));
set((state) => ({
guideNumber:
state.guideNumber !== null && state.guideNumber !== guide.length
? state.guideNumber + 1
: null,
}));
},
setCurrentQuestion: (number) => set(() => ({ currentQuestion: number })),
}));
Expand Down
40 changes: 28 additions & 12 deletions apps/web/app/abc/utils/urlUpdater.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
"use client";
import { useEffect } from "react";
import { useQuestionsStore } from "../providers/storeProvider";
import { usePathname } from "next/navigation";
import { usePathname, useRouter } from "next/navigation";

type Props = {
children: React.ReactNode;
};

export default function UrlUpdater({ children }: Props) {
const path = usePathname();
const router = useRouter();
const currentQuestion = useQuestionsStore((state) => state.currentQuestion);
const rekapitulace = useQuestionsStore((state) => state.isRekapitulace);
const setIsRekapitulace = useQuestionsStore(
(state) => state.setIsRekapitulace,
);
Expand All @@ -19,43 +21,57 @@ export default function UrlUpdater({ children }: Props) {
const guideNumber = useQuestionsStore((state) => state.guideNumber);
const currentLocation = useQuestionsStore((state) => state.currentLocation);

// location setter
// is slow, make a better approach?
// Location setter
useEffect(() => {
if (path.includes("rekapitulace")) {
if (
path.includes("rekapitulace") &&
rekapitulace !== true &&
currentLocation !== "rekapitulace"
) {
setIsRekapitulace(true);
setCurrentLocation("rekapitulace");
} else if (path.includes("otazka")) {
} else if (
path.includes("otazka") &&
rekapitulace !== false &&
currentLocation !== "otazka"
) {
setIsRekapitulace(false);
setCurrentLocation("otazka");
} else if (path.includes("navod")) {
} else if (path.includes("navod") && currentLocation !== "navod") {
setCurrentLocation("navod");
}
}, [path]);

// Url updater
useEffect(() => {
function changeUrl() {
// insert conditionals here for edge cases?
if (currentLocation === "otazka") {
// refactor url structure
history.replaceState({}, "", `/abc/otazka/${currentQuestion}`);
} else if (currentLocation === "navod") {
// refactor url structure
history.replaceState({}, "", `/abc/navod/${guideNumber}`);
// edge case if currentLocation null or undefined
} else if (currentLocation === undefined || currentLocation === null) {
router.push("/");
}
}
changeUrl();
}, [currentQuestion, guideNumber]);

useEffect(() => {
// change title
function changeTitle() {
if (currentLocation === "otazka") {
// refactor title structure
document.title = `Otázka ${currentQuestion}`;
} else if (currentLocation === "navod") {
// refactor title structure
document.title = `Návod ${guideNumber}`;
} else {
// default title
document.title;
}
}
changeTitle();
}, [currentLocation]);
changeUrl();
}, [currentQuestion, guideNumber, currentLocation]);

return children;
}
12 changes: 6 additions & 6 deletions packages/design-system/src/ui/layout/questionWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ export function QuestionWrapper({
// fix k1 prefix issue!!!
>
{currentQuestion === 1 ? (
<span className="k1-hidden lg:k1-inline">
<Link href="/abc/navod/1">Návod</Link>
</span>
<Link href="/abc/navod/1">
<span className="k1-hidden lg:k1-inline">Návod</span>
</Link>
) : (
<span className="k1-hidden md:k1-block">
Předchozí{" "}
Expand Down Expand Up @@ -107,9 +107,9 @@ export function QuestionWrapper({
onClick={skipQuestion}
>
{currentQuestion >= questionCount ? (
<span className="k1-hidden lg:k1-inline">
<Link href="/abc/rekapitulace">Rekapitulace</Link>
</span>
<Link href="/abc/rekapitulace">
<span className="k1-hidden lg:k1-inline">Rekapitulace</span>
</Link>
) : (
<span className="k1-hidden md:k1-block">
Přeskočit{" "}
Expand Down

0 comments on commit 1f233a7

Please sign in to comment.