import { FC, useMemo, useRef, useState } from 'react'; import AddUpsInputGroup from './AddUpsInputGroup'; import api from '../../lib/api'; import ConfirmDialog from '../ConfirmDialog'; import FormDialog from '../FormDialog'; import handleAPIError from '../../lib/handleAPIError'; import List from '../List'; import { Panel, PanelHeader } from '../Panels'; import Spinner from '../Spinner'; import { HeaderText } from '../Text'; import useConfirmDialogProps from '../../hooks/useConfirmDialogProps'; import useIsFirstRender from '../../hooks/useIsFirstRender'; import useProtectedState from '../../hooks/useProtectedState'; const ManageUpsPanel: FC = () => { const isFirstRender = useIsFirstRender(); const confirmDialogRef = useRef({}); const formDialogRef = useRef({}); const [confirmDialogProps] = useConfirmDialogProps(); const [formDialogProps, setFormDialogProps] = useConfirmDialogProps(); const [isEditUpses, setIsEditUpses] = useState(false); const [isLoadingUpsTemplate, setIsLoadingUpsTemplate] = useProtectedState(true); const [upsTemplate, setUpsTemplate] = useProtectedState< APIUpsTemplate | undefined >(undefined); const listElement = useMemo( () => ( { setFormDialogProps({ actionProceedText: 'Add', content: , titleText: 'Add a UPS', }); formDialogRef.current.setOpen?.call(null, true); }} onEdit={() => { setIsEditUpses((previous) => !previous); }} /> ), [isEditUpses, setFormDialogProps, upsTemplate], ); const panelContent = useMemo( () => (isLoadingUpsTemplate ? : listElement), [isLoadingUpsTemplate, listElement], ); if (isFirstRender) { api .get('/ups/template') .then(({ data }) => { setUpsTemplate(data); }) .catch((error) => { handleAPIError(error); }) .finally(() => { setIsLoadingUpsTemplate(false); }); } return ( <> Manage Upses {panelContent} ); }; export default ManageUpsPanel;