feat: Download invoice pdf of the payment link

This commit is contained in:
Ahmed Bouhuolia
2024-10-05 13:56:25 +02:00
parent c5ff1e4d4a
commit 2649f1c326
6 changed files with 173 additions and 4 deletions

View File

@@ -11,6 +11,7 @@ import useApiRequest from '../useRequest';
import { transformToCamelCase, transfromToSnakeCase } from '@/utils';
const GetPaymentLinkInvoice = 'GetPaymentLinkInvoice';
const GetPaymentLinkInvoicePdf = 'GetPaymentLinkInvoicePdf';
// Create Payment Link
// ------------------------------------
@@ -170,3 +171,56 @@ export const useCreateStripeCheckoutSession = (
{ ...options },
);
};
// Get Payment Link Invoice PDF
// ------------------------------------
interface GetPaymentLinkInvoicePdfResponse {}
interface GeneratePaymentLinkInvoicePdfValues {
paymentLinkId: string;
}
export const useGeneratePaymentLinkInvoicePdf = (
options?: UseMutationOptions<
GetPaymentLinkInvoicePdfResponse,
Error,
GeneratePaymentLinkInvoicePdfValues
>,
): UseMutationResult<
GetPaymentLinkInvoicePdfResponse,
Error,
GeneratePaymentLinkInvoicePdfValues
> => {
const apiRequest = useApiRequest();
return useMutation<
GetPaymentLinkInvoicePdfResponse,
Error,
GeneratePaymentLinkInvoicePdfValues
>(
(values: GeneratePaymentLinkInvoicePdfValues) => {
return apiRequest
.get(`/payment-links/${values.paymentLinkId}/invoice/pdf`)
.then((res) => res?.data);
},
{ ...options },
);
};
export const useGetPaymentLinkInvoicePdf = (
invoiceId: string,
options?: UseQueryOptions<GetPaymentLinkInvoicePdfResponse, Error>,
): UseQueryResult<GetPaymentLinkInvoicePdfResponse, Error> => {
const apiRequest = useApiRequest();
return useQuery<GetPaymentLinkInvoicePdfResponse, Error>(
[GetPaymentLinkInvoicePdf, invoiceId],
() =>
apiRequest
.get(`/payment-links/${invoiceId}/invoice/pdf`)
.then((res) => res.data),
{
...options,
},
);
};

View File

@@ -33,9 +33,15 @@ export const useDownloadFile = (args: IArgs) => {
return { ...mutation };
};
export function downloadFile(data, filename, mime, bom) {
export function downloadFile(
data,
filename,
mime = 'application/octet-stream',
bom?: any,
) {
var blobData = typeof bom !== 'undefined' ? [bom, data] : [data];
var blob = new Blob(blobData, { type: mime || 'application/octet-stream' });
var blob = new Blob(blobData, { type: mime });
if (typeof window.navigator.msSaveBlob !== 'undefined') {
// IE workaround for "HTML7007: One or more blob URLs were
// revoked by closing the blob for which they were created.