fix:#18 reroute for default page only applies to empty paths

This commit is contained in:
Elmar Kresse
2024-07-21 22:21:14 +02:00
parent 059b8ca24b
commit 5330ade54e
8 changed files with 80 additions and 23 deletions

View File

@@ -30,14 +30,22 @@ const CourseSelection = () => import("../view/CourseSelection.vue");
const FreeRooms = () => import("../view/FreeRooms.vue");
const CalenderViewer = () => import("../view/UserCalendar.vue");
const SettingsView = () => import("../view/SettingsView.vue");
const NotFound = () => import("../view/NotFound.vue");
import i18n from "../i18n";
import settingsStore from "@/store/settingsStore.ts";
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: "/",
name: "default",
component: CourseSelection,
},
{
path: "/home",
name: "home",
component: CourseSelection,
},
@@ -123,18 +131,31 @@ const router = createRouter({
path: "/settings",
name: "settings",
component: SettingsView,
}
},
{
path: "/:catchAll(.*)", // Catch all undefined routes
name: "not-found",
component: NotFound, // Replace with your NotFound component
},
],
});
router.beforeEach(async (to, from) => {
router.beforeEach(async (to, from, next) => {
const newLocale = to.params.locale;
const prevLocale = from.params.locale;
// If the locale hasn't changed, do nothing
if (newLocale === prevLocale) {
return;
if (!(newLocale === prevLocale)) {
i18n.setLocale(newLocale);
}
const userSettings = settingsStore();
const defaultPath = userSettings.defaultPage || "/home";
if (to.path === "/") {
next(defaultPath.value);
} else {
next();
}
i18n.setLocale(newLocale);
});
export default router;