Skip to content

Commit 9316e7e

Browse files
committed
chore: change how @q1zhen wrote network requests. simply the worst practices.
1 parent c87fe5d commit 9316e7e

File tree

3 files changed

+57
-37
lines changed

3 files changed

+57
-37
lines changed

pages/manage/reservation.vue

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<script setup lang="ts">
2-
import { LoaderCircle } from 'lucide-vue-next'
2+
import type { ClassroomData } from '@prisma/client'
33
import { Button } from '@/components/ui/button'
4-
import { useToast } from '@/components/ui/toast/use-toast'
54
import Toaster from '@/components/ui/toast/Toaster.vue'
5+
import { useToast } from '@/components/ui/toast/use-toast'
6+
import { LoaderCircle } from 'lucide-vue-next'
67
import { enums } from '~/components/custom/enum2str'
8+
import type { AllClubs } from '~/types/api/user/all_clubs'
79
810
definePageMeta({
911
middleware: ['auth'],
@@ -15,20 +17,35 @@ useHead({
1517
1618
const { toast } = useToast()
1719
18-
let dataLoaded = false
19-
let data: any // should be classroom data
20-
let clubs: any
20+
const { data } = await useAsyncData<ClassroomData[]>('classroomStatuses', () => {
21+
return $fetch<ClassroomData[]>(`/api/reservation/classroomId`, {
22+
headers: useRequestHeaders(),
23+
method: 'GET',
24+
})
25+
})
2126
22-
try {
23-
let response = await $fetch('/api/reservation/classroomId')
24-
const rawData = JSON.parse(response).data
25-
data = rawData.sort((a: any, b: any) => a.name < b.name ? -1 : 1)
26-
response = await $fetch('/api/user/all_clubs')
27-
clubs = response
28-
dataLoaded = true
27+
if (!data.value) {
28+
toast({
29+
title: '错误',
30+
description: '获取教室信息出错',
31+
})
2932
}
30-
catch (error) {
31-
console.log(error)
33+
else {
34+
data.value = data.value.sort((a: any, b: any) => a.name < b.name ? -1 : 1)
35+
}
36+
37+
const { data: clubs } = await useAsyncData<AllClubs>('clubs', () => {
38+
return $fetch<AllClubs>(`/api/user/all_clubs`, {
39+
headers: useRequestHeaders(),
40+
method: 'GET',
41+
})
42+
})
43+
44+
if (!clubs.value) {
45+
toast({
46+
title: '错误',
47+
description: '获取社团信息出错',
48+
})
3249
}
3350
3451
let reloadKey = 0
@@ -98,7 +115,7 @@ async function handleSubmit(e: any) {
98115
else if (data.value?.status === 'PRISMA_ERROR') {
99116
toast({
100117
title: '数据错误',
101-
description: data.value?.message,
118+
description: '请稍后再试',
102119
variant: 'destructive',
103120
})
104121
}
@@ -173,10 +190,10 @@ async function handleSubmit(e: any) {
173190
</FormItem>
174191
<FormItem>
175192
<FormLabel>选择教室</FormLabel>
176-
<div v-if="!dataLoaded">
193+
<div v-if="!clubs || !data">
177194
<Skeleton class="h-5 p-3 my-3" />
178195
</div>
179-
<FormControl v-if="dataLoaded">
196+
<FormControl>
180197
<Select v-model="formData.classroom" required>
181198
<SelectTrigger>
182199
<SelectValue placeholder="选择教室" />
@@ -211,24 +228,24 @@ async function handleSubmit(e: any) {
211228
<SelectValue placeholder="选择你的社团" />
212229
</SelectTrigger>
213230
<SelectContent>
214-
<SelectGroup v-if="clubs.president.length">
231+
<SelectGroup v-if="clubs?.president.length">
215232
<SelectItem v-for="club in clubs.president" :key="club.id" :value="club.id">
216233
{{ club.name.zh }}
217234
<span class="inline-block text-gray-500">
218235
社长
219236
</span>
220237
</SelectItem>
221238
</SelectGroup>
222-
<SelectGroup v-if="clubs.vice.length">
223-
<SelectItem v-for="club in clubs.vice" :key="club.id" :value="club.id">
239+
<SelectGroup v-if="clubs?.vice.length">
240+
<SelectItem v-for="club in clubs?.vice" :key="club.id" :value="club.id">
224241
{{ club.name.zh }}
225242
<span class="inline-block text-gray-500">
226243
副社
227244
</span>
228245
</SelectItem>
229246
</SelectGroup>
230-
<SelectGroup v-if="clubs.member.length">
231-
<SelectItem v-for="club in clubs.member" :key="club.id" :value="club.id">
247+
<SelectGroup v-if="clubs?.member.length">
248+
<SelectItem v-for="club in clubs?.member" :key="club.id" :value="club.id">
232249
{{ club.name.zh }}
233250
<span class="inline-block text-gray-500">
234251
成员

pages/manage/statuses.vue

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<script setup lang="ts">
2-
import { enums, time2period } from '~/components/custom/enum2str'
3-
import { useToast } from '@/components/ui/toast/use-toast'
2+
import type { ClassroomData, ReservationRecord } from '@prisma/client'
43
import Toaster from '@/components/ui/toast/Toaster.vue'
4+
import { useToast } from '@/components/ui/toast/use-toast'
5+
import { enums, time2period } from '~/components/custom/enum2str'
56
67
definePageMeta({
78
middleware: ['auth'],
89
})
910
1011
useHead({
11-
title: 'Classroom Statuses | Enspire',
12+
title: 'Classroom Status | Enspire',
1213
})
1314
1415
const { toast } = useToast()
@@ -21,22 +22,27 @@ const selectedDay = ref(enums.days.values[utc8Time.day()])
2122
const selectedPeriod = ref(time2period(utc8Time.hour() * 100 + utc8Time.minute(), selectedDay.value))
2223
2324
let dataLoaded = false
24-
let data: any
2525
26-
try {
27-
const response = await $fetch('/api/reservation/classroomId')
28-
const rawData = JSON.parse(response).data
29-
data = rawData.sort((a: any, b: any) => a.name < b.name ? -1 : 1)
26+
const { data, refresh } = await useAsyncData<ClassroomData[]>('classroomStatuses', () => {
27+
return $fetch<ClassroomData[]>(`/api/reservation/classroomId`, {
28+
headers: useRequestHeaders(),
29+
method: 'GET',
30+
})
31+
})
32+
33+
if (data.value) {
34+
console.log('data:', data.value)
35+
data.value = data.value.sort((a: any, b: any) => a.name < b.name ? -1 : 1)
3036
dataLoaded = true
3137
}
32-
catch {
38+
else {
3339
toast({
3440
title: '错误',
3541
description: '获取教室信息出错',
3642
})
3743
}
3844
39-
function status(reservationRecords: object[]) {
45+
function status(reservationRecords: ReservationRecord[]) {
4046
for (const record of reservationRecords) {
4147
if (record.day === selectedDay.value && record.period === selectedPeriod.value) {
4248
return record.club.name.zh

server/api/reservation/classroomId.get.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@ export default eventHandler(async (event) => {
88
if (!auth.userId)
99
setResponseStatus(event, 403)
1010

11-
const records = await prisma.classroomData.findMany({
11+
return prisma.classroomData.findMany({
1212
include: {
1313
ReservationRecord: {
1414
include: {
1515
club: true,
1616
},
1717
},
1818
},
19-
})
20-
return JSON.stringify({
21-
data: Array.from(records.values()),
22-
})
19+
});
2320
})

0 commit comments

Comments
 (0)