@lynsoluciones/medusa-docs
Version:
Medusa plugins to generate docs
126 lines (125 loc) • 10.5 kB
JavaScript
;
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");
/*
*
*
* 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("Configuración de la factura", {
description: "Nueva configuración de facturas guardada",
});
setOpenModal(false);
}
else {
ui_2.toast.error("Configuración de la factura", {
description: "La nueva configuración de la factura no se puede guardar, ocurrió algún error.",
});
}
},
onError: ({}) => {
ui_2.toast.error("Configuración de la factura", {
description: "La nueva configuración de la factura no se puede guardar, ocurrió algún error.",
});
},
});
};
const INVOICE_NUMBER_PLACEHOLDER = "{invoice_number}";
const errorText = `El texto ${INVOICE_NUMBER_PLACEHOLDER} debe incluirse en la entrada.`;
const LABEL_MUST_FORMAT = `El formato debe incluir ${INVOICE_NUMBER_PLACEHOLDER}`;
const LABEL_MUST_FORCED = `El número forzado debe ser un número`;
const LABEL_INFO_FORCED = `Se incrementará automáticamente a partir de este número.`;
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: "Formato de n\u00FAmero" }) }), (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: "N\u00FAmero forzado" }) }), (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: "Su pr\u00F3ximo n\u00FAmero de factura ser\u00E1:" }) }), 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: "Guardar" }) }), (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: "Configuraci\u00F3n de la factura" }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Text, { children: "Esta configuraci\u00F3n se aplicar\u00E1 a las facturas reci\u00E9n generadas." }) }), (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: "Cambiar la configuraci\u00F3n" }) }), (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;