-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathuseSerialLogs.ts
More file actions
50 lines (42 loc) · 1.26 KB
/
useSerialLogs.ts
File metadata and controls
50 lines (42 loc) · 1.26 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
import { useQuery, UseQueryResult } from "react-query";
import type { SerialLogResponse, ApiResponse } from "../types/shared";
const useSerialLogs = (
brandId: string | undefined,
modelId: string | undefined,
urlSearchParams?: {
pageSize?: number;
nextPage?: string;
interval?: {
startTime: string;
endTime: string;
};
},
): UseQueryResult<ApiResponse<SerialLogResponse>, Error> => {
const url = new URL(
`/api/store/${brandId}/models/${modelId}/serial-log`,
window.location.origin,
);
if (urlSearchParams) {
const { interval, pageSize, nextPage } = urlSearchParams;
if (interval) {
url.searchParams.set("start-time", interval.startTime);
url.searchParams.set("end-time", interval.endTime);
}
if (pageSize) {
url.searchParams.set("page-size", pageSize.toString());
}
if (nextPage) {
url.searchParams.set("next-page", nextPage);
}
}
return useQuery<ApiResponse<SerialLogResponse>, Error>({
queryKey: ["serials", brandId, modelId, urlSearchParams],
queryFn: async () => {
const response = await fetch(url.toString());
const responseData = await response.json();
return responseData;
},
enabled: !!brandId,
});
};
export default useSerialLogs;