// imports import { useInfiniteQuery } from "@tanstack/vue-query"; import { API_ENDPOINTS, QUERY_KEYS } from "~/constants"; import { useAuth } from "~/composables/api/auth/useAuth"; // types export type GetChatResponse = ApiPaginated; const useGetChat = (productId: string | number, enabled: Ref) => { // state const { $axios: axios } = useNuxtApp(); const { isLoggedIn } = useAuth(); const isEnabled = computed(() => { return enabled.value && isLoggedIn.value; }); // methods const handleGetChat = async ({ productId, limit, offset, }: { productId: number | string; limit: number; offset: number; }) => { const { data } = await axios.get(`${API_ENDPOINTS.chat.messages}/${productId}`, { params: { offset, limit, }, headers: { Authorization: `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoyMTY3ODE2OTAwLCJpYXQiOjE3MzU4MTY5MDAsImp0aSI6ImQwN2E2Y2Y2NzgwZjRlNTE5NWIzOGQxMTAzYzU4NDQ3IiwidXNlcl9pZCI6NX0.slwd7ZSV7nUXEuDTYwwHUOo9ekCefwEEL4kVv2vSTFo`, }, }); return data; }; return useInfiniteQuery({ enabled: isEnabled, queryKey: [QUERY_KEYS.chat], initialPageParam: { limit: 10, offset: 0, }, queryFn: ({ pageParam }) => handleGetChat({ limit: pageParam.limit, offset: pageParam.offset, productId: productId, }), getNextPageParam: (lastPage, pages) => { if (!lastPage.next) return undefined; return { limit: 10, offset: pages.length * 10, }; }, }); }; export default useGetChat;