-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathfreeform-search.ts
More file actions
129 lines (112 loc) · 4.31 KB
/
Copy pathfreeform-search.ts
File metadata and controls
129 lines (112 loc) · 4.31 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import {
type SavedScreeningSearchFilters,
type SavedScreeningSearchPage,
type ScreeningMatchPayload,
} from '@app-builder/models/screening';
import {
createFreeFormSearchPresetFn,
deleteFreeFormSearchPresetFn,
type FreeformSearchInput,
FreeformSearchPreset,
freeformSearchFn,
getFreeFormSearchPresetFn,
getFreeformSearchFn,
getListFreeFormSearchPresetsFn,
listSavedFreeformSearchesFn,
saveFreeformSearchFn,
} from '@app-builder/server-fns/screenings';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { useServerFn } from '@tanstack/react-start';
type FreeformSearchResponse = { id: string; matches: ScreeningMatchPayload[] };
export const useFreeformSearchMutation = () => {
const freeformSearch = useServerFn(freeformSearchFn);
const queryClient = useQueryClient();
return useMutation({
mutationKey: ['screening', 'freeform-search'],
mutationFn: async (input: FreeformSearchInput): Promise<FreeformSearchResponse> => {
// TODO: remove this filter when reindexation is done
const datasets =
input.entityType !== 'Person'
? input.datasets?.filter(
(dataset) =>
dataset !== 'global:topic:liveness:filter.alive' && dataset !== 'global:topic:liveness:filter.deceased',
)
: input.datasets;
return freeformSearch({ data: { ...input, datasets } }) as Promise<FreeformSearchResponse>;
},
onSuccess: () => {
void queryClient.invalidateQueries({ queryKey: ['screening', 'saved-searches'] });
},
});
};
export const useSaveFreeformSearchMutation = () => {
const queryClient = useQueryClient();
const saveFreeformSearch = useServerFn(saveFreeformSearchFn);
return useMutation({
mutationKey: ['screening', 'save-freeform-search'],
mutationFn: async (input: { id: string }): Promise<void> => {
await saveFreeformSearch({ data: input });
},
onSuccess: () => {
void queryClient.invalidateQueries({ queryKey: ['screening', 'saved-searches'] });
},
});
};
export const useSavedFreeformSearchesQuery = (filters: SavedScreeningSearchFilters = {}) => {
const listSavedSearches = useServerFn(listSavedFreeformSearchesFn);
return useQuery({
queryKey: ['screening', 'saved-searches', filters],
queryFn: async (): Promise<SavedScreeningSearchPage> => {
return listSavedSearches({ data: filters }) as Promise<SavedScreeningSearchPage>;
},
});
};
type GetFreeformSearchResponse = { id: string; matches: ScreeningMatchPayload[] };
export const useGetFreeformSearchQuery = (id: string) => {
const getFreeformSearch = useServerFn(getFreeformSearchFn);
return useQuery({
queryKey: ['screening', 'freeform-search', id],
queryFn: async (): Promise<GetFreeformSearchResponse> => {
return getFreeformSearch({ data: { id } }) as Promise<GetFreeformSearchResponse>;
},
});
};
export const useListFreeFormSearchPresetsQuery = () => {
const getListFFS = useServerFn(getListFreeFormSearchPresetsFn);
return useQuery({
queryKey: ['screening', 'freeform-search'],
queryFn: async () => {
const result = await getListFFS();
return result;
},
});
};
export const useGetFreeFormSearchPresetQuery = (name: string) => {
const getFreeFormSearchPreset = useServerFn(getFreeFormSearchPresetFn);
return useQuery({
queryKey: ['screening', 'freeform-search', 'preset', name],
queryFn: async () => {
const result = await getFreeFormSearchPreset({ data: { name } });
return result;
},
});
};
export const useCreateFreeFormSearchPresetMutation = () => {
const createFreeFormSearchPreset = useServerFn(createFreeFormSearchPresetFn);
return useMutation({
mutationKey: ['screening', 'freeform-search', 'preset'],
mutationFn: async ({ name, value }: { name: string; value: FreeformSearchPreset }) => {
console.log('createFreeFormSearchPreset', { name, value });
return createFreeFormSearchPreset({ data: { name, value } });
},
});
};
export const useDeleteFreeFormSearchPresetMutation = () => {
const deleteFreeFormSearchPreset = useServerFn(deleteFreeFormSearchPresetFn);
return useMutation({
mutationKey: ['screening', 'freeform-search', 'preset'],
mutationFn: async ({ name }: { name: string }) => {
return deleteFreeFormSearchPreset({ data: { name } });
},
});
};