-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuse-showtimes.ts
More file actions
51 lines (45 loc) · 1.35 KB
/
use-showtimes.ts
File metadata and controls
51 lines (45 loc) · 1.35 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
import axios from 'axios';
import { useQuery, type QueryClient } from '@tanstack/react-query';
import { type ShowtimeReadRequest } from 'apis/data-contracts';
import { showtimesKey } from '@pages/movie-reservation/utils/showtimes-key';
async function getShowtimes(params: ShowtimeReadRequest) {
const { movieIds, date, timeSlot } = params;
const response = await axios.get(
'https://sopt37mega.kro.kr/api/v1/showtimes',
{
params: {
movieIds,
date,
timeSlot,
},
paramsSerializer: {
indexes: null,
},
}
);
return response.data.data?.cinemas;
}
/**
* 상영정보 조회 API
* @param movieIds 선택된 영화들
* @param date 선택된 날짜
* @param timeSlot 선택된 시간대
* @returns 조건에 해당하는 상영정보 배열
*/
export function useShowtimes(params: ShowtimeReadRequest) {
return useQuery({
queryKey: showtimesKey(params),
queryFn: () => getShowtimes(params),
enabled: params.movieIds && params.movieIds.length > 0,
staleTime: 1000 * 60,
});
}
export async function prefetchShowtimes(queryClient: QueryClient, params: ShowtimeReadRequest) {
if (!params.movieIds?.length) return;
await queryClient.prefetchQuery({
queryKey: showtimesKey(params),
queryFn: () => getShowtimes(params),
staleTime: 1000 * 60,
gcTime: 1000 * 60 * 5,
});
};