From 974c9cdd218c1f26ac23637571c659193ff9dc40 Mon Sep 17 00:00:00 2001 From: kolaente Date: Tue, 19 Mar 2024 14:39:10 +0100 Subject: [PATCH] fix(views): always redirect to the first view when none was specified --- frontend/src/views/project/ProjectView.vue | 27 ++++++++++++++-------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/frontend/src/views/project/ProjectView.vue b/frontend/src/views/project/ProjectView.vue index 7d8162382..055cdecd9 100644 --- a/frontend/src/views/project/ProjectView.vue +++ b/frontend/src/views/project/ProjectView.vue @@ -25,25 +25,34 @@ const currentView = computed(() => { return project?.views.find(v => v.id === viewId) }) -watch( - () => viewId, - () => { - if (viewId === 0) { - // Ideally, we would do that in the router redirect, but we the projects (and therefore, the views) - // are not always loaded then. - const viewId = projectStore.projects[projectId].views[0].id +function redirectToFirstViewIfNecessary() { + if (viewId === 0) { + // Ideally, we would do that in the router redirect, but the projects (and therefore, the views) + // are not always loaded then. + const firstViewId = projectStore.projects[projectId]?.views[0].id + if (firstViewId) { router.replace({ name: 'project.view', params: { projectId, - viewId, + viewId: firstViewId, }, }) } - }, + } +} + +watch( + () => viewId, + redirectToFirstViewIfNecessary, {immediate: true}, ) +watch( + () => projectStore.projects[projectId], + redirectToFirstViewIfNecessary, +) + const route = useRoute()