1
1
<script setup lang="ts">
2
- import { LoaderCircle } from ' lucide-vue-next '
2
+ import type { ClassroomData } from ' @prisma/client '
3
3
import { Button } from ' @/components/ui/button'
4
- import { useToast } from ' @/components/ui/toast/use-toast'
5
4
import Toaster from ' @/components/ui/toast/Toaster.vue'
5
+ import { useToast } from ' @/components/ui/toast/use-toast'
6
+ import { LoaderCircle } from ' lucide-vue-next'
6
7
import { enums } from ' ~/components/custom/enum2str'
8
+ import type { AllClubs } from ' ~/types/api/user/all_clubs'
7
9
8
10
definePageMeta ({
9
11
middleware: [' auth' ],
@@ -15,20 +17,35 @@ useHead({
15
17
16
18
const { toast } = useToast ()
17
19
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
+ })
21
26
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
+ })
29
32
}
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
+ })
32
49
}
33
50
34
51
let reloadKey = 0
@@ -98,7 +115,7 @@ async function handleSubmit(e: any) {
98
115
else if (data .value ?.status === ' PRISMA_ERROR' ) {
99
116
toast ({
100
117
title: ' 数据错误' ,
101
- description: data . value ?. message ,
118
+ description: ' 请稍后再试 ' ,
102
119
variant: ' destructive' ,
103
120
})
104
121
}
@@ -173,10 +190,10 @@ async function handleSubmit(e: any) {
173
190
</FormItem >
174
191
<FormItem >
175
192
<FormLabel >选择教室</FormLabel >
176
- <div v-if =" !dataLoaded " >
193
+ <div v-if =" !clubs || !data " >
177
194
<Skeleton class =" h-5 p-3 my-3" />
178
195
</div >
179
- <FormControl v-if = " dataLoaded " >
196
+ <FormControl >
180
197
<Select v-model =" formData.classroom" required >
181
198
<SelectTrigger >
182
199
<SelectValue placeholder =" 选择教室" />
@@ -211,24 +228,24 @@ async function handleSubmit(e: any) {
211
228
<SelectValue placeholder =" 选择你的社团" />
212
229
</SelectTrigger >
213
230
<SelectContent >
214
- <SelectGroup v-if =" clubs.president.length" >
231
+ <SelectGroup v-if =" clubs? .president.length" >
215
232
<SelectItem v-for =" club in clubs.president" :key =" club.id" :value =" club.id" >
216
233
{{ club.name.zh }}
217
234
<span class =" inline-block text-gray-500" >
218
235
社长
219
236
</span >
220
237
</SelectItem >
221
238
</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" >
224
241
{{ club.name.zh }}
225
242
<span class =" inline-block text-gray-500" >
226
243
副社
227
244
</span >
228
245
</SelectItem >
229
246
</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" >
232
249
{{ club.name.zh }}
233
250
<span class =" inline-block text-gray-500" >
234
251
成员
0 commit comments