import { FC, useRef, useState } from 'react'; import API_BASE_URL from '../../lib/consts/API_BASE_URL'; import AddMailServerForm from './AddMailServerForm'; import api from '../../lib/api'; import { DialogWithHeader } from '../Dialog'; import EditMailServerForm from './EditMailServerForm'; import FormSummary from '../FormSummary'; import List from '../List'; import { ExpandablePanel } from '../Panels'; import periodicFetch from '../../lib/fetchers/periodicFetch'; import { BodyText } from '../Text'; import useActiveFetch from '../../hooks/useActiveFetch'; import useChecklist from '../../hooks/useChecklist'; import useConfirmDialog from '../../hooks/useConfirmDialog'; import useFetch from '../../hooks/useFetch'; const ManageMailServer: FC = () => { const addDialogRef = useRef(null); const editDialogRef = useRef(null); const { confirmDialog, setConfirmDialogOpen, setConfirmDialogProps } = useConfirmDialog({ initial: { closeOnProceed: true } }); const [edit, setEdit] = useState(false); const [mailServer, setMailServer] = useState< APIMailServerDetail | undefined >(); const [mailServers, setMailServers] = useState< APIMailServerOverviewList | undefined >(); const { isLoading: loadingMailServersPeriodic } = periodicFetch(`${API_BASE_URL}/mail-server`, { onSuccess: (data) => setMailServers(data), }); const { fetch: getMailServers, loading: loadingMailServersActive } = useActiveFetch({ onData: (data) => setMailServers(data), url: '/mail-server', }); const { fetch: getMailServer, loading: loadingMailServer } = useActiveFetch({ onData: (data) => setMailServer(data), url: '/mail-server', }); const { data: host, loading: loadingHost } = useFetch('/host/local'); const { buildDeleteDialogProps, checks, getCheck, hasAllChecks, hasChecks, multipleItems, resetChecks, setAllChecks, setCheck, } = useChecklist({ list: mailServers }); return ( <> ({ checked: hasAllChecks, onChange: (event, checked) => setAllChecks(checked), })} getListItemCheckboxProps={(uuid) => ({ checked: getCheck(uuid), onChange: (event, checked) => setCheck(uuid, checked), })} header listEmpty="No mail server(s) found." listItems={mailServers} loading={loadingMailServersPeriodic || loadingMailServersActive} onAdd={() => addDialogRef?.current?.setOpen(true)} onDelete={() => { setConfirmDialogProps({ ...buildDeleteDialogProps({ onProceedAppend: () => { Promise.all( checks.map((uuid) => api.delete(`/mail-server/${uuid}`)), ).then(() => getMailServers()); resetChecks(); }, getConfirmDialogTitle: (count) => `Delete the following ${count} mail server(s)?`, renderEntry: ({ key }) => { const ms = mailServers?.[key]; return ( {ms?.address}:{ms?.port} ); }, }), }); setConfirmDialogOpen(true); }} onEdit={() => setEdit((previous) => !previous)} onItemClick={(value, uuid) => { editDialogRef?.current?.setOpen(true); getMailServer(`/${uuid}`); }} renderListItem={(uuid, { address, port }) => ( {address}:{port} )} /> {host && ( { setConfirmDialogProps({ actionProceedText: 'Add', content: , onCancelAppend: () => tools.onConfirmCancel(...args), onProceedAppend: () => tools.onConfirmProceed(...args), titleText: 'Add mail server with the following?', }); setConfirmDialogOpen(true); }} /> )} {mailServer && ( { setConfirmDialogProps({ actionProceedText: 'Update', content: , onCancelAppend: () => tools.onConfirmCancel(...args), onProceedAppend: () => tools.onConfirmProceed(...args), titleText: 'Update mail server with the following?', }); setConfirmDialogOpen(true); }} previousFormikValues={{ [mailServer.uuid]: mailServer }} /> )} {confirmDialog} ); }; export default ManageMailServer;