-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuseCustomAPIHook.js
53 lines (46 loc) · 1.46 KB
/
useCustomAPIHook.js
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
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { CustomService } from "../services/elements/CustomService";
/**
* Custom hook which can make api call and format response
*
* @author jagankumar-egov
*
*
* @example
*
const requestCriteria = [
"/user/_search", // API details
{}, //requestParam
{data : {uuid:[Useruuid]}}, // requestBody
{} , // privacy value
{ // other configs
enabled: privacyState,
gcTime: 100,
select: (data) => {
// format data
return _.get(data, loadData?.jsonPath, value);
},
},
];
const { isLoading, data, revalidate } = Digit.Hooks.useCustomAPIHook(...requestCriteria);
*
* @returns {Object} Returns the object which contains data and isLoading flag
*/
const useCustomAPIHook = ({ url, params, body, config = {}, plainAccessRequest,changeQueryName="Random" }) => {
const client = useQueryClient();
const { isLoading, data, isFetching } = useQuery({
queryKey: [url, changeQueryName].filter(Boolean),
queryFn: () => CustomService.getResponse({ url, params, body, plainAccessRequest }),
gcTime: 0,
...config,
});
return {
isLoading,
isFetching,
data,
revalidate: () => {
data && client.invalidateQueries([{ queryKey: [url].filter((e) => e) }]);
},
};
};
export default useCustomAPIHook;