You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
337 lines
9.3 KiB
337 lines
9.3 KiB
2 years ago
|
import { Box } from '@mui/material';
|
||
|
import {
|
||
|
FC,
|
||
|
FormEventHandler,
|
||
|
ReactElement,
|
||
|
useMemo,
|
||
|
useRef,
|
||
|
useState,
|
||
|
} from 'react';
|
||
2 years ago
|
|
||
2 years ago
|
import API_BASE_URL from '../../lib/consts/API_BASE_URL';
|
||
2 years ago
|
|
||
|
import AddFenceInputGroup from './AddFenceInputGroup';
|
||
2 years ago
|
import api from '../../lib/api';
|
||
2 years ago
|
import { ID_SEPARATOR } from './CommonFenceInputGroup';
|
||
2 years ago
|
import ConfirmDialog from '../ConfirmDialog';
|
||
2 years ago
|
import EditFenceInputGroup from './EditFenceInputGroup';
|
||
2 years ago
|
import FlexBox from '../FlexBox';
|
||
|
import handleAPIError from '../../lib/handleAPIError';
|
||
|
import List from '../List';
|
||
|
import { Panel, PanelHeader } from '../Panels';
|
||
|
import periodicFetch from '../../lib/fetchers/periodicFetch';
|
||
|
import Spinner from '../Spinner';
|
||
2 years ago
|
import {
|
||
|
BodyText,
|
||
|
HeaderText,
|
||
|
InlineMonoText,
|
||
|
MonoText,
|
||
2 years ago
|
SensitiveText,
|
||
2 years ago
|
SmallText,
|
||
2 years ago
|
} from '../Text';
|
||
|
import useIsFirstRender from '../../hooks/useIsFirstRender';
|
||
|
import useProtectedState from '../../hooks/useProtectedState';
|
||
2 years ago
|
|
||
|
type FormFenceParameterData = {
|
||
|
fenceAgent: string;
|
||
|
fenceName: string;
|
||
|
parameterInputs: {
|
||
|
[parameterInputId: string]: {
|
||
|
isParameterSensitive: boolean;
|
||
|
parameterId: string;
|
||
|
parameterType: string;
|
||
|
parameterValue: string;
|
||
|
};
|
||
|
};
|
||
|
};
|
||
2 years ago
|
|
||
2 years ago
|
const fenceParameterBooleanToString = (value: boolean) => (value ? '1' : '0');
|
||
|
|
||
|
const getFormFenceParameters = (
|
||
|
fenceTemplate: APIFenceTemplate,
|
||
|
...[{ target }]: Parameters<FormEventHandler<HTMLDivElement>>
|
||
|
) => {
|
||
|
const { elements } = target as HTMLFormElement;
|
||
|
|
||
2 years ago
|
return Object.values(elements).reduce<FormFenceParameterData>(
|
||
2 years ago
|
(previous, formElement) => {
|
||
|
const { id: inputId } = formElement;
|
||
|
const reExtract = new RegExp(`^(fence[^-]+)${ID_SEPARATOR}([^\\s]+)$`);
|
||
|
const matched = inputId.match(reExtract);
|
||
|
|
||
|
if (matched) {
|
||
|
const [, fenceId, parameterId] = matched;
|
||
|
|
||
|
previous.fenceAgent = fenceId;
|
||
|
|
||
|
const inputElement = formElement as HTMLInputElement;
|
||
2 years ago
|
const {
|
||
|
checked,
|
||
|
dataset: { sensitive: rawSensitive },
|
||
|
value,
|
||
|
} = inputElement;
|
||
|
|
||
|
if (parameterId === 'name') {
|
||
|
previous.fenceName = value;
|
||
|
}
|
||
|
|
||
2 years ago
|
const {
|
||
|
[fenceId]: {
|
||
|
parameters: {
|
||
|
[parameterId]: { content_type: parameterType = 'string' } = {},
|
||
|
},
|
||
|
},
|
||
|
} = fenceTemplate;
|
||
|
|
||
2 years ago
|
previous.parameterInputs[inputId] = {
|
||
|
isParameterSensitive: rawSensitive === 'true',
|
||
|
parameterId,
|
||
|
parameterType,
|
||
|
parameterValue:
|
||
2 years ago
|
parameterType === 'boolean'
|
||
|
? fenceParameterBooleanToString(checked)
|
||
|
: value,
|
||
|
};
|
||
|
}
|
||
|
|
||
|
return previous;
|
||
|
},
|
||
2 years ago
|
{ fenceAgent: '', fenceName: '', parameterInputs: {} },
|
||
2 years ago
|
);
|
||
|
};
|
||
|
|
||
2 years ago
|
const buildConfirmFenceParameters = (
|
||
|
parameterInputs: FormFenceParameterData['parameterInputs'],
|
||
|
) => (
|
||
|
<List
|
||
|
listItems={parameterInputs}
|
||
|
listItemProps={{ sx: { padding: 0 } }}
|
||
|
renderListItem={(
|
||
|
parameterInputId,
|
||
|
{ isParameterSensitive, parameterId, parameterValue },
|
||
|
) => {
|
||
|
let textElement: ReactElement;
|
||
|
|
||
|
if (parameterValue) {
|
||
|
textElement = isParameterSensitive ? (
|
||
|
<SensitiveText monospaced>{parameterValue}</SensitiveText>
|
||
|
) : (
|
||
|
<Box sx={{ maxWidth: '100%', overflowX: 'scroll' }}>
|
||
|
<MonoText lineHeight={2.8} whiteSpace="nowrap">
|
||
|
{parameterValue}
|
||
|
</MonoText>
|
||
|
</Box>
|
||
|
);
|
||
|
} else {
|
||
|
textElement = <SmallText>none</SmallText>;
|
||
|
}
|
||
|
|
||
|
return (
|
||
|
<FlexBox
|
||
|
fullWidth
|
||
|
growFirst
|
||
|
height="2.8em"
|
||
|
key={`confirm-${parameterInputId}`}
|
||
|
maxWidth="100%"
|
||
|
row
|
||
|
>
|
||
|
<BodyText>{parameterId}</BodyText>
|
||
|
{textElement}
|
||
|
</FlexBox>
|
||
|
);
|
||
|
}}
|
||
|
/>
|
||
|
);
|
||
|
|
||
2 years ago
|
const ManageFencePanel: FC = () => {
|
||
2 years ago
|
const isFirstRender = useIsFirstRender();
|
||
|
|
||
|
const confirmDialogRef = useRef<ConfirmDialogForwardedRefContent>({});
|
||
2 years ago
|
const formDialogRef = useRef<ConfirmDialogForwardedRefContent>({});
|
||
2 years ago
|
|
||
|
const [confirmDialogProps, setConfirmDialogProps] =
|
||
|
useState<ConfirmDialogProps>({
|
||
|
actionProceedText: '',
|
||
|
content: '',
|
||
|
titleText: '',
|
||
|
});
|
||
2 years ago
|
const [formDialogProps, setFormDialogProps] = useState<ConfirmDialogProps>({
|
||
|
actionProceedText: '',
|
||
|
content: '',
|
||
|
titleText: '',
|
||
|
});
|
||
2 years ago
|
const [fenceTemplate, setFenceTemplate] = useProtectedState<
|
||
|
APIFenceTemplate | undefined
|
||
|
>(undefined);
|
||
|
const [isEditFences, setIsEditFences] = useState<boolean>(false);
|
||
|
const [isLoadingFenceTemplate, setIsLoadingFenceTemplate] =
|
||
|
useProtectedState<boolean>(true);
|
||
|
|
||
|
const { data: fenceOverviews, isLoading: isFenceOverviewsLoading } =
|
||
|
periodicFetch<APIFenceOverview>(`${API_BASE_URL}/fence`, {
|
||
|
refreshInterval: 60000,
|
||
|
});
|
||
|
|
||
|
const listElement = useMemo(
|
||
|
() => (
|
||
|
<List
|
||
|
allowEdit
|
||
|
allowItemButton={isEditFences}
|
||
|
edit={isEditFences}
|
||
|
header
|
||
|
listItems={fenceOverviews}
|
||
|
onAdd={() => {
|
||
2 years ago
|
setFormDialogProps({
|
||
2 years ago
|
actionProceedText: 'Add',
|
||
|
content: <AddFenceInputGroup fenceTemplate={fenceTemplate} />,
|
||
2 years ago
|
onSubmitAppend: (event) => {
|
||
|
if (!fenceTemplate) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
2 years ago
|
const addData = getFormFenceParameters(fenceTemplate, event);
|
||
|
|
||
|
setConfirmDialogProps({
|
||
|
actionProceedText: 'Add',
|
||
|
content: buildConfirmFenceParameters(addData.parameterInputs),
|
||
|
titleText: (
|
||
|
<HeaderText>
|
||
|
Add a{' '}
|
||
|
<InlineMonoText fontSize="inherit">
|
||
|
{addData.fenceAgent}
|
||
|
</InlineMonoText>{' '}
|
||
|
fence device with the following parameters?
|
||
|
</HeaderText>
|
||
|
),
|
||
|
});
|
||
|
|
||
|
confirmDialogRef.current.setOpen?.call(null, true);
|
||
2 years ago
|
},
|
||
2 years ago
|
titleText: 'Add a fence device',
|
||
|
});
|
||
|
|
||
2 years ago
|
formDialogRef.current.setOpen?.call(null, true);
|
||
2 years ago
|
}}
|
||
|
onEdit={() => {
|
||
|
setIsEditFences((previous) => !previous);
|
||
|
}}
|
||
|
onItemClick={({ fenceAgent: fenceId, fenceName, fenceParameters }) => {
|
||
2 years ago
|
setFormDialogProps({
|
||
2 years ago
|
actionProceedText: 'Update',
|
||
|
content: (
|
||
|
<EditFenceInputGroup
|
||
|
fenceId={fenceId}
|
||
|
fenceTemplate={fenceTemplate}
|
||
|
previousFenceName={fenceName}
|
||
|
previousFenceParameters={fenceParameters}
|
||
|
/>
|
||
|
),
|
||
2 years ago
|
onSubmitAppend: (event) => {
|
||
|
if (!fenceTemplate) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
2 years ago
|
const editData = getFormFenceParameters(fenceTemplate, event);
|
||
|
|
||
|
setConfirmDialogProps({
|
||
|
actionProceedText: 'Update',
|
||
|
content: buildConfirmFenceParameters(editData.parameterInputs),
|
||
|
titleText: (
|
||
|
<HeaderText>
|
||
|
Update{' '}
|
||
|
<InlineMonoText fontSize="inherit">
|
||
|
{editData.fenceName}
|
||
|
</InlineMonoText>{' '}
|
||
|
fence device with the following parameters?
|
||
|
</HeaderText>
|
||
|
),
|
||
|
});
|
||
|
|
||
|
confirmDialogRef.current.setOpen?.call(null, true);
|
||
2 years ago
|
},
|
||
2 years ago
|
titleText: (
|
||
|
<HeaderText>
|
||
|
Update fence device{' '}
|
||
2 years ago
|
<InlineMonoText fontSize="inherit">{fenceName}</InlineMonoText>{' '}
|
||
2 years ago
|
parameters
|
||
|
</HeaderText>
|
||
|
),
|
||
2 years ago
|
});
|
||
|
|
||
2 years ago
|
formDialogRef.current.setOpen?.call(null, true);
|
||
2 years ago
|
}}
|
||
|
renderListItem={(
|
||
|
fenceUUID,
|
||
|
{ fenceAgent, fenceName, fenceParameters },
|
||
|
) => (
|
||
|
<FlexBox row>
|
||
|
<BodyText>{fenceName}</BodyText>
|
||
|
<BodyText>
|
||
|
{Object.entries(fenceParameters).reduce<string>(
|
||
|
(previous, [parameterId, parameterValue]) =>
|
||
|
`${previous} ${parameterId}="${parameterValue}"`,
|
||
|
fenceAgent,
|
||
|
)}
|
||
|
</BodyText>
|
||
|
</FlexBox>
|
||
|
)}
|
||
|
/>
|
||
|
),
|
||
|
[fenceOverviews, fenceTemplate, isEditFences],
|
||
|
);
|
||
|
const panelContent = useMemo(
|
||
|
() =>
|
||
|
isLoadingFenceTemplate || isFenceOverviewsLoading ? (
|
||
|
<Spinner />
|
||
|
) : (
|
||
|
listElement
|
||
|
),
|
||
|
[isFenceOverviewsLoading, isLoadingFenceTemplate, listElement],
|
||
|
);
|
||
|
|
||
|
if (isFirstRender) {
|
||
|
api
|
||
|
.get<APIFenceTemplate>(`/fence/template`)
|
||
|
.then(({ data }) => {
|
||
|
setFenceTemplate(data);
|
||
|
})
|
||
|
.catch((error) => {
|
||
|
handleAPIError(error);
|
||
|
})
|
||
|
.finally(() => {
|
||
|
setIsLoadingFenceTemplate(false);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
return (
|
||
|
<>
|
||
|
<Panel>
|
||
|
<PanelHeader>
|
||
|
<HeaderText>Manage fence devices</HeaderText>
|
||
|
</PanelHeader>
|
||
|
{panelContent}
|
||
|
</Panel>
|
||
|
<ConfirmDialog
|
||
|
dialogProps={{
|
||
|
PaperProps: { sx: { minWidth: { xs: '90%', md: '50em' } } },
|
||
|
}}
|
||
|
formContent
|
||
2 years ago
|
scrollBoxProps={{
|
||
|
padding: '.3em .5em',
|
||
|
}}
|
||
|
scrollContent
|
||
|
{...formDialogProps}
|
||
|
ref={formDialogRef}
|
||
|
/>
|
||
|
<ConfirmDialog
|
||
|
scrollBoxProps={{ paddingRight: '1em' }}
|
||
2 years ago
|
scrollContent
|
||
2 years ago
|
{...confirmDialogProps}
|
||
|
ref={confirmDialogRef}
|
||
|
/>
|
||
|
</>
|
||
|
);
|
||
|
};
|
||
|
|
||
2 years ago
|
export default ManageFencePanel;
|