-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinteraction-queries.ts
More file actions
74 lines (66 loc) · 2.55 KB
/
interaction-queries.ts
File metadata and controls
74 lines (66 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { addInteraction, deleteInteraction, editInteraction, getInteractions } from '../api/api';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { Interaction } from '../Interactions';
import { interactionsQueryKeys } from './keys';
/**
* gets all interactions for a trainee
* @param traineeId the id of the trainee
* @returns an array of interactions for the trainee
*/
export const useGetInteractions = (traineeId: string) => {
return useQuery({
queryKey: interactionsQueryKeys.list(traineeId),
queryFn: async () => {
const interactions = await getInteractions(traineeId);
return orderInteractionsByDateDesc(interactions);
},
enabled: !!traineeId,
refetchOnWindowFocus: false,
});
};
const orderInteractionsByDateDesc = (data: Interaction[]): Interaction[] => {
return data.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
};
/**
* Hook to add an interaction to a trainee.
* @param {string} traineeId - The ID of the trainee to add the interaction to
* @returns Mutation hook for adding an interaction
*/
export const useAddInteraction = (traineeId: string) => {
const queryClient = useQueryClient();
// partial because not all fields are sent to the backend
return useMutation({
mutationFn: (interaction: Partial<Interaction>) => addInteraction(traineeId, interaction),
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: interactionsQueryKeys.list(traineeId) });
},
});
};
/**
* Hook to delete an interaction from a trainee.
* @param {string} traineeId - The ID of the trainee to delete the interaction from
* @returns Mutation hook for deleting an interaction
*/
export const useDeleteInteraction = (traineeId: string) => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (interactionId: string) => deleteInteraction(traineeId, interactionId),
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: interactionsQueryKeys.list(traineeId) });
},
});
};
/**
* Hook to edit an existing interaction for a trainee.
* @param {string} traineeId - The ID of the trainee
* @returns Mutation hook for editing an interaction
*/
export const useEditInteraction = (traineeId: string) => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (interaction: Interaction) => editInteraction(traineeId, interaction),
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: interactionsQueryKeys.list(traineeId) });
},
});
};