-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
110 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
"use client"; | ||
import { useEffect } from "react"; | ||
import { useQuestionsStore } from "../providers/storeProvider"; | ||
import { usePathname } from "next/navigation"; | ||
|
||
type Props = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
export default function UrlUpdater({ children }: Props) { | ||
const path = usePathname(); | ||
const currentQuestion = useQuestionsStore((state) => state.currentQuestion); | ||
const setIsRekapitulace = useQuestionsStore( | ||
(state) => state.setIsRekapitulace, | ||
); | ||
const setCurrentLocation = useQuestionsStore( | ||
(state) => state.setCurrentLocation, | ||
); | ||
const guideNumber = useQuestionsStore((state) => state.guideNumber); | ||
const currentLocation = useQuestionsStore((state) => state.currentLocation); | ||
|
||
useEffect(() => { | ||
// store location setter | ||
function locationSetter() { | ||
if (path.includes("rekapitulace")) { | ||
setCurrentLocation("rekapitulace"); | ||
setIsRekapitulace(true); | ||
} else if (path.includes("otazka")) { | ||
setCurrentLocation("otazka"); | ||
setIsRekapitulace(false); | ||
} else if (path.includes("navod")) { | ||
setCurrentLocation("navod"); | ||
} else if (path.includes("vysledky")) { | ||
setCurrentLocation("vysledky"); | ||
setIsRekapitulace(false); | ||
} | ||
} | ||
// url setter | ||
function changeUrl() { | ||
// insert conditionals here for edge cases? | ||
if (currentLocation === "otazka" && currentQuestion !== null) { | ||
// refactor url structure | ||
history.replaceState({}, "", `/abc/otazka/${currentQuestion}`); | ||
} else if (currentLocation === "navod") { | ||
// refactor url structure | ||
history.replaceState({}, "", `/abc/navod/${guideNumber}`); | ||
} else if (currentLocation === "rekapitulace") { | ||
history.replaceState({}, "", `/abc/rekapitulace`); | ||
} else if (currentLocation === "vysledky") { | ||
history.replaceState({}, "", `/abc/vysledky`); | ||
} | ||
} | ||
// title setter | ||
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 if (currentLocation === "rekapitulace") { | ||
document.title = `Rekapitulace`; | ||
} else if (currentLocation === "vysledky") { | ||
document.title = `Výsledky`; | ||
} | ||
} | ||
|
||
locationSetter(); | ||
changeTitle(); | ||
changeUrl(); | ||
}, [path, currentQuestion, guideNumber, currentLocation]); | ||
|
||
return children; | ||
} | ||
|
||
// TODO: | ||
// 1. needs refactoring (url updates glitch) |