refactor/fix: group query argumenten gerefactord, group query cach keys gefixt
This commit is contained in:
		
							parent
							
								
									7bee08537a
								
							
						
					
					
						commit
						389ce91b52
					
				
					 2 changed files with 28 additions and 41 deletions
				
			
		|  | @ -70,8 +70,7 @@ export function useCreateClassMutation(): UseMutationReturnType<ClassResponse, E | ||||||
|     return useMutation({ |     return useMutation({ | ||||||
|         mutationFn: async (data) => classController.createClass(data), |         mutationFn: async (data) => classController.createClass(data), | ||||||
|         onSuccess: async () => { |         onSuccess: async () => { | ||||||
|             await queryClient.invalidateQueries({ queryKey: classesQueryKey(true) }); |             await queryClient.invalidateQueries({ queryKey: [ "classes" ] }); | ||||||
|             await queryClient.invalidateQueries({ queryKey: classesQueryKey(false) }); |  | ||||||
|         }, |         }, | ||||||
|     }); |     }); | ||||||
| } | } | ||||||
|  | @ -90,11 +89,11 @@ export function useDeleteClassMutation(): UseMutationReturnType<ClassResponse, E | ||||||
|     }); |     }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export function useUpdateClassMutation(): UseMutationReturnType<ClassResponse, Error, ClassDTO, unknown> { | export function useUpdateClassMutation(): UseMutationReturnType<ClassResponse, Error, {cid: string, data: Partial<ClassDTO>}, unknown> { | ||||||
|     const queryClient = useQueryClient(); |     const queryClient = useQueryClient(); | ||||||
| 
 | 
 | ||||||
|     return useMutation({ |     return useMutation({ | ||||||
|         mutationFn: async (data) => classController.updateClass(data.id, data), |         mutationFn: async ({ cid, data }) => classController.updateClass(cid, data), | ||||||
|         onSuccess: async (data) => { |         onSuccess: async (data) => { | ||||||
|             await invalidateAllClassKeys(queryClient, data.class.id); |             await invalidateAllClassKeys(queryClient, data.class.id); | ||||||
|             await invalidateAllAssignmentKeys(queryClient, data.class.id); |             await invalidateAllAssignmentKeys(queryClient, data.class.id); | ||||||
|  |  | ||||||
|  | @ -5,6 +5,8 @@ import type { SubmissionsResponse } from "@/controllers/submissions"; | ||||||
| import type { GroupDTO } from "@dwengo-1/common/interfaces/group"; | import type { GroupDTO } from "@dwengo-1/common/interfaces/group"; | ||||||
| import { QueryClient, useMutation, useQuery, useQueryClient, type UseMutationReturnType, type UseQueryReturnType } from "@tanstack/vue-query"; | import { QueryClient, useMutation, useQuery, useQueryClient, type UseMutationReturnType, type UseQueryReturnType } from "@tanstack/vue-query"; | ||||||
| import { computed, toValue, type MaybeRefOrGetter } from "vue"; | import { computed, toValue, type MaybeRefOrGetter } from "vue"; | ||||||
|  | import { invalidateAllAssignmentKeys } from "./assignments"; | ||||||
|  | import { invalidateAllSubmissionKeys } from "./submissions"; | ||||||
| 
 | 
 | ||||||
| export function groupsQueryKey(classid: string, assignmentNumber: number, full: boolean) { | export function groupsQueryKey(classid: string, assignmentNumber: number, full: boolean) { | ||||||
|     return [ "groups", classid, assignmentNumber, full ]; |     return [ "groups", classid, assignmentNumber, full ]; | ||||||
|  | @ -83,62 +85,48 @@ export function useGroupQuery( | ||||||
|     }); |     }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // TODO: find way to check if cid and an are not undefined.
 | export function useCreateGroupMutation(): UseMutationReturnType<GroupResponse, Error, {cid: string, an: number, data: GroupDTO}, unknown> { | ||||||
| // depends on how this function is used.
 |  | ||||||
| export function useCreateGroupMutation( |  | ||||||
|     classid: MaybeRefOrGetter<string | undefined>,  |  | ||||||
|     assignmentNumber: MaybeRefOrGetter<number | undefined>,  |  | ||||||
| ): UseMutationReturnType<GroupResponse, Error, GroupDTO, unknown> { |  | ||||||
|     const queryClient = useQueryClient(); |     const queryClient = useQueryClient(); | ||||||
|     const { cid, an } = toValues(classid, assignmentNumber, 1, true); |  | ||||||
| 
 | 
 | ||||||
|     return useMutation({ |     return useMutation({ | ||||||
|         mutationFn: async (data) => new GroupController(cid!, an!).createGroup(data), |         mutationFn: async ({ cid, an, data }) => new GroupController(cid, an).createGroup(data), | ||||||
|         onSuccess: async () => { |         onSuccess: async (response) => { | ||||||
|             await queryClient.invalidateQueries({ queryKey: groupsQueryKey(cid!, an!, true) }); |             const cid = typeof(response.group.class) === 'string' ? response.group.class : response.group.class.id; | ||||||
|             await queryClient.invalidateQueries({ queryKey: groupsQueryKey(cid!, an!, false) }); |             const an = typeof(response.group.assignment) === 'number' ? response.group.assignment : response.group.assignment.id; | ||||||
|  | 
 | ||||||
|  |             await queryClient.invalidateQueries({ queryKey: groupsQueryKey(cid, an, true) }); | ||||||
|  |             await queryClient.invalidateQueries({ queryKey: groupsQueryKey(cid, an, false) }); | ||||||
|         }, |         }, | ||||||
|     }); |     }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export function useDeleteGroupMutation( | export function useDeleteGroupMutation(): UseMutationReturnType<GroupResponse, Error, {cid: string, an: number, gn: number}, unknown> { | ||||||
|     classid: MaybeRefOrGetter<string | undefined>,  |  | ||||||
|     assignmentNumber: MaybeRefOrGetter<number | undefined>,  |  | ||||||
| ): UseMutationReturnType<GroupResponse, Error, number, unknown> { |  | ||||||
|     const queryClient = useQueryClient(); |     const queryClient = useQueryClient(); | ||||||
|     const { cid, an, gn } = toValues(classid, assignmentNumber, 1, true); |  | ||||||
| 
 | 
 | ||||||
|     return useMutation({ |     return useMutation({ | ||||||
|         mutationFn: async (id) => new GroupController(cid!, an!).deleteGroup(id), |         mutationFn: async ({cid, an, gn}) => new GroupController(cid, an).deleteGroup(gn), | ||||||
|         onSuccess: async () => { |         onSuccess: async (response) => { | ||||||
|             await queryClient.invalidateQueries({ queryKey: groupQueryKey(cid!, an!, gn!) }); |             const cid = typeof(response.group.class) === 'string' ? response.group.class : response.group.class.id; | ||||||
|  |             const an = typeof(response.group.assignment) === 'number' ? response.group.assignment : response.group.assignment.id; | ||||||
|  |             const gn = response.group.groupNumber; | ||||||
| 
 | 
 | ||||||
|             await queryClient.invalidateQueries({ queryKey: groupsQueryKey(cid!, an!, true) }); |             await invalidateAllGroupKeys(queryClient, cid, an, gn); | ||||||
|             await queryClient.invalidateQueries({ queryKey: groupsQueryKey(cid!, an!, false) }); |             await invalidateAllSubmissionKeys(queryClient, cid, an, gn); | ||||||
| 
 |  | ||||||
|             await queryClient.invalidateQueries({ queryKey: groupSubmissionsQueryKey(cid!, an!, gn!, true) }); |  | ||||||
|             await queryClient.invalidateQueries({ queryKey: groupSubmissionsQueryKey(cid!, an!, gn!, false) }); |  | ||||||
| 
 |  | ||||||
|             await queryClient.invalidateQueries({ queryKey: groupQuestionsQueryKey(cid!, an!, gn!, true) }); |  | ||||||
|             await queryClient.invalidateQueries({ queryKey: groupQuestionsQueryKey(cid!, an!, gn!, false) }); |  | ||||||
|         }, |         }, | ||||||
|     }); |     }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export function useUpdateGroupMutation( | export function useUpdateGroupMutation(): UseMutationReturnType<GroupResponse, Error, {cid: string, an: number, gn: number, data: Partial<GroupDTO>}, unknown> { | ||||||
|     classid: MaybeRefOrGetter<string | undefined>,  |  | ||||||
|     assignmentNumber: MaybeRefOrGetter<number | undefined>,  |  | ||||||
| ): UseMutationReturnType<GroupResponse, Error, GroupDTO, unknown> { |  | ||||||
|     const queryClient = useQueryClient(); |     const queryClient = useQueryClient(); | ||||||
|     const { cid, an, gn } = toValues(classid, assignmentNumber, 1, true); |  | ||||||
| 
 | 
 | ||||||
|     return useMutation({ |     return useMutation({ | ||||||
|         mutationFn: async (data) => new GroupController(cid!, an!).updateGroup(gn!, data), |         mutationFn: async ({cid, an, gn, data}) => new GroupController(cid, an).updateGroup(gn, data), | ||||||
|         onSuccess: async () => { |         onSuccess: async (response) => { | ||||||
|             await queryClient.invalidateQueries({ queryKey: groupQueryKey(cid!, an!, gn!) }); |             const cid = typeof(response.group.class) === 'string' ? response.group.class : response.group.class.id; | ||||||
|  |             const an = typeof(response.group.assignment) === 'number' ? response.group.assignment : response.group.assignment.id; | ||||||
|  |             const gn = response.group.groupNumber; | ||||||
| 
 | 
 | ||||||
|             await queryClient.invalidateQueries({ queryKey: groupsQueryKey(cid!, an!, true) }); |             await invalidateAllGroupKeys(queryClient, cid, an, gn); | ||||||
|             await queryClient.invalidateQueries({ queryKey: groupsQueryKey(cid!, an!, false) }); |  | ||||||
|         }, |         }, | ||||||
|     }); |     }); | ||||||
| } | } | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Adriaan Jacquet
						Adriaan Jacquet