use /a/{id} as redirect

This commit is contained in:
Hayden 2023-01-14 09:18:03 -09:00
parent 60db244c66
commit a5c59243c4
No known key found for this signature in database
GPG key ID: 17CF79474E257545
2 changed files with 43 additions and 34 deletions

View file

@ -4,39 +4,6 @@
}); });
const route = useRoute(); const route = useRoute();
const api = useUserApi();
const toast = useNotifier();
const assetId = computed<string>(() => route.params.id as string); const assetId = computed<string>(() => route.params.id as string);
await navigateTo("/assets/" + assetId.value, { replace: true, redirectCode: 301 });
const { pending, data: items } = useLazyAsyncData(`asset/${assetId.value}`, async () => {
const { data, error } = await api.assets.get(assetId.value);
if (error) {
toast.error("Failed to load asset");
navigateTo("/home");
return;
}
switch (data.total) {
case 0:
toast.error("Asset not found");
navigateTo("/home");
break;
case 1:
navigateTo(`/item/${data.items[0].id}`);
break;
default:
return data.items;
}
});
</script> </script>
<template>
<BaseContainer>
<section v-if="!pending">
<BaseSectionHeader class="mb-5"> This Asset Id is associated with multiple items</BaseSectionHeader>
<div class="grid gap-2 grid-cols-1 sm:grid-cols-2">
<ItemCard v-for="item in items" :key="item.id" :item="item" />
</div>
</section>
</BaseContainer>
</template>

View file

@ -0,0 +1,42 @@
<script setup lang="ts">
definePageMeta({
middleware: ["auth"],
});
const route = useRoute();
const api = useUserApi();
const toast = useNotifier();
const assetId = computed<string>(() => route.params.id as string);
const { pending, data: items } = useLazyAsyncData(`asset/${assetId.value}`, async () => {
const { data, error } = await api.assets.get(assetId.value);
if (error) {
toast.error("Failed to load asset");
navigateTo("/home");
return;
}
switch (data.total) {
case 0:
toast.error("Asset not found");
navigateTo("/home");
break;
case 1:
navigateTo(`/item/${data.items[0].id}`, { replace: true, redirectCode: 302 });
break;
default:
return data.items;
}
});
</script>
<template>
<BaseContainer>
<section v-if="!pending">
<BaseSectionHeader class="mb-5"> This Asset Id is associated with multiple items</BaseSectionHeader>
<div class="grid gap-2 grid-cols-1 sm:grid-cols-2">
<ItemCard v-for="item in items" :key="item.id" :item="item" />
</div>
</section>
</BaseContainer>
</template>