import { keepPreviousData, queryOptions } from "@tanstack/react-query";
import { api } from "../apiHelper";

export function getActiveLoadingOrders() {
	return queryOptions({
		queryKey: ["getActiveLoadingOrders"],
		queryFn: () => dataFetch(),
		staleTime: 5000,
		refetchOnWindowFocus: true,
		placeholderData: keepPreviousData,
	});
}

const dataFetch = async () => {
	const { data } = await api.get("/dockDoor/activeLoadingOrders");
	if (!data.success) {
		throw new Error(data.message ?? "Failed to load articles");
	}

	return data.data ?? [];
};
