1
2import { useQuery } from "@tanstack/react-query";
3import { fetch } from "@marzneshin/common/utils";
4import {
5 FetchEntityReturn,
6 UseEntityQueryProps,
7 EntityQueryKeyType
8} from "@marzneshin/libs/entity-table";
9import { InboundType } from "../types";
10
11export async function fetchInbounds({ queryKey }: EntityQueryKeyType): FetchEntityReturn<InboundType> {
12 const pagination = queryKey[1];
13 const primaryFilter = queryKey[2];
14 const filters = queryKey[4].filters;
15 return fetch(`/inbounds`, {
16 query: {
17 ...pagination,
18 ...filters,
19 tag: primaryFilter,
20 descending: queryKey[3].desc,
21 order_by: queryKey[3].sortBy,
22 }
23 }).then((result) => {
24 return {
25 entities: result.items,
26 pageCount: result.pages
27 };
28 })
29}
30
31export const InboundsQueryFetchKey = "inbounds";
32
33export const useInboundsQuery = ({
34 page, size, sortBy = "created_at", desc = false, filters = {}
35}: UseEntityQueryProps) => {
36 return useQuery({
37 queryKey: [InboundsQueryFetchKey, { page, size }, filters?.tag ?? "", { sortBy, desc }, { filters }],
38 queryFn: fetchInbounds,
39 initialData: { entities: [], pageCount: 0 }
40 })
41}