-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
get-discussions.ts
42 lines (37 loc) · 905 Bytes
/
get-discussions.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
import { queryOptions, useQuery } from '@tanstack/react-query';
import { api } from '@/lib/api-client';
import { QueryConfig } from '@/lib/react-query';
import { Discussion, Meta } from '@/types/api';
export const getDiscussions = (
page = 1,
): Promise<{
data: Discussion[];
meta: Meta;
}> => {
return api.get(`/discussions`, {
params: {
page,
},
});
};
export const getDiscussionsQueryOptions = ({
page,
}: { page?: number } = {}) => {
return queryOptions({
queryKey: page ? ['discussions', { page }] : ['discussions'],
queryFn: () => getDiscussions(page),
});
};
type UseDiscussionsOptions = {
page?: number;
queryConfig?: QueryConfig<typeof getDiscussionsQueryOptions>;
};
export const useDiscussions = ({
queryConfig,
page,
}: UseDiscussionsOptions) => {
return useQuery({
...getDiscussionsQueryOptions({ page }),
...queryConfig,
});
};