UNPKG

@selfcommunity/react-ui

Version:

React UI Components to integrate a Community created with SelfCommunity Platform.

64 lines (63 loc) 4.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = tslib_1.__importDefault(require("react")); const material_1 = require("@mui/material"); const system_1 = require("@mui/system"); const classnames_1 = tslib_1.__importDefault(require("classnames")); const react_core_1 = require("@selfcommunity/react-core"); const BaseDialog_1 = tslib_1.__importDefault(require("../../shared/BaseDialog")); const PdfPreview_1 = tslib_1.__importDefault(require("../PdfPreview")); const api_services_1 = require("@selfcommunity/api-services"); const utils_1 = require("@selfcommunity/utils"); const Errors_1 = require("../../constants/Errors"); const PREFIX = 'SCPdfPreviewDialog'; const classes = { root: `${PREFIX}-root`, content: `${PREFIX}-content` }; const Root = (0, material_1.styled)(BaseDialog_1.default, { slot: 'Root', name: PREFIX })(() => ({})); const Transition = react_1.default.forwardRef(function Transition(props, ref) { return (0, jsx_runtime_1.jsx)(material_1.Slide, Object.assign({ direction: "up", ref: ref }, props)); }); const NoTransition = function NoTransition(props) { return (0, jsx_runtime_1.jsx)(react_1.default.Fragment, { children: props.children }); }; function PdfPreviewDialog(inProps) { // PROPS const props = (0, system_1.useThemeProps)({ props: inProps, name: PREFIX }); const { className, title, pdfUrl, disableInitialTransition = false, onClose, PdfPreviewComponentProps = {} } = props, rest = tslib_1.__rest(props, ["className", "title", "pdfUrl", "disableInitialTransition", "onClose", "PdfPreviewComponentProps"]); // HOOKS const { isPaymentsEnabled } = (0, react_core_1.useSCPaymentsEnabled)(); /** * handle download pdf */ const handleDownload = () => tslib_1.__awaiter(this, void 0, void 0, function* () { try { const response = yield api_services_1.http.request({ url: pdfUrl, responseType: 'blob' }); const blob = new Blob([response.data], { type: 'application/pdf' }); const url = window.URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = 'order.pdf'; link.click(); // Cleanup window.URL.revokeObjectURL(url); } catch (error) { utils_1.Logger.error(Errors_1.SCOPE_SC_UI, error); } }); if (!isPaymentsEnabled) { return null; } return ((0, jsx_runtime_1.jsxs)(Root, Object.assign({ fullScreen: true, scroll: "paper", open: true, maxWidth: "md" }, (disableInitialTransition ? { TransitionComponent: NoTransition } : { TransitionComponent: Transition }), { className: (0, classnames_1.default)(classes.root, className), TransitionComponent: Transition, DialogContentProps: { sx: { overflow: 'hidden' } } }, rest, { children: [(0, jsx_runtime_1.jsx)(material_1.AppBar, Object.assign({ sx: { position: 'fixed', top: 0 } }, { children: (0, jsx_runtime_1.jsxs)(material_1.Toolbar, { children: [(0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "h6", component: "div", sx: { flexGrow: 1 } }, { children: title })), pdfUrl && ((0, jsx_runtime_1.jsx)(material_1.IconButton, Object.assign({ edge: "end", color: "inherit", onClick: handleDownload, "aria-label": "close" }, { children: (0, jsx_runtime_1.jsx)(material_1.Icon, { children: "download" }) }))), (0, jsx_runtime_1.jsx)(material_1.IconButton, Object.assign({ edge: "end", color: "inherit", onClick: onClose, "aria-label": "close", sx: { ml: 2 } }, { children: (0, jsx_runtime_1.jsx)(material_1.Icon, { children: "close" }) }))] }) })), (0, jsx_runtime_1.jsx)(material_1.Grid, Object.assign({ container: true, className: classes.content }, { children: (0, jsx_runtime_1.jsx)(material_1.Grid, Object.assign({ item: true, xs: 12, justifyContent: "center", alignContent: "center" }, { children: (0, jsx_runtime_1.jsx)(PdfPreview_1.default, Object.assign({}, PdfPreviewComponentProps, { pdfUrl: pdfUrl })) })) }))] }))); } exports.default = PdfPreviewDialog;