import cn from "classnames"; import EasyModal, { type InnerModalProps } from "ez-modal-react"; import { Field, Form, Formik } from "formik"; import { type ReactNode, useState } from "react"; import { Alert } from "react-bootstrap"; import Modal from "react-bootstrap/Modal"; import type { AccessList, AccessListClient, AccessListItem } from "src/api/backend"; import { AccessClientFields, BasicAuthFields, Button, Loading } from "src/components"; import { useAccessList, useSetAccessList } from "src/hooks"; import { intl, T } from "src/locale"; import { validateString } from "src/modules/Validations"; import { showObjectSuccess } from "src/notifications"; const showAccessListModal = (id: number | "new") => { EasyModal.show(AccessListModal, { id }); }; interface Props extends InnerModalProps { id: number | "new"; } const AccessListModal = EasyModal.create(({ id, visible, remove }: Props) => { const { data, isLoading, error } = useAccessList(id, ["items", "clients"]); const { mutate: setAccessList } = useSetAccessList(); const [errorMsg, setErrorMsg] = useState(null); const [isSubmitting, setIsSubmitting] = useState(false); const validate = (values: any): string | null => { // either Auths or Clients must be defined if (values.items?.length === 0 && values.clients?.length === 0) { return intl.formatMessage({ id: "error.access.at-least-one" }); } // ensure the items don't contain the same username twice const usernames = values.items.map((i: any) => i.username); const uniqueUsernames = Array.from(new Set(usernames)); if (usernames.length !== uniqueUsernames.length) { return intl.formatMessage({ id: "error.access.duplicate-usernames" }); } return null; }; const onSubmit = async (values: any, { setSubmitting }: any) => { if (isSubmitting) return; const vErr = validate(values); if (vErr) { setErrorMsg(vErr); return; } setIsSubmitting(true); setErrorMsg(null); const { ...payload } = { id: id === "new" ? undefined : id, ...values, }; // Filter out "items" to only use the "username" and "password" fields payload.items = (values.items || []).map((i: AccessListItem) => ({ username: i.username, password: i.password, })); // Filter out "clients" to only use the "directive" and "address" fields payload.clients = (values.clients || []).map((i: AccessListClient) => ({ directive: i.directive, address: i.address, })); setAccessList(payload, { onError: (err: any) => setErrorMsg(), onSuccess: () => { showObjectSuccess("access-list", "saved"); remove(); }, onSettled: () => { setIsSubmitting(false); setSubmitting(false); }, }); }; const toggleClasses = "form-check-input"; const toggleEnabled = cn(toggleClasses, "bg-cyan"); return ( {!isLoading && error && ( {error?.message || "Unknown error"} )} {isLoading && } {!isLoading && data && ( {({ setFieldValue }: any) => (
setErrorMsg(null)} dismissible> {errorMsg}
{({ field, form }: any) => (
{form.errors.name ? (
{form.errors.name && form.touched.name ? form.errors.name : null}
) : null}
)}

)}
)}
); }); export { showAccessListModal };