UNPKG

@rsc-labs/medusa-documents

Version:
116 lines (115 loc) 10 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; 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 ui_1 = require("@medusajs/ui"); const material_1 = require("@mui/material"); const medusa_react_1 = require("medusa-react"); const react_hook_form_1 = require("react-hook-form"); const ui_2 = require("@medusajs/ui"); const react_1 = require("react"); const settings_invoice_display_number_1 = __importDefault(require("./settings-invoice-display-number")); const lodash_1 = require("lodash"); const InvoiceSettingsForm = ({ invoiceSettings, setOpenModal }) => { const { register, handleSubmit, formState: { errors } } = (0, react_hook_form_1.useForm)(); const [formatNumber, setFormatNumber] = (0, react_1.useState)(invoiceSettings === null || invoiceSettings === void 0 ? void 0 : invoiceSettings.invoice_number_format); const [forcedNumber, setForcedNumber] = (0, react_1.useState)(invoiceSettings === null || invoiceSettings === void 0 ? void 0 : invoiceSettings.invoice_forced_number); const [error, setError] = (0, react_1.useState)(undefined); const { mutate } = (0, medusa_react_1.useAdminCustomPost)(`/document-invoice-settings`, ['document-invoice-settings']); const onSubmit = (data) => { return mutate({ formatNumber: data.formatNumber, forcedNumber: data.forcedNumber !== undefined && data.forcedNumber.toString().length ? data.forcedNumber : undefined }, { onSuccess: async ({ response, settings }) => { if (response.status == 201 && settings) { ui_2.toast.success('Invoice settings', { description: "New invoice settings saved", }); setOpenModal(false); } else { ui_2.toast.error('Invoice settings', { description: "New invoice settings cannot be saved, some error happened.", }); } }, onError: ({}) => { ui_2.toast.error('Invoice settings', { description: "New invoice settings cannot be saved, some error happened.", }); }, }); }; const INVOICE_NUMBER_PLACEHOLDER = '{invoice_number}'; const errorText = `Text ${INVOICE_NUMBER_PLACEHOLDER} needs to be included in input.`; const LABEL_MUST_FORMAT = `Format must include ${INVOICE_NUMBER_PLACEHOLDER}`; const LABEL_MUST_FORCED = `Forced number must be a number`; const LABEL_INFO_FORCED = `It will auto-increment starting from this number.`; const validateFormatNumber = (value) => { if (!value.includes(INVOICE_NUMBER_PLACEHOLDER)) { return LABEL_MUST_FORMAT; } return true; }; const validateForcedNumber = (value) => { if (value.length && isNaN(Number(value))) { return LABEL_MUST_FORCED; } return true; }; return ((0, jsx_runtime_1.jsx)("form", { children: (0, jsx_runtime_1.jsxs)(material_1.Grid, { container: true, direction: 'column', rowSpacing: 4, paddingTop: 8, children: [(0, jsx_runtime_1.jsxs)(material_1.Grid, { container: true, direction: 'column', spacing: 1, marginTop: 2, children: [(0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsxs)(material_1.Grid, { container: true, direction: 'column', children: [(0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Label, { size: "small", children: "Number format" }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Label, { size: 'xsmall', children: LABEL_MUST_FORMAT }) })] }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Input, { placeholder: INVOICE_NUMBER_PLACEHOLDER, defaultValue: (invoiceSettings === null || invoiceSettings === void 0 ? void 0 : invoiceSettings.invoice_number_format) ? invoiceSettings.invoice_number_format : INVOICE_NUMBER_PLACEHOLDER, ...register('formatNumber', { validate: validateFormatNumber, onChange(e) { const value = e.target.value; if ((0, lodash_1.isBoolean)(validateFormatNumber(value))) { setError(undefined); setFormatNumber(value); } else { setError(validateFormatNumber(value)); } }, }) }) })] }), (0, jsx_runtime_1.jsxs)(material_1.Grid, { container: true, direction: 'column', spacing: 1, marginTop: 2, children: [(0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsxs)(material_1.Grid, { container: true, direction: 'column', children: [(0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Label, { size: "small", children: "Forced number" }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Label, { size: 'xsmall', children: LABEL_INFO_FORCED }) })] }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Input, { defaultValue: (invoiceSettings === null || invoiceSettings === void 0 ? void 0 : invoiceSettings.invoice_forced_number) !== undefined && invoiceSettings.invoice_forced_number !== null ? invoiceSettings.invoice_forced_number : '', type: "number", ...register('forcedNumber', { validate: validateForcedNumber, onChange(e) { const value = e.target.value; if ((0, lodash_1.isBoolean)(validateForcedNumber(value))) { setError(undefined); setForcedNumber(value); } else { setError(validateForcedNumber(value)); } }, }) }) })] }), (0, jsx_runtime_1.jsxs)(material_1.Grid, { container: true, direction: 'column', spacing: 1, marginTop: 2, children: [(0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Label, { size: "small", children: "Your next invoice number will be:" }) }), errors.formatNumber == undefined && errors.forcedNumber == undefined && error == undefined && (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(settings_invoice_display_number_1.default, { formatNumber: formatNumber, forcedNumber: forcedNumber !== undefined && forcedNumber !== null ? parseInt(forcedNumber) : undefined }) })] }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Button, { type: "submit", variant: 'primary', onClick: handleSubmit(onSubmit), children: "Save" }) }), (errors.formatNumber || errors.forcedNumber) && (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Alert, { variant: "error", children: errorText }) }), error && (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Alert, { variant: "error", children: error }) })] }) })); }; const InvoiceSettingsModalDetails = ({ setOpenModal }) => { const { data, isLoading } = (0, medusa_react_1.useAdminCustomQuery)("/document-invoice-settings", [], {}, { refetchOnMount: "always", cacheTime: 0 }); if (isLoading) { return ((0, jsx_runtime_1.jsx)(ui_1.FocusModal.Body, { children: (0, jsx_runtime_1.jsx)(material_1.CircularProgress, {}) })); } return ((0, jsx_runtime_1.jsx)(ui_1.FocusModal.Body, { children: (0, jsx_runtime_1.jsxs)(material_1.Grid, { container: true, direction: 'column', alignContent: 'center', paddingTop: 8, children: [(0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Heading, { children: "Invoice settings" }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Text, { children: "These settings will be applied for newly generated invoices." }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(InvoiceSettingsForm, { invoiceSettings: data === null || data === void 0 ? void 0 : data.settings, setOpenModal: setOpenModal }) })] }) })); }; const InvoiceSettingsModal = () => { const [open, setOpen] = (0, react_1.useState)(false); return ((0, jsx_runtime_1.jsxs)(ui_1.FocusModal, { open: open, onOpenChange: setOpen, children: [(0, jsx_runtime_1.jsx)(ui_1.FocusModal.Trigger, { asChild: true, children: (0, jsx_runtime_1.jsx)(ui_1.Button, { children: "Change settings" }) }), (0, jsx_runtime_1.jsxs)(ui_1.FocusModal.Content, { children: [(0, jsx_runtime_1.jsx)(ui_1.FocusModal.Header, {}), (0, jsx_runtime_1.jsx)(InvoiceSettingsModalDetails, { setOpenModal: setOpen })] })] })); }; exports.default = InvoiceSettingsModal;