@rsc-labs/medusa-documents
Version:
Generate documents from Medusa
92 lines (91 loc) • 6.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.InvoiceTemplatesTab = void 0;
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 ui_1 = require("@medusajs/ui");
const ui_2 = require("@medusajs/ui");
const react_1 = require("react");
const material_1 = require("@mui/material");
const template_kind_1 = require("../../types/template-kind");
const medusa_react_1 = require("medusa-react");
const ViewExampleInvoice = ({ kind }) => {
var _a, _b;
const { data, isLoading, isError, error } = (0, medusa_react_1.useAdminCustomQuery)(`/invoice/generate`, [], {
template: kind
});
if (isLoading) {
return ((0, jsx_runtime_1.jsx)(material_1.Grid, { container: true, justifyContent: 'center', children: (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(material_1.CircularProgress, { size: 12 }) }) }));
}
if (isError) {
const trueError = error;
if ((_b = (_a = trueError.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) {
return ((0, jsx_runtime_1.jsx)(ui_1.Alert, { variant: "error", children: trueError.response.data.message }));
}
else {
return ((0, jsx_runtime_1.jsx)(ui_1.Alert, { variant: "error", children: "Preview can't be generated" }));
}
}
if (data && data.buffer) {
const anyBuffer = data.buffer;
const blob = new Blob([new Uint8Array(anyBuffer.data)], { type: 'application/pdf' });
const pdfURL = URL.createObjectURL(blob);
return ((0, jsx_runtime_1.jsx)("iframe", { src: pdfURL, width: 660, height: 1000 }));
}
else {
return ((0, jsx_runtime_1.jsx)(ui_1.Alert, { variant: "error", children: "Preview can't be generated" }));
}
};
const ChooseTemplate = (props) => {
const handleChange = (checked) => {
props.setKind(checked);
};
return ((0, jsx_runtime_1.jsxs)(ui_2.RadioGroup, { onValueChange: handleChange, defaultValue: props.lastKind.toString(), children: [(0, jsx_runtime_1.jsxs)("div", { className: "flex items-center gap-x-3", children: [(0, jsx_runtime_1.jsx)(ui_2.RadioGroup.Item, { value: template_kind_1.InvoiceTemplateKind.BASIC.toString(), id: template_kind_1.InvoiceTemplateKind.BASIC.toString() }), (0, jsx_runtime_1.jsx)(ui_2.Label, { htmlFor: "radio_1", weight: "plus", children: "Basic" })] }), (0, jsx_runtime_1.jsxs)("div", { className: "flex items-center gap-x-3", children: [(0, jsx_runtime_1.jsx)(ui_2.RadioGroup.Item, { value: template_kind_1.InvoiceTemplateKind.BASIC_LOGO.toString(), id: template_kind_1.InvoiceTemplateKind.BASIC_LOGO.toString() }), (0, jsx_runtime_1.jsx)(ui_2.Label, { htmlFor: "radio_1", weight: "plus", children: "Basic with logo" })] })] }));
};
const TemplatesTabContent = ({ lastKind }) => {
const [templateKind, setTemplateKind] = (0, react_1.useState)(lastKind !== undefined && lastKind !== null ? lastKind : template_kind_1.InvoiceTemplateKind.BASIC);
const { mutate } = (0, medusa_react_1.useAdminCustomPost)(`/document-invoice-settings/invoice-template`, ["document-invoice-settings"]);
const onSubmit = () => {
return mutate({
invoiceTemplate: templateKind
}, {
onSuccess: async ({ response, settings }) => {
if (response.status == 201 && settings) {
ui_2.toast.success('Template', {
description: "New template saved",
});
}
else {
ui_2.toast.error('Template', {
description: "Template cannot be saved, some error happened.",
});
}
},
onError: ({}) => {
ui_2.toast.error('Template', {
description: "Template cannot be saved, some error happened.",
});
},
});
};
return ((0, jsx_runtime_1.jsxs)(material_1.Grid, { container: true, spacing: 2, children: [(0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, xs: 3, md: 3, xl: 3, children: (0, jsx_runtime_1.jsxs)(material_1.Grid, { container: true, rowSpacing: 3, children: [(0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, xs: 12, md: 12, xl: 12, children: (0, jsx_runtime_1.jsx)(ui_1.Alert, { children: "Preview is based on the last order" }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, xs: 12, md: 12, xl: 12, children: (0, jsx_runtime_1.jsx)(ui_2.Container, { children: (0, jsx_runtime_1.jsxs)(material_1.Grid, { container: true, rowSpacing: 3, direction: 'column', children: [(0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_2.Heading, { level: "h1", children: "Choose template" }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ChooseTemplate, { lastKind: templateKind, setKind: setTemplateKind }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_2.Button, { variant: "primary", onClick: onSubmit, children: "Save" }) })] }) }) })] }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, xs: 6, md: 6, xl: 6, children: (0, jsx_runtime_1.jsx)(ViewExampleInvoice, { kind: templateKind }) })] }));
};
const InvoiceTemplatesTab = () => {
var _a;
const { data, isLoading } = (0, medusa_react_1.useAdminCustomQuery)("/document-invoice-settings", [''], {});
if (isLoading) {
return ((0, jsx_runtime_1.jsx)(material_1.CircularProgress, { size: 12 }));
}
return ((0, jsx_runtime_1.jsx)(TemplatesTabContent, { lastKind: (_a = data === null || data === void 0 ? void 0 : data.settings) === null || _a === void 0 ? void 0 : _a.invoice_template }));
};
exports.InvoiceTemplatesTab = InvoiceTemplatesTab;