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 { useRouteQuery } from "@vueuse/router";
import DueDateSingle from "@/components/dueDates/DueDateSingle.vue";
import { getPreviousRoute } from "@/router/history";
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]);
watch(selectedSession, () => {
@ -223,9 +226,9 @@ watch(selectedCourse, async () => {
<div v-else class="bg-gray-200">
<div class="container-large">
<router-link
:to="`/`"
:to="getPreviousRoute() || '/'"
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>
<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
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) => {
// Add the current route to the history, and ensure it doesn't exceed the maximum length
if (isFirstNavigation) {
isFirstNavigation = false;
} else {
} else if (lastNavigationWasPush) {
routeHistory.push(from);
}
if (routeHistory.length > MAX_HISTORY) {
routeHistory.shift();
}
lastNavigationWasPush = false;
next();
};

View File

@ -10,7 +10,7 @@ import {
redirectToLoginIfRequired,
updateLoggedIn,
} from "@/router/guards";
import { addToHistory } from "@/router/history";
import { addToHistory, setLastNavigationWasPush } from "@/router/history";
import { onboardingRedirect } from "@/router/onboarding";
import { createRouter, createWebHistory } from "vue-router";
@ -405,4 +405,18 @@ router.afterEach(
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;