@rsc-labs/medusa-documents
Version:
Generate documents from Medusa
66 lines (65 loc) • 5.8 kB
JavaScript
;
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 AddressField = ({ name, placeholder, initValue, register }) => {
return ((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: name }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Input, { placeholder: placeholder, ...register, defaultValue: initValue }) })] }));
};
const AddressForm = ({ address, setOpenModal }) => {
const { register, handleSubmit } = (0, react_hook_form_1.useForm)();
const { mutate } = (0, medusa_react_1.useAdminCustomPost)(`/document-settings/document-address`, ["document-settings"]);
const onSubmit = (data) => {
return mutate({
address: data
}, {
onSuccess: async ({ response, settings }) => {
if (response.status == 201 && settings) {
ui_2.toast.success('Address', {
description: "New address saved",
});
setOpenModal(false);
}
else {
ui_2.toast.error('Address', {
description: "Address cannot be saved, some error happened.",
});
}
},
onError: ({}) => {
ui_2.toast.error('Address', {
description: "Address cannot be saved, some error happened.",
});
},
});
};
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.jsx)(AddressField, { name: "Company name", placeholder: 'My store', register: register('company'), initValue: address === null || address === void 0 ? void 0 : address.company }), (0, jsx_runtime_1.jsx)(AddressField, { name: "First name", placeholder: 'John', register: register('first_name'), initValue: address === null || address === void 0 ? void 0 : address.first_name }), (0, jsx_runtime_1.jsx)(AddressField, { name: "Last name", placeholder: 'Doe', register: register('last_name'), initValue: address === null || address === void 0 ? void 0 : address.last_name }), (0, jsx_runtime_1.jsx)(AddressField, { name: "Address", placeholder: '56 Street', register: register('address_1'), initValue: address === null || address === void 0 ? void 0 : address.address_1 }), (0, jsx_runtime_1.jsx)(AddressField, { name: "City", placeholder: 'Warsaw', register: register('city'), initValue: address === null || address === void 0 ? void 0 : address.city }), (0, jsx_runtime_1.jsx)(AddressField, { name: "Postal code", placeholder: '55-200', register: register('postal_code'), initValue: address === null || address === void 0 ? void 0 : address.postal_code }), (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" }) })] }) }));
};
const AddressModalDetails = ({ setOpenModal }) => {
var _a;
const { data, isLoading } = (0, medusa_react_1.useAdminCustomQuery)("/document-settings", [''], {});
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: "Store address" }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Text, { children: "This address will be used on your documents." }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Text, { children: "Presence of field on document depends on template." }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(AddressForm, { address: (_a = data === null || data === void 0 ? void 0 : data.settings) === null || _a === void 0 ? void 0 : _a.store_address, setOpenModal: setOpenModal }) })] }) }));
};
const AddressChangeModal = () => {
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 address" }) }), (0, jsx_runtime_1.jsxs)(ui_1.FocusModal.Content, { children: [(0, jsx_runtime_1.jsx)(ui_1.FocusModal.Header, {}), (0, jsx_runtime_1.jsx)(AddressModalDetails, { setOpenModal: setOpen })] })] }));
};
exports.default = AddressChangeModal;