Skip to content

Commit

Permalink
fix: index auto refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
ZTL-UwU committed Dec 23, 2023
1 parent da828f7 commit 633074f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 41 deletions.
18 changes: 12 additions & 6 deletions components/TimeAvailability.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<script setup lang="ts">
withDefaults(defineProps<{
type TState = 'unknown' | 'can' | 'cannot';
const props = withDefaults(defineProps<{
borderless?: boolean
showButton?: boolean
isCard?: boolean
status?: TState
}>(), {
borderless: false,
showButton: false,
isCard: false,
status: 'unknown',
});
const { $api } = useNuxtApp();
const state = ref<'unknown' | 'can' | 'cannot'>('unknown');
const state = ref<TState>('unknown');
const stateText = {
unknown: '未知',
Expand Down Expand Up @@ -46,18 +51,19 @@ const stateColor = {
};
onMounted(async () => {
state.value = await $api.time.currently.query() ? 'can' : 'cannot';
if (!props.isCard)
state.value = await $api.time.currently.query() ? 'can' : 'cannot';
});
</script>

<template>
<div>
<UiCard
v-if="isCard"
:class="`flex flex-col justify-center items-start shadow p-0 pt-1 pb-2 ${stateColor.from[state]} from-[-10%] ${stateColor.via[state]} via-30% to-white to-80%`" style="background: linear-gradient(310deg, var(--tw-gradient-stops))"
:class="`flex flex-col justify-center items-start shadow p-0 pt-1 pb-2 ${stateColor.from[status]} from-[-10%] ${stateColor.via[status]} via-30% to-white to-80%`" style="background: linear-gradient(310deg, var(--tw-gradient-stops))"
>
<UiCardHeader :class="`pt-1 pb-0 text-3xl font-bold ${stateColor.text[state]}`">
{{ stateText[state] }}
<UiCardHeader :class="`pt-1 pb-0 text-3xl font-bold ${stateColor.text[status]}`">
{{ stateText[status] }}
</UiCardHeader>
<UiCardContent class="pt-0 pb-0">
<span class="text-md">目前投稿状态</span>
Expand Down
19 changes: 10 additions & 9 deletions pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,15 @@ async function useRefreshData() {
if (isSubmitOpen.value)
return; // pause refresh data when submit dialog is open
try {
const newSongInfoData = await $api.song.info.query();
songListInfo.value = newSongInfoData;
const newCanSubmit = await $api.time.currently.query();
timeCanSubmit.value = newCanSubmit;
const newSongListData = (await $api.song.listSafe.query()).sort((a, b) => (a.createdAt > b.createdAt ? -1 : 1));
songList.value = newSongListData;
const newArrangementData = await $api.arrangement.listSafe.query();
arrangementList.value = newArrangementData;
const res = await Promise.all([
$api.song.info.query(),
$api.time.currently.query(),
$api.song.listSafe.query(),
]);
songListInfo.value = res[0];
timeCanSubmit.value = res[1];
songList.value = res[2].sort((a, b) => (a.createdAt > b.createdAt ? -1 : 1));
} catch (err) {
// swallow the errors
}
Expand Down Expand Up @@ -164,7 +165,7 @@ onMounted(async () => {
</UiCardContent>
</UiCard>
<TimeAvailableDialog>
<TimeAvailability is-card />
<TimeAvailability :status="timeCanSubmit ? 'can' : 'cannot'" is-card />
</TimeAvailableDialog>
</div>
<div class="grid grid-cols-3 gap-2">
Expand Down
39 changes: 13 additions & 26 deletions pages/manage/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -244,44 +244,31 @@ async function batchUpdateSong(songs: TSong[], status: TStatus) {
}
async function addToArrangement(song: TSong) {
const i = arrangementList.value.findIndex(e => e.date === dateString.value);
if (!arrangementList.value[i] || arrangementList.value[i].songs.includes(song))
if (!arrangement.value || arrangement.value.songs.includes(song))
return;
arrangementList.value[i].songs.push(song);
try {
await $api.arrangement.modifySongList.mutate({
date: dateString.value,
newSongList: arrangementList.value[i].songs.map(item => item.id) ?? [],
});
arrangement.value.songs.push(song);
const res = await updateArrangement();
if (!res)
arrangement.value.songs.pop();
else
await updateSong(song, 'used');
} catch (err) {
useErrorHandler(err);
arrangementList.value[i].songs.pop();
}
}
async function removeFromArrangement(song: TSong) {
const i = arrangementList.value.findIndex(e => e.date === dateString.value);
if (!arrangementList.value[i])
if (!arrangement.value)
return;
const j = arrangementList.value[i].songs.indexOf(song);
const j = arrangement.value.songs.indexOf(song);
if (j === -1)
return;
arrangementList.value[i].songs.splice(j, 1);
try {
await $api.arrangement.modifySongList.mutate({
date: dateString.value,
newSongList: arrangementList.value[i].songs.map(item => item.id) ?? [],
});
arrangement.value.songs.splice(j, 1);
const res = await updateArrangement();
if (!res)
arrangement.value.songs.splice(j, 1, song);
else
await updateSong(song, 'approved');
} catch (err) {
useErrorHandler(err);
arrangementList.value[i].songs.splice(j, 1, song);
}
}
const removeArrangementLoading = ref(false);
Expand Down

0 comments on commit 633074f

Please sign in to comment.