diff --git a/frontend/components/profile/purchases-and-orders/index/PurchasesTableRow.vue b/frontend/components/profile/purchases-and-orders/index/PurchasesTableRow.vue index 9e86e53..0b9696c 100644 --- a/frontend/components/profile/purchases-and-orders/index/PurchasesTableRow.vue +++ b/frontend/components/profile/purchases-and-orders/index/PurchasesTableRow.vue @@ -1,4 +1,8 @@ - + {{ data.order_id ? `${data.order_id}#` : "--" }} - + {{ data.created_at ?? "--" }} @@ -39,40 +42,35 @@ const { data } = toRefs(props); {{ data.verbose_status ? data.verbose_status : "--" }} - - - - - + + + diff --git a/frontend/composables/api/orders/useDownloadInvoice.ts b/frontend/composables/api/orders/useDownloadInvoice.ts new file mode 100644 index 0000000..15a79ea --- /dev/null +++ b/frontend/composables/api/orders/useDownloadInvoice.ts @@ -0,0 +1,59 @@ +// imports + +import { API_ENDPOINTS } from "~/constants"; +import { useQuery } from "@tanstack/vue-query"; + +const useDownloadInvoice = (transactionId: string) => { + // state + + const { $queryClient: queryClient, $axios: axios } = useNuxtApp(); + + const enabled = ref(false); + + // methods + + const download = () => { + if (!enabled.value) enabled.value = true; + queryClient.invalidateQueries({ + queryKey: ["download-invoice", transactionId], + }); + }; + + // query + + const handleDownloadInvoice = async () => { + const { data } = await axios.get(`${API_ENDPOINTS.orders.cart.download_invoice}/${transactionId}`, { + responseType: "blob", + }); + return data; + }; + + const { data: file, isLoading } = useQuery({ + queryKey: ["download-invoice", transactionId], + queryFn: () => handleDownloadInvoice(), + enabled, + refetchOnWindowFocus: false, + }); + + // watch + + whenever(file, () => { + if (!!file.value) { + const url = URL.createObjectURL(file.value); + const link = document.createElement("a"); + link.href = url; + link.download = "invoice.pdf"; + link.click(); + URL.revokeObjectURL(url); + + enabled.value = false; + } + }); + + return { + downloadIsLoading: isLoading, + downloadFn: download, + }; +}; + +export default useDownloadInvoice; diff --git a/frontend/constants/index.ts b/frontend/constants/index.ts index f5b7256..b8f540a 100644 --- a/frontend/constants/index.ts +++ b/frontend/constants/index.ts @@ -56,6 +56,7 @@ export const API_ENDPOINTS = { orders: { get_all: "/order/all", cart: { + download_invoice: "/order/invoice", get_all: "/order/cart", delete_one: "/order/cart/item", delete_all: "/order/cart/all",