-
Notifications
You must be signed in to change notification settings - Fork 0
/
useLiveLessonsListInfiniteQuery.ts
46 lines (44 loc) · 1.24 KB
/
useLiveLessonsListInfiniteQuery.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
import { getLivesList, GET_LIVES_LIST_LIMIT } from '@apis/v1/get-lives-list'
import useAuthState from '@hooks/useAuthState'
import { useInfiniteQuery } from '@tanstack/react-query'
import { merge } from 'lodash'
import { PartialDeep } from 'type-fest'
const useLiveLessonsListInfiniteQuery = (
props?: PartialDeep<Parameters<typeof getLivesList>[0]>,
) => {
const { auth } = useAuthState()
return useInfiniteQuery({
queryKey: ['liveLessonsList', props],
queryFn: async context => {
const { offset = 0, limit = GET_LIVES_LIST_LIMIT } = context.pageParam ?? {}
const finalProps = merge(
{
userId: auth.userId,
offset: offset,
limit,
filter: {
status: 'ongoing' as any,
},
},
props ?? {},
)
const { items, count } = await getLivesList(finalProps)
return {
items,
offset,
limit,
count,
}
},
getNextPageParam: lastPage => {
const { count, limit, offset } = lastPage
const newOffset = offset + limit
if (newOffset >= count) return
return {
offset: newOffset,
limit: GET_LIVES_LIST_LIMIT,
}
},
})
}
export default useLiveLessonsListInfiniteQuery