69 lines
1.8 KiB
TypeScript
69 lines
1.8 KiB
TypeScript
// imports
|
|
|
|
import { useInfiniteQuery } from "@tanstack/vue-query";
|
|
import { API_ENDPOINTS, QUERY_KEYS } from "~/constants";
|
|
import { useAuth } from "~/composables/api/auth/useAuth";
|
|
|
|
// types
|
|
|
|
export type GetBranchResponse = ApiPaginated<Chat>;
|
|
|
|
const useGetBranch = (productId: string | number, enabled: Ref<boolean>) => {
|
|
// state
|
|
|
|
const { $axios: axios } = useNuxtApp();
|
|
|
|
const { isLoggedIn } = useAuth();
|
|
|
|
// methods
|
|
|
|
const handleGetChat = async ({
|
|
productId,
|
|
limit,
|
|
offset,
|
|
}: {
|
|
productId: number | string;
|
|
limit: number;
|
|
offset: number;
|
|
}) => {
|
|
const { data } = await axios.get<GetBranchResponse>(
|
|
`${API_ENDPOINTS.chat.messages}/${productId}`,
|
|
{
|
|
params: {
|
|
offset,
|
|
limit,
|
|
},
|
|
headers: {
|
|
Authorization: `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoyMTY3ODE2OTAwLCJpYXQiOjE3MzU4MTY5MDAsImp0aSI6ImQwN2E2Y2Y2NzgwZjRlNTE5NWIzOGQxMTAzYzU4NDQ3IiwidXNlcl9pZCI6NX0.slwd7ZSV7nUXEuDTYwwHUOo9ekCefwEEL4kVv2vSTFo`,
|
|
},
|
|
}
|
|
);
|
|
return data;
|
|
};
|
|
|
|
return useInfiniteQuery({
|
|
enabled: isLoggedIn,
|
|
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 useGetBranch;
|