🔨 Redirect if guild is not available

This commit is contained in:
SebClem 2022-06-16 12:23:36 +02:00
parent d460a2d5b0
commit 3511bedaec
Signed by: sebclem
GPG Key ID: 5A4308F6A359EA50
5 changed files with 86 additions and 51 deletions

View File

@ -13,5 +13,7 @@
"javascript.inlayHints.propertyDeclarationTypes.enabled": true, "javascript.inlayHints.propertyDeclarationTypes.enabled": true,
"javascript.inlayHints.parameterNames.suppressWhenArgumentMatchesName": true, "javascript.inlayHints.parameterNames.suppressWhenArgumentMatchesName": true,
"javascript.inlayHints.parameterNames.enabled": "literals", "javascript.inlayHints.parameterNames.enabled": "literals",
"emmet.includeLanguages": {
"vue": "html"
}
} }

View File

@ -6,7 +6,7 @@ export const useMutualGuildsStore = defineStore({
state: () => ({ state: () => ({
guilds: [] as Array<Guild>, guilds: [] as Array<Guild>,
loaded: false, loaded: false,
lastGuildId: "", lastGuildId: undefined as string | undefined,
}), }),
getters: {}, getters: {},
actions: { actions: {

20
src/tools/GuildTools.ts Normal file
View File

@ -0,0 +1,20 @@
import type { Guild } from "@/data/Guild";
import { useMutualGuildsStore } from "@/stores/mutualGuilds";
import type { Router } from "vue-router";
function redirectIfNoGuild(guild: Guild | undefined, router: Router) {
const guildStore = useMutualGuildsStore();
if (!guild) {
guildStore.lastGuildId = guildStore.guilds[0]?.id;
if (guildStore.lastGuildId) {
router.push({
name: "guildHome",
params: { guildId: guildStore.lastGuildId },
});
} else {
router.push({ name: "home" });
}
}
}
export { redirectIfNoGuild };

View File

@ -1,9 +1,13 @@
<template> <template>
<div>
<v-row> <v-row>
<v-col> <v-col>
<v-card> <v-card>
<v-card-title class="d-flex"> <v-card-title class="d-flex">
<v-avatar :color="guild?.iconUrl ? '' : 'grey-darken-3'" class="mr-3"> <v-avatar
:color="guild?.iconUrl ? '' : 'grey-darken-3'"
class="mr-3"
>
<v-img v-if="guild?.iconUrl" :src="guild.iconUrl"></v-img> <v-img v-if="guild?.iconUrl" :src="guild.iconUrl"></v-img>
<template v-if="!guild?.iconUrl">{{ guild?.name[1] }}</template> <template v-if="!guild?.iconUrl">{{ guild?.name[1] }}</template>
</v-avatar> </v-avatar>
@ -34,25 +38,30 @@
</v-container> </v-container>
</v-col> </v-col>
</v-row> </v-row>
</div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { useMutualGuildsStore } from "@/stores/mutualGuilds"; import { useMutualGuildsStore } from "@/stores/mutualGuilds";
import { ref, watch } from "vue"; import { ref, watch } from "vue";
import { useRoute } from "vue-router"; import { useRoute, useRouter } from "vue-router";
import SettingPreviewComponent from "../components/guild/home/SettingPreviewComponent.vue"; import SettingPreviewComponent from "../components/guild/home/SettingPreviewComponent.vue";
import MusicPreviewComponent from "../components/guild/home/MusicPreviewComponent.vue"; import MusicPreviewComponent from "../components/guild/home/MusicPreviewComponent.vue";
import StatsPreviewComponent from "../components/guild/home/StatsPreviewComponent.vue"; import StatsPreviewComponent from "../components/guild/home/StatsPreviewComponent.vue";
import { redirectIfNoGuild } from "@/tools/GuildTools";
const guildStore = useMutualGuildsStore(); const guildStore = useMutualGuildsStore();
const route = useRoute(); const route = useRoute();
const router = useRouter();
const guild = ref(guildStore.getGuild(route.params.guildId as string)); const guild = ref(guildStore.getGuild(route.params.guildId as string));
redirectIfNoGuild(guild.value, router);
watch( watch(
() => route.params.guildId, () => route.params.guildId,
(value, oldValue) => { (value, oldValue) => {
guild.value = guildStore.getGuild(value as string); guild.value = guildStore.getGuild(value as string);
guildStore.lastGuildId = guild.value?.id;
redirectIfNoGuild(guild.value, router);
} }
); );
</script> </script>

View File

@ -1,4 +1,5 @@
<template> <template>
<div>
<v-row v-if="!userStore.isLoggedIn"> <v-row v-if="!userStore.isLoggedIn">
<v-col> <v-col>
<v-btn to="/oauth2/redirect" prepend-icon="mdi-discord" color="primary"> <v-btn to="/oauth2/redirect" prepend-icon="mdi-discord" color="primary">
@ -6,6 +7,7 @@
</v-btn> </v-btn>
</v-col> </v-col>
</v-row> </v-row>
</div>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
@ -20,12 +22,14 @@ const router = useRouter();
onBeforeMount(() => { onBeforeMount(() => {
if (userStore.isLoggedIn) { if (userStore.isLoggedIn) {
if (!mutualGuildStore.lastGuildId) { if (!mutualGuildStore.lastGuildId) {
mutualGuildStore.lastGuildId = mutualGuildStore.guilds[0].id; mutualGuildStore.lastGuildId = mutualGuildStore.guilds[0]?.id;
} }
if (mutualGuildStore.lastGuildId) {
router.push({ router.push({
name: "guildHome", name: "guildHome",
params: { guildId: mutualGuildStore.lastGuildId }, params: { guildId: mutualGuildStore.lastGuildId },
}); });
} }
}
}); });
</script> </script>