Improve history management

This commit is contained in:
Daniel Egger 2024-04-19 15:28:19 +02:00
parent 7847191d97
commit 41b0d3ae4d
3 changed files with 38 additions and 5 deletions

View File

@ -8,6 +8,7 @@ import { useTranslation } from "i18next-vue";
import _ from "lodash"; import _ from "lodash";
import { useRouteQuery } from "@vueuse/router"; import { useRouteQuery } from "@vueuse/router";
import DueDateSingle from "@/components/dueDates/DueDateSingle.vue"; import DueDateSingle from "@/components/dueDates/DueDateSingle.vue";
import { getPreviousRoute } from "@/router/history";
log.debug("DashboardPersonsPage created"); log.debug("DashboardPersonsPage created");
@ -75,7 +76,9 @@ const courseSessions = computed(() => {
]; ];
}); });
const selectedSessionRouteQuery = useRouteQuery("session"); const selectedSessionRouteQuery = useRouteQuery("session", UNFILTERED, {
mode: "replace",
});
const selectedSession = ref<DropboxItem>(courseSessions.value[0]); const selectedSession = ref<DropboxItem>(courseSessions.value[0]);
watch(selectedSession, () => { watch(selectedSession, () => {
@ -223,9 +226,9 @@ watch(selectedCourse, async () => {
<div v-else class="bg-gray-200"> <div v-else class="bg-gray-200">
<div class="container-large"> <div class="container-large">
<router-link <router-link
:to="`/`" :to="getPreviousRoute() || '/'"
class="btn-text inline-flex items-center p-0" class="btn-text inline-flex items-center p-0"
data-cy="back-to-learning-path-button" data-cy="back-button"
> >
<it-icon-arrow-left class="-ml-1 mr-1 h-5 w-5"></it-icon-arrow-left> <it-icon-arrow-left class="-ml-1 mr-1 h-5 w-5"></it-icon-arrow-left>
<span class="inline">{{ $t("general.back") }}</span> <span class="inline">{{ $t("general.back") }}</span>

View File

@ -10,16 +10,32 @@ const routeHistory: RouteLocationNormalized[] = [];
const MAX_HISTORY = 10; // for example, store the last 10 visited routes const MAX_HISTORY = 10; // for example, store the last 10 visited routes
let isFirstNavigation = true; let isFirstNavigation = true;
// Variable to track the type of the last navigation
let lastNavigationWasPush = false;
// Function to set the state
export function setLastNavigationWasPush(value: boolean) {
lastNavigationWasPush = value;
}
// Function to get the current state
export function getLastNavigationWasPush() {
return lastNavigationWasPush;
}
export const addToHistory: NavigationGuard = (to, from, next) => { export const addToHistory: NavigationGuard = (to, from, next) => {
// Add the current route to the history, and ensure it doesn't exceed the maximum length // Add the current route to the history, and ensure it doesn't exceed the maximum length
if (isFirstNavigation) { if (isFirstNavigation) {
isFirstNavigation = false; isFirstNavigation = false;
} else { } else if (lastNavigationWasPush) {
routeHistory.push(from); routeHistory.push(from);
} }
if (routeHistory.length > MAX_HISTORY) { if (routeHistory.length > MAX_HISTORY) {
routeHistory.shift(); routeHistory.shift();
} }
lastNavigationWasPush = false;
next(); next();
}; };

View File

@ -10,7 +10,7 @@ import {
redirectToLoginIfRequired, redirectToLoginIfRequired,
updateLoggedIn, updateLoggedIn,
} from "@/router/guards"; } from "@/router/guards";
import { addToHistory } from "@/router/history"; import { addToHistory, setLastNavigationWasPush } from "@/router/history";
import { onboardingRedirect } from "@/router/onboarding"; import { onboardingRedirect } from "@/router/onboarding";
import { createRouter, createWebHistory } from "vue-router"; import { createRouter, createWebHistory } from "vue-router";
@ -405,4 +405,18 @@ router.afterEach(
router.beforeEach(addToHistory); router.beforeEach(addToHistory);
// Wrap router.replace to track when it's called
const originalReplace = router.replace;
router.replace = function (to) {
setLastNavigationWasPush(false);
return originalReplace.call(this, to);
};
// Wrap router.push to track when it's called
const originalPush = router.push;
router.push = function (to) {
setLastNavigationWasPush(true);
return originalPush.call(this, to);
};
export default router; export default router;