fix: use router guards for refetching projects and versions, closes #1148

This commit is contained in:
MiniDigger | Martin 2023-04-22 19:27:12 +02:00
parent 6e83f6b689
commit db9cfa3b47
3 changed files with 24 additions and 23 deletions

View File

@ -20,7 +20,7 @@ export async function useProjectPage(route: RouteLocationNormalizedLoaded, route
}
async function savePage(content: string) {
if (!page) return;
if (!page?.value) return;
await useInternalApi(`pages/save/${project.id}/${page.value?.id}`, "post", {
content,
}).catch((e) => handleRequestError(e, "page.new.error.save"));

View File

@ -1,14 +1,14 @@
<script lang="ts" setup>
import { type PropType, provide, watch } from "vue";
import { type PropType, provide } from "vue";
import { User } from "hangar-api";
import { useI18n } from "vue-i18n";
import { useRoute } from "vue-router";
import { RouteLocationNormalized, useRoute } from "vue-router";
import { HangarProjectPage, HangarProject } from "hangar-internal";
import { useProject } from "~/composables/useApiHelper";
import { useErrorRedirect } from "~/composables/useErrorRedirect";
import ProjectHeader from "~/components/projects/ProjectHeader.vue";
import ProjectNav from "~/components/projects/ProjectNav.vue";
import { createError, navigateTo, useInternalApi } from "#imports";
import { createError, navigateTo, onBeforeRouteUpdate, useInternalApi } from "#imports";
import Delayed from "~/components/design/Delayed.vue";
defineProps({
@ -21,26 +21,26 @@ defineProps({
const i18n = useI18n();
const route = useRoute();
const project = await useProject(route.params.user as string, route.params.project as string);
await verify();
await verify(route);
async function verify() {
async function verify(to: RouteLocationNormalized) {
if (!project?.value) {
throw useErrorRedirect(route, 404, "Not found");
} else if (route.params.project !== project.value?.namespace.slug) {
const newPath = route.fullPath.replace(route.params.project as string, project.value!.namespace.slug);
console.debug("Redirect to " + newPath + " from (" + route.fullPath + ")");
throw useErrorRedirect(to, 404, "Not found");
} else if (to.params.project !== project.value?.namespace.slug) {
const newPath = to.fullPath.replace(to.params.project as string, project.value!.namespace.slug);
console.debug("Redirect to " + newPath + " from (" + to.fullPath + ")");
await navigateTo(newPath);
throw createError("dummy");
}
}
watch(
() => route.params.project,
async () => {
project.value = await useInternalApi<HangarProject>("projects/project/" + route.params.user + "/" + route.params.project);
await verify();
}
);
onBeforeRouteUpdate(async (to) => {
if (!to.params.project || !to.params.user) return;
console.log("before project update");
project.value = await useInternalApi<HangarProject>("projects/project/" + to.params.user + "/" + to.params.project);
console.log("after project update", project.value?.mainPage);
await verify(to);
});
provide("updateProjectPages", function (pages: HangarProjectPage[]) {
if (project && project.value) project.value!.pages = pages;

View File

@ -1,13 +1,14 @@
<script lang="ts" setup>
import { useI18n } from "vue-i18n";
import { useRoute } from "vue-router";
import { computed, watch } from "vue";
import { computed } from "vue";
import { HangarProject, HangarVersion } from "hangar-internal";
import { useProjectVersionsInternal } from "~/composables/useApiHelper";
import { useErrorRedirect } from "~/composables/useErrorRedirect";
import { Platform } from "~/types/enums";
import Delayed from "~/components/design/Delayed.vue";
import { useInternalApi } from "~/composables/useApi";
import { onBeforeRouteUpdate } from "#imports";
const i18n = useI18n();
const route = useRoute();
@ -25,11 +26,11 @@ function verify() {
}
}
watch(
() => route.params.version,
async () =>
(version.value = await useInternalApi<HangarVersion>(`versions/version/${route.params.user}/${route.params.project}/versions/${route.params.version}`))
);
onBeforeRouteUpdate(async (to) => {
if (!to.params.version || !to.params.project || !to.params.user) return;
version.value = await useInternalApi<HangarVersion>(`versions/version/${to.params.user}/${to.params.project}/versions/${to.params.version}`);
await verify();
});
const versionPlatforms = computed<Set<Platform>>(() => {
const result = new Set<Platform>();