import { ActionIcon, Box, Card, Group, LoadingOverlay, Text, TextInput, } from "@mantine/core"; import { useForm, zodResolver } from "@mantine/form"; import { IconAt, IconMinus, IconPlus } from "@tabler/icons-react"; import { useEffect, useMemo, useState } from "react"; import { z } from "zod"; import { getConfig, upsertConfig } from "../../apis/config"; import { IConfig } from "../../system/type"; import { useConfirmStore } from "../../lib/zustand/use-confirm"; import { useDisclosure } from "@mantine/hooks"; const schema = z.object({ email: z .string({ message: "Email is required" }) .email({ message: "Invalid email address" }), }); const MailInput = ({ initValue, onDelete, onAdd, }: { initValue?: string; onDelete?: (data: string) => void; onAdd?: (data: string) => Promise; }) => { const form = useForm({ initialValues: { email: initValue || "", }, validate: zodResolver(schema), }); return (
{ await onAdd(values.email); form.reset(); } : () => {} )} className="flex items-start gap-2 w-full" > } placeholder="Enter email" className="flex-1" size="xs" /> onDelete(initValue) : undefined} type={!initValue ? "submit" : "button"} color={initValue ? "red" : "blue"} variant="light" > {initValue ? : } ); }; export default function MailsConfig() { const [config, setConfig] = useState(null); const { setConfirm } = useConfirmStore(); const [opened, { open, close }] = useDisclosure(false); useEffect(() => { fetchConfig(); }, []); const mails = useMemo(() => { if (!config) return []; return config?.value?.split(", ").length > 0 ? config?.value.split(",") : []; }, [config]); const fetchConfig = async () => { const response = await getConfig("MAIL_SCRAP_REPORT"); if (!response || ![200, 201].includes(response.data?.status_code)) return; setConfig(response.data.data); }; const handleDelete = (mail: string) => { setConfirm({ message: "Are you want to delete: " + mail, title: "Delete", handleOk: async () => { open(); const newMails = mails.filter((item) => item !== mail); if (!config) return; const response = await upsertConfig({ ...(config as IConfig), value: newMails.join(", "), }); if (response) { fetchConfig(); } close(); }, }); }; const handleAdd = async (mail: string) => { const newMails = [...mails, mail]; open(); const response = await upsertConfig({ ...(config as IConfig), value: newMails.join(", "), }); if (response) { fetchConfig(); } close(); }; return ( Mails {mails.length > 0 && mails.map((mail) => { return ( ); })} ); }