import { useEffect, useState } from "react"; import { Button, Checkbox, FormControl, FormErrorMessage, FormLabel, Input, Modal, ModalOverlay, ModalContent, ModalHeader, ModalCloseButton, ModalBody, ModalFooter, Select, Stack, useToast, } from "@chakra-ui/react"; import Ajv, { Schema } from "ajv"; import { DNSProvider, DNSProvidersAcmesh, DNSProvidersAcmeshProperty, } from "api/npm"; import { PrettyButton } from "components"; import { Formik, Form, Field, getIn } from "formik"; import { useSetDNSProvider, useDNSProvidersAcmesh } from "hooks"; import { intl } from "locale"; import { validateString } from "modules/Validations"; interface DNSProviderCreateModalProps { isOpen: boolean; onClose: () => void; } function DNSProviderCreateModal({ isOpen, onClose, }: DNSProviderCreateModalProps) { const toast = useToast(); const { mutate: setDNSProvider } = useSetDNSProvider(); const { isLoading: acmeshIsLoading, data: acmeshDataResp } = useDNSProvidersAcmesh(); const [acmeshData, setAcmeshData] = useState([] as DNSProvidersAcmesh[]); const [acmeshItem, setAcmeshItem] = useState(""); useEffect(() => { setAcmeshData(acmeshDataResp || []); }, [acmeshDataResp]); const onModalClose = () => { setAcmeshItem(""); onClose(); }; const getAcmeshItem = (name: string): DNSProvidersAcmesh | undefined => { return acmeshData.find((item) => item.title === name); }; const fullItem = getAcmeshItem(acmeshItem); const itemProperties = fullItem?.properties; const onSubmit = async ( payload: DNSProvider, { setErrors, setSubmitting }: any, ) => { const showErr = (msg: string) => { toast({ description: intl.formatMessage({ id: `error.${msg}`, }), status: "error", position: "top", duration: 3000, isClosable: true, }); }; const ajv = new Ajv({ strictSchema: false }); try { const valid = ajv.validate(fullItem as Schema, payload.meta); if (!valid) { let errs: any = {}; ajv.errors?.forEach((e: any) => { errs["meta"] = { [e.instancePath.substring(1)]: e.message, }; }); setErrors(errs); setSubmitting(false); } else { // Json schema is happy setDNSProvider(payload, { onError: (err: any) => { if (err.message === "ca-bundle-does-not-exist") { setErrors({ caBundle: intl.formatMessage({ id: `error.${err.message}`, }), }); } else { showErr(err.message); } }, onSuccess: () => onModalClose(), onSettled: () => setSubmitting(false), }); } } catch (err: any) { showErr(err); setSubmitting(false); } }; const renderInputType = ( field: any, fieldName: string, f: DNSProvidersAcmeshProperty, value: any, ) => { if (["bool", "boolean"].indexOf(f.type) !== -1) { return ( {f.title} ); } let type = "text"; let props: any = {}; if (f.type === "string") { props.minLength = f.minLength || null; props.maxLength = f.maxLength || null; props.pattern = f.pattern || null; } if (f.type === "integer") { type = "number"; props.min = f.minimum || null; props.max = f.maximum || null; } if (f.isSecret) { type = "password"; } return ( ); }; return ( {acmeshIsLoading ? ( "loading" ) : ( {({ isSubmitting, handleChange, values, setValues }) => (
{intl.formatMessage({ id: "dns-provider.create" })} {({ field, form }: any) => ( {intl.formatMessage({ id: "dns-provider.acmesh-name", })} {form.errors.acmeshName} )} {acmeshItem !== "" ? ( <> {({ field, form }: any) => ( {intl.formatMessage({ id: "dns-provider.name", })} {form.errors.name} )} {itemProperties ? Object.keys(itemProperties).map((fieldName, _) => { const f = itemProperties[fieldName]; const name = `meta[${fieldName}]`; return ( {({ field, form }: any) => ( {f.type !== "bool" ? ( {intl.formatMessage({ id: `acmesh-property.${f.title}`, })} ) : null} {renderInputType( field, fieldName, f, values.meta[f.title], )} {form.errors?.meta?.[fieldName]} )} ); }) : null} ) : null} {intl.formatMessage({ id: "form.save" })} )}
)}
); } export { DNSProviderCreateModal };