UNPKG

@rsc-labs/medusa-documents

Version:
86 lines (85 loc) 6.17 kB
"use strict"; 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 LogoFields = ({ logoSource, register }) => { const [logoUrl, setLogoUrl] = (0, react_1.useState)(logoSource); const [isValidUrl, setIsValidUrl] = (0, react_1.useState)(true); const [imgLoaded, setIsImageLoaded] = (0, react_1.useState)(false); const handleInputChange = (event) => { setLogoUrl(event.target.value); setIsValidUrl(true); }; const handleImageError = () => { setIsValidUrl(false); setIsImageLoaded(false); }; const handleOnLoad = (event) => { setIsImageLoaded(true); }; return ((0, jsx_runtime_1.jsxs)(material_1.Grid, { container: true, direction: 'column', spacing: 1, children: [(0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Label, { size: "small", children: "Link to logo" }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Input, { placeholder: 'https://raw.githubusercontent.com/RSC-Labs/medusa-store-analytics/main/docs/store-analytics-logo.PNG', ...register, defaultValue: logoSource, onChange: handleInputChange }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { container: true, justifyContent: 'center', alignContent: 'center', marginTop: 5, children: (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)("div", { style: { height: '200px', width: '300px', overflow: 'hidden', border: imgLoaded ? undefined : "1px solid rgba(0, 0, 0, 0.12)" }, children: logoUrl && isValidUrl && (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, textAlign: 'center', children: (0, jsx_runtime_1.jsx)("img", { src: logoUrl, alt: "Preview", style: { maxWidth: 300, maxHeight: 200 }, onLoad: handleOnLoad, onError: handleImageError }) }) }) }) })] })); }; const LogoForm = ({ logoSource, setOpenModal }) => { const { register, handleSubmit } = (0, react_hook_form_1.useForm)(); const { mutate } = (0, medusa_react_1.useAdminCustomPost)(`/document-settings/logo`, ["logo"]); const onSubmit = (data) => { return mutate({ logoSource: data.logoSource }, { onSuccess: async ({ response, settings }) => { if (response.status == 201) { if (settings && settings.store_logo_source) { ui_2.toast.success('Logo', { description: "New logo saved", }); } else { ui_2.toast.success('Logo', { description: "Logo removed", }); } setOpenModal(false); } else { ui_2.toast.error('Logo', { description: "Logo cannot be saved, some error happened.", }); } }, onError: ({}) => { ui_2.toast.error('Logo', { description: "Logo 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)(LogoFields, { logoSource: logoSource, register: register('logoSource') }), (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 LogoModalDetails = ({ setOpenModal }) => { 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 logo" }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(ui_1.Text, { children: "This logo 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 logo on document depends on template." }) }), (0, jsx_runtime_1.jsx)(material_1.Grid, { item: true, children: (0, jsx_runtime_1.jsx)(LogoForm, { logoSource: data.settings && data.settings.store_logo_source ? data.settings.store_logo_source : undefined, setOpenModal: setOpenModal }) })] }) })); }; const LogoChangeModal = () => { 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 logo" }) }), (0, jsx_runtime_1.jsxs)(ui_1.FocusModal.Content, { children: [(0, jsx_runtime_1.jsx)(ui_1.FocusModal.Header, {}), (0, jsx_runtime_1.jsx)(LogoModalDetails, { setOpenModal: setOpen })] })] })); }; exports.default = LogoChangeModal;