UNPKG

@rsc-labs/medusa-documents

Version:
64 lines (63 loc) 2.44 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 ViewPackingSlipDropdownButton = ({ order }) => { const { data, refetch } = (0, medusa_react_1.useAdminCustomQuery)("/packing-slip", [], { id: order.metadata['packing_slip_id'], includeBuffer: true }, { enabled: false }); const handleClick = async () => { const id = ui_1.toast.loading("Packing slip", { description: "Preparing...", 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("Packing slip", { description: 'Problem happened when preparing', }); } } catch (error) { ui_1.toast.dismiss(); ui_1.toast.error("Packing slip", { description: error, }); } finally { ui_1.toast.dismiss(); } }; const openPdf = (packingSlipResult) => { if (packingSlipResult && packingSlipResult.buffer) { const anyBuffer = packingSlipResult.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['packing_slip_id'] == undefined), children: [(0, jsx_runtime_1.jsx)(icons_1.DocumentText, {}), "View packing slip"] })); }; exports.default = ViewPackingSlipDropdownButton;