Skip to content

Commit

Permalink
🐛 fix undefined gameCode warning
Browse files Browse the repository at this point in the history
  • Loading branch information
zetaraku committed Nov 16, 2024
1 parent b07871a commit 0e415b6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
6 changes: 5 additions & 1 deletion composables/useGameInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export default function useGameInfo() {
,
);

const gameCode = computed(() => siteInfo.value?.gameCode ?? undefined);
const gameCode = computed(() => (
route.value.params.gameCode !== undefined
? siteInfo.value?.gameCode ?? undefined
: null
));
const gameTitle = computed(() => siteInfo.value?.gameTitle ?? undefined);
const themeColor = computed(() => siteInfo.value?.themeColor ?? '#424242');
const dataSourceUrl = computed(() => siteInfo.value?.dataSourceUrl ?? undefined);
Expand Down
26 changes: 12 additions & 14 deletions layouts/default.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
/* eslint-disable import/first, import/no-duplicates */
import { ref, computed, watch, useRoute, useMeta as useHead, useContext } from '@nuxtjs/composition-api';
import { ref, computed, watch, useMeta as useHead, useContext } from '@nuxtjs/composition-api';
import { useDataStore } from '~/stores/data';
import useGameInfo from '~/composables/useGameInfo';
import useDarkMode from '~/composables/useDarkMode';
Expand All @@ -13,7 +13,6 @@ import sites from '~/data/sites.json';
import { PageNotFoundError } from '~/utils';
const context = useContext();
const route = useRoute();
const dataStore = useDataStore();
const { isDarkMode } = useDarkMode();
const {
Expand Down Expand Up @@ -111,18 +110,17 @@ function adaptSiteStyle() {
context.$vuetify.theme.themes.light.primary = themeColor.value;
context.$vuetify.theme.themes.dark.primary = '#FFAC1C';
}
function validateGameCode() {
if (route.value.params.gameCode !== undefined && gameCode.value === undefined) {
watch(gameCode, () => {
adaptSiteStyle();
if (gameCode.value === undefined) {
context.error(new PageNotFoundError());
return;
}
}
async function detectGameCode() {
adaptSiteStyle();
validateGameCode();
dataStore.gameCode = gameCode.value!;
}
watch(gameCode, () => detectGameCode(), {
dataStore.gameCode = gameCode.value;
}, {
immediate: true,
});
watch(isDarkMode, () => {
Expand All @@ -146,7 +144,7 @@ export default defineComponent({
<LoadingOverlay v-if="dataStore.currentLoadingStatus === LoadingStatus.LOADING" />

<v-navigation-drawer
v-if="gameCode !== undefined"
v-if="gameCode != null"
v-model="isDrawerOpened"
width="300"
temporary
Expand Down Expand Up @@ -192,7 +190,7 @@ export default defineComponent({
<v-divider />
</template>

<v-list v-if="gameCode !== undefined">
<v-list v-if="gameCode != null">
<v-list-item
v-for="(menuItem, i) in menu"
:key="i"
Expand Down Expand Up @@ -226,7 +224,7 @@ export default defineComponent({
app
>
<v-app-bar-nav-icon
:disabled="gameCode === undefined"
:disabled="gameCode == null"
@click="isDrawerOpened = !isDrawerOpened;"
/>
<NuxtLink
Expand Down
10 changes: 5 additions & 5 deletions stores/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import type { Data } from '~/types';

// eslint-disable-next-line import/prefer-default-export
export const useDataStore = defineStore('data', () => {
const currentGameCode = ref('');
const currentGameCode = ref<string | null>(null);

const loadedData = ref(new Map<string, Data>());
const loadingStatuses = ref(new Map<string, LoadingStatus>());
const loadingErrorMessages = ref(new Map<string, string>());
const loadedData = ref(new Map<string | null, Data>());
const loadingStatuses = ref(new Map<string | null, LoadingStatus>());
const loadingErrorMessages = ref(new Map<string | null, string>());

const currentData = computed(
() => loadedData.value.get(currentGameCode.value) ?? buildEmptyData(),
Expand Down Expand Up @@ -65,7 +65,7 @@ export const useDataStore = defineStore('data', () => {
}

watch(currentGameCode, async () => {
if (currentGameCode.value === '') return;
if (currentGameCode.value === null) return;

if (currentLoadingStatus.value === LoadingStatus.PENDING) {
await loadData(currentGameCode.value);
Expand Down

0 comments on commit 0e415b6

Please sign in to comment.