-
Notifications
You must be signed in to change notification settings - Fork 59
/
reactQuery.ts
143 lines (126 loc) · 3.2 KB
/
reactQuery.ts
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { api } from './api';
import {
useInfiniteQuery,
useMutation,
useQuery,
useQueryClient,
UseQueryOptions,
} from 'react-query';
import { QueryFunctionContext } from 'react-query/types/core/types';
import { AxiosError, AxiosResponse } from 'axios';
import { GetInfinitePagesInterface } from '../interfaces';
type QueryKeyT = [string, object | undefined];
export const fetcher = <T>({
queryKey,
pageParam,
}: QueryFunctionContext<QueryKeyT>): Promise<T> => {
const [url, params] = queryKey;
return api
.get<T>(url, { params: { ...params, pageParam } })
.then((res) => res.data);
};
export const useLoadMore = <T>(url: string | null, params?: object) => {
const context = useInfiniteQuery<
GetInfinitePagesInterface<T>,
Error,
GetInfinitePagesInterface<T>,
QueryKeyT
>(
[url!, params],
({ queryKey, pageParam = 1 }) => fetcher({ queryKey, pageParam }),
{
getPreviousPageParam: (firstPage) => firstPage.previousId ?? false,
getNextPageParam: (lastPage) => {
return lastPage.nextId ?? false;
},
}
);
return context;
};
export const usePrefetch = <T>(url: string | null, params?: object) => {
const queryClient = useQueryClient();
return () => {
if (!url) {
return;
}
queryClient.prefetchQuery<T, Error, T, QueryKeyT>(
[url!, params],
({ queryKey }) => fetcher({ queryKey })
);
};
};
export const useFetch = <T>(
url: string | null,
params?: object,
config?: UseQueryOptions<T, Error, T, QueryKeyT>
) => {
const context = useQuery<T, Error, T, QueryKeyT>(
[url!, params],
({ queryKey }) => fetcher({ queryKey }),
{
enabled: !!url,
...config,
}
);
return context;
};
const useGenericMutation = <T, S>(
func: (data: T | S) => Promise<AxiosResponse<S>>,
url: string,
params?: object,
updater?: ((oldData: T, newData: S) => T) | undefined
) => {
const queryClient = useQueryClient();
return useMutation<AxiosResponse, AxiosError, T | S>(func, {
onMutate: async (data) => {
await queryClient.cancelQueries([url!, params]);
const previousData = queryClient.getQueryData([url!, params]);
queryClient.setQueryData<T>([url!, params], (oldData) => {
return updater ? updater(oldData!, data as S) : (data as T);
});
return previousData;
},
onError: (err, _, context) => {
queryClient.setQueryData([url!, params], context);
},
onSettled: () => {
queryClient.invalidateQueries([url!, params]);
},
});
};
export const useDelete = <T>(
url: string,
params?: object,
updater?: (oldData: T, id: string | number) => T
) => {
return useGenericMutation<T, string | number>(
(id) => api.delete(`${url}/${id}`),
url,
params,
updater
);
};
export const usePost = <T, S>(
url: string,
params?: object,
updater?: (oldData: T, newData: S) => T
) => {
return useGenericMutation<T, S>(
(data) => api.post<S>(url, data),
url,
params,
updater
);
};
export const useUpdate = <T, S>(
url: string,
params?: object,
updater?: (oldData: T, newData: S) => T
) => {
return useGenericMutation<T, S>(
(data) => api.patch<S>(url, data),
url,
params,
updater
);
};