// imports import { useInfiniteQuery } from "@tanstack/vue-query"; import axios from "~/configs/axios.config"; import { API_ENDPOINTS, QUERY_KEYS } from "~/constants"; import type { ComputedRef } from "vue"; // types export type SearchFileResponse = Branch; type HandlerProps = typeof initialPageParam & { signal: AbortSignal, search: string, id: number | undefined; sort: string | undefined; } // state const initialPageParam = { limit: 10, offset: 0 }; // methods export const handleSearchFile = async ({ search, offset, limit, id, signal, sort }: HandlerProps) => { const { data } = await axios.get(`${API_ENDPOINTS.branch.get}/${id}`, { params: { offset, limit, search, sort_by: sort }, signal }); return data; }; // composable const useSearchFile = (search: Ref, id: Ref, sort: ComputedRef) => { const enabled = computed(() => { return search.value.trim() != "" && !!id.value; }); return useInfiniteQuery({ enabled, retry: false, refetchOnMount: false, refetchOnWindowFocus: false, queryKey: [QUERY_KEYS.searchFile, search, id, sort], queryFn: ({ pageParam, signal }) => handleSearchFile({ ...pageParam, signal, search: search.value, sort: sort.value, id: id.value }), initialPageParam, getNextPageParam: (lastPage, pages) => { const page = pages.length + 1; const limit = initialPageParam.limit; const nextPageParams = { offset: page * limit - limit, limit }; return lastPage?.structure.next ? nextPageParams : undefined; } }); }; export default useSearchFile;