From 6db4416a1c22a37b6a00bd188702fb49c028d6aa Mon Sep 17 00:00:00 2001 From: Tom Tang Date: Fri, 20 Dec 2024 23:44:50 +0800 Subject: [PATCH] refactor!: use `@tanstack/vue-query` instead of `useAsyncData` (#656) * refactor!: use `vue-query` instead of `useAsyncData` * revert commenting out sentry * changeset --- .changeset/dull-icons-report.md | 5 ++ .npmrc | 1 + .../NewClassroomReservation.vue | 16 +++--- .../custom/CAS/Record/NewActivityRecord.vue | 12 ++--- .../CAS/Record/ViewMyActivityRecords.vue | 28 ++++------ components/custom/Index/announcements.vue | 7 ++- components/custom/sidebar.vue | 25 ++++----- eslint.config.js | 2 + nuxt.config.ts | 15 ++++++ package.json | 2 + pages/cas/rating.vue | 10 ++-- pages/forms/[id].vue | 10 ++-- pages/forms/index.vue | 16 +++--- pages/manage/statuses.vue | 8 ++- plugins/vue-query.ts | 38 +++++++++++++ pnpm-lock.yaml | 54 +++++++++++++++++++ queries/default.ts | 18 +++++++ server/api/user/all_clubs.get.ts | 10 ++-- 18 files changed, 189 insertions(+), 88 deletions(-) create mode 100644 .changeset/dull-icons-report.md create mode 100644 .npmrc create mode 100644 plugins/vue-query.ts create mode 100644 queries/default.ts diff --git a/.changeset/dull-icons-report.md b/.changeset/dull-icons-report.md new file mode 100644 index 00000000..f67d932a --- /dev/null +++ b/.changeset/dull-icons-report.md @@ -0,0 +1,5 @@ +--- +"enspire": patch +--- + +Refactor internal strategy for making API requests diff --git a/.npmrc b/.npmrc new file mode 100644 index 00000000..c483022c --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +shamefully-hoist=true \ No newline at end of file diff --git a/components/custom/CAS/ClassroomReservation/NewClassroomReservation.vue b/components/custom/CAS/ClassroomReservation/NewClassroomReservation.vue index 3bf11a0d..480e0b2f 100644 --- a/components/custom/CAS/ClassroomReservation/NewClassroomReservation.vue +++ b/components/custom/CAS/ClassroomReservation/NewClassroomReservation.vue @@ -15,12 +15,10 @@ import { enums } from '~/components/custom/enum2str' const { toast } = useToast() -const { data } = await useAsyncData('classroomStatuses', () => { - return $fetch(`/api/reservation/classroomId`, { - headers: useRequestHeaders(), - method: 'GET', - }) +const { data, suspense } = useQuery({ + queryKey: ['/api/reservation/classroomId'], }) +await suspense() if (!data.value) { toast({ @@ -32,12 +30,10 @@ else { data.value = data.value.sort((a: any, b: any) => a.name < b.name ? -1 : 1) } -const { data: clubs } = await useAsyncData('allClubs', () => { - return $fetch(`/api/user/all_clubs`, { - headers: useRequestHeaders(), - method: 'GET', - }) +const { data: clubs, suspense: clubsSuspense } = useQuery({ + queryKey: ['/api/user/all_clubs'], }) +await clubsSuspense() if (!clubs.value) { toast({ diff --git a/components/custom/CAS/Record/NewActivityRecord.vue b/components/custom/CAS/Record/NewActivityRecord.vue index a55caaf5..02446c3b 100644 --- a/components/custom/CAS/Record/NewActivityRecord.vue +++ b/components/custom/CAS/Record/NewActivityRecord.vue @@ -33,10 +33,6 @@ const { toast } = useToast() const selectedClub = ref() -definePageMeta({ - middleware: ['auth'], -}) - const isLoading = ref(false) const formSchema = toTypedSchema(z.object({ @@ -51,12 +47,10 @@ const formSchema = toTypedSchema(z.object({ sTime: z.number().min(0).max(5), })) -const { data } = await useAsyncData('allClubsWithMemberships', async () => { - return await $fetch('/api/user/all_clubs?includeMemberships=true', { - headers: useRequestHeaders(), - method: 'GET', - }) +const { data, suspense } = useQuery({ + queryKey: ['/api/user/all_clubs'], }) +await suspense() if (!data.value) { throw createError({ diff --git a/components/custom/CAS/Record/ViewMyActivityRecords.vue b/components/custom/CAS/Record/ViewMyActivityRecords.vue index 44d105dc..352a1dad 100644 --- a/components/custom/CAS/Record/ViewMyActivityRecords.vue +++ b/components/custom/CAS/Record/ViewMyActivityRecords.vue @@ -1,5 +1,7 @@