UNPKG

@rsc-labs/medusa-documents

Version:
64 lines (63 loc) 2.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); /* * Copyright 2024 RSC-Labs, https://rsoftcon.com/ * * MIT License * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const icons_1 = require("@medusajs/icons"); const ui_1 = require("@medusajs/ui"); const medusa_react_1 = require("medusa-react"); const ViewInvoiceDropdownButton = ({ order }) => { const { data, refetch } = (0, medusa_react_1.useAdminCustomQuery)("/invoice", [], { invoiceId: order.metadata['invoice_id'], includeBuffer: true }, { enabled: false }); const handleClick = async () => { ui_1.toast.loading("Invoice", { description: "Preparing invoice...", duration: Infinity, }); try { const result = await refetch(); if (result.data && result.data.buffer) { ui_1.toast.dismiss(); openPdf(result.data); } else { ui_1.toast.dismiss(); ui_1.toast.error("Invoice", { description: 'Problem happened when preparing invoice', }); } } catch (error) { ui_1.toast.dismiss(); ui_1.toast.error("Invoice", { description: error, }); } finally { ui_1.toast.dismiss(); } }; const openPdf = (invoiceResult) => { if (invoiceResult && invoiceResult.buffer) { const anyBuffer = invoiceResult.buffer; const blob = new Blob([new Uint8Array(anyBuffer.data)], { type: 'application/pdf' }); const pdfURL = URL.createObjectURL(blob); window.open(pdfURL, '_blank'); } }; return ((0, jsx_runtime_1.jsxs)(ui_1.DropdownMenu.Item, { className: "gap-x-2", onClick: handleClick, disabled: (order.metadata['invoice_id'] == undefined), children: [(0, jsx_runtime_1.jsx)(icons_1.DocumentText, {}), "View invoice"] })); }; exports.default = ViewInvoiceDropdownButton;