Skip to content

Commit 6db4416

Browse files
authored
refactor!: use @tanstack/vue-query instead of useAsyncData (#656)
* refactor!: use `vue-query` instead of `useAsyncData` * revert commenting out sentry * changeset
1 parent 1f1e4c7 commit 6db4416

File tree

18 files changed

+189
-88
lines changed

18 files changed

+189
-88
lines changed

.changeset/dull-icons-report.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"enspire": patch
3+
---
4+
5+
Refactor internal strategy for making API requests

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
shamefully-hoist=true

components/custom/CAS/ClassroomReservation/NewClassroomReservation.vue

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ import { enums } from '~/components/custom/enum2str'
1515
1616
const { toast } = useToast()
1717
18-
const { data } = await useAsyncData<ClassroomData[]>('classroomStatuses', () => {
19-
return $fetch<ClassroomData[]>(`/api/reservation/classroomId`, {
20-
headers: useRequestHeaders(),
21-
method: 'GET',
22-
})
18+
const { data, suspense } = useQuery<ClassroomData[]>({
19+
queryKey: ['/api/reservation/classroomId'],
2320
})
21+
await suspense()
2422
2523
if (!data.value) {
2624
toast({
@@ -32,12 +30,10 @@ else {
3230
data.value = data.value.sort((a: any, b: any) => a.name < b.name ? -1 : 1)
3331
}
3432
35-
const { data: clubs } = await useAsyncData<AllClubs>('allClubs', () => {
36-
return $fetch<AllClubs>(`/api/user/all_clubs`, {
37-
headers: useRequestHeaders(),
38-
method: 'GET',
39-
})
33+
const { data: clubs, suspense: clubsSuspense } = useQuery<AllClubs>({
34+
queryKey: ['/api/user/all_clubs'],
4035
})
36+
await clubsSuspense()
4137
4238
if (!clubs.value) {
4339
toast({

components/custom/CAS/Record/NewActivityRecord.vue

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ const { toast } = useToast()
3333
3434
const selectedClub = ref<string>()
3535
36-
definePageMeta({
37-
middleware: ['auth'],
38-
})
39-
4036
const isLoading = ref(false)
4137
4238
const formSchema = toTypedSchema(z.object({
@@ -51,12 +47,10 @@ const formSchema = toTypedSchema(z.object({
5147
sTime: z.number().min(0).max(5),
5248
}))
5349
54-
const { data } = await useAsyncData<AllClubs>('allClubsWithMemberships', async () => {
55-
return await $fetch<AllClubs>('/api/user/all_clubs?includeMemberships=true', {
56-
headers: useRequestHeaders(),
57-
method: 'GET',
58-
})
50+
const { data, suspense } = useQuery<AllClubs>({
51+
queryKey: ['/api/user/all_clubs'],
5952
})
53+
await suspense()
6054
6155
if (!data.value) {
6256
throw createError({

components/custom/CAS/Record/ViewMyActivityRecords.vue

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<script setup lang="ts">
22
import type { Ref } from 'vue'
3+
import type { MyRecords } from '~/types/api/cas/record/my'
4+
import type { AllClubs } from '~/types/api/user/all_clubs'
35
import {
46
Card,
57
CardContent,
@@ -16,36 +18,26 @@ import {
1618
SelectValue,
1719
} from '@/components/ui/select'
1820
import { Toaster } from '~/components/ui/toast'
19-
import type { MyRecords } from '~/types/api/cas/record/my'
20-
import type { AllClubs } from '~/types/api/user/all_clubs'
2121
import { columns } from './view-activity-records/columns'
2222
import DataTable from './view-activity-records/DataTable.vue'
23+
import { useQuery } from '@tanstack/vue-query';
2324
2425
const props = defineProps<{
2526
refreshWatcher: Ref<boolean>
2627
}>()
2728
28-
definePageMeta({
29-
middleware: ['auth'],
30-
})
31-
3229
const selectedClub = ref<string>()
3330
const isLoading = ref(false)
3431
35-
const { data: clubs } = await useAsyncData<AllClubs>('allClubs', () => {
36-
return $fetch<AllClubs>(`/api/user/all_clubs`, {
37-
headers: useRequestHeaders(),
38-
method: 'GET',
39-
})
32+
const { data: clubs, suspense } = useQuery<AllClubs>({
33+
queryKey: ['/api/user/all_clubs'],
4034
})
35+
await suspense()
4136
42-
const { data, refresh } = await useAsyncData<MyRecords>('allRequests', () => {
43-
return selectedClub.value
44-
? $fetch<MyRecords>(`/api/cas/record/my?club=${selectedClub.value}`, {
45-
headers: useRequestHeaders(),
46-
method: 'GET',
47-
})
48-
: Promise.resolve({ total: 0, data: [] })
37+
const { data, refetch: refresh } = useQuery<MyRecords>({
38+
queryKey: [`/api/cas/record/my?club=`, selectedClub],
39+
enabled: !!selectedClub.value,
40+
placeholderData: { total: 0, data: [] },
4941
})
5042
5143
async function onRefresh() {

components/custom/Index/announcements.vue

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
import type { Announcements } from '~/types/payloadcms/announcements'
33
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table'
44
5-
const { data } = await useAsyncData<Announcements>('allRequests', () => {
6-
return $fetch('/api/cms/api/announcements', {
7-
method: 'GET',
8-
})
5+
const { data, suspense } = useQuery<Announcements>({
6+
queryKey: ['/api/cms/api/announcements'],
97
})
8+
await suspense()
109
</script>
1110

1211
<template>

components/custom/sidebar.vue

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,21 @@
22
import type { AllClubs } from '~/types/api/user/all_clubs'
33
import { Button } from '@/components/ui/button'
44
import { cn } from '@/lib/utils'
5-
import { useClerk } from 'vue-clerk'
65
7-
const clerk = useClerk()
86
const route = useRoute()
97
108
const isPresidentOrVicePresident = ref(false)
119
useState('isEnspireLoading').value = true
1210
13-
if (import.meta.client) {
14-
if (clerk.user?.publicMetadata.binded) {
15-
const clubs = await $fetch<AllClubs>(`/api/user/all_clubs`, {
16-
headers: useRequestHeaders(),
17-
method: 'GET',
18-
})
19-
if (clubs) {
20-
useState('isEnspireLoading').value = false
21-
if (clubs.president.length !== 0 || clubs.vice.length !== 0) {
22-
isPresidentOrVicePresident.value = true
23-
}
24-
}
11+
const { data: clubs, suspense } = useQuery<AllClubs>({
12+
queryKey: ['/api/user/all_clubs'],
13+
})
14+
await suspense()
15+
16+
if (clubs.value) {
17+
useState('isEnspireLoading').value = false
18+
if (clubs.value.president.length !== 0 || clubs.value.vice.length !== 0) {
19+
isPresidentOrVicePresident.value = true
2520
}
2621
}
2722
</script>
@@ -64,7 +59,7 @@ if (import.meta.client) {
6459
</Button>
6560
</NuxtLink>
6661
<NuxtLink v-if="[0, 1, 5, 6].includes(new Date().getMonth())" to="/cas/rating">
67-
<Button :variant="route.name === 'cas-rating' ? 'secondary' : 'ghost'" class="w-full justify-start mt-1">
62+
<Button :variant="route.name === 'cas-rating' ? 'secondary' : 'ghost'" class="mt-1 w-full justify-start">
6863
<Icon class="mr-2 h-4 w-4" name="material-symbols:rate-review-outline" />
6964
期末评价
7065
</Button>

eslint.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import antfu from '@antfu/eslint-config'
2+
import pluginQuery from '@tanstack/eslint-plugin-query'
23

34
export default antfu({
45
typescript: {
@@ -7,6 +8,7 @@ export default antfu({
78
vue: true,
89
ignores: ['components/ui/'],
910
unocss: true,
11+
...pluginQuery.configs['flat/recommended'],
1012
}, {
1113
rules: {
1214
'node/prefer-global/process': 'off',

nuxt.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,20 @@ export default defineNuxtConfig({
7676
client: true,
7777
},
7878

79+
imports: {
80+
presets: [
81+
{
82+
from: '@tanstack/vue-query',
83+
imports: ['useQuery'],
84+
},
85+
],
86+
},
87+
88+
vite: {
89+
optimizeDeps: {
90+
exclude: ['vee-validate'],
91+
},
92+
},
93+
7994
compatibilityDate: '2024-08-31',
8095
})

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@nuxt/content": "^2.13.4",
3030
"@radix-icons/vue": "^1.0.0",
3131
"@sentry/nuxt": "^8.42.0",
32+
"@tanstack/vue-query": "^5.62.8",
3233
"@tanstack/vue-table": "^8.20.5",
3334
"@unovis/ts": "^1.4.5",
3435
"@unovis/vue": "^1.4.5",
@@ -71,6 +72,7 @@
7172
"@nuxtjs/google-fonts": "^3.2.0",
7273
"@prisma/client": "^6.0.1",
7374
"@rollup/plugin-wasm": "^6.2.2",
75+
"@tanstack/eslint-plugin-query": "^5.62.1",
7476
"@types/node-fetch": "^2.6.12",
7577
"@types/sanitize-html": "^2.13.0",
7678
"@types/uuid": "^10.0.0",

0 commit comments

Comments
 (0)