56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
// features/invoices/view-pdf/hooks/use-download-invoice-pdf.ts
|
|
|
|
import { useDataSource } from "@erp/core/hooks";
|
|
import { type QueryKey, useQuery } from "@tanstack/react-query";
|
|
|
|
import { downloadInvoicePDFApi } from "../api";
|
|
|
|
export const ISSUED_INVOICE_QUERY_KEY = (id: string): QueryKey => ["issued_invoice", id] as const;
|
|
|
|
type DownloadInvoicePDFOptions = {
|
|
enabled?: boolean;
|
|
};
|
|
|
|
export function useDownloadInvoicePDFQuery(
|
|
invoiceId?: string,
|
|
options?: DownloadInvoicePDFOptions
|
|
) {
|
|
const dataSource = useDataSource();
|
|
const enabled = (options?.enabled ?? true) && !!invoiceId;
|
|
|
|
return useQuery({
|
|
queryKey: ISSUED_INVOICE_QUERY_KEY(invoiceId ?? "unknown"),
|
|
queryFn: async (context) => {
|
|
if (!invoiceId) throw new Error("invoiceId is required");
|
|
|
|
const { signal } = context;
|
|
return await downloadInvoicePDFApi(dataSource, invoiceId, {
|
|
signal,
|
|
});
|
|
},
|
|
enabled,
|
|
staleTime: 0,
|
|
refetchOnWindowFocus: false,
|
|
});
|
|
}
|
|
|
|
/*
|
|
export function useQuery<
|
|
TQueryFnData = unknown,
|
|
TError = unknown,
|
|
TData = TQueryFnData,
|
|
TQueryKey extends QueryKey = QueryKey
|
|
>
|
|
|
|
TQueryFnData: the type returned from the queryFn.
|
|
TError: the type of Errors to expect from the queryFn.
|
|
TData: the type our data property will eventually have.
|
|
Only relevant if you use the select option,
|
|
because then the data property can be different
|
|
from what the queryFn returns.
|
|
Otherwise, it will default to whatever the queryFn returns.
|
|
TQueryKey: the type of our queryKey, only relevant
|
|
if you use the queryKey that is passed to your queryFn.
|
|
|
|
*/
|