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.
136 lines
3.5 KiB
136 lines
3.5 KiB
2 years ago
|
import { Box } from '@mui/material';
|
||
|
import { FC, useMemo, useState } from 'react';
|
||
2 years ago
|
|
||
2 years ago
|
import api from '../lib/api';
|
||
|
import Autocomplete from './Autocomplete';
|
||
2 years ago
|
import CommonFenceInputGroup from './CommonFenceInputGroup';
|
||
2 years ago
|
import FlexBox from './FlexBox';
|
||
2 years ago
|
import handleAPIError from '../lib/handleAPIError';
|
||
2 years ago
|
import Spinner from './Spinner';
|
||
|
import { BodyText } from './Text';
|
||
2 years ago
|
import useIsFirstRender from '../hooks/useIsFirstRender';
|
||
|
import useProtectedState from '../hooks/useProtectedState';
|
||
2 years ago
|
|
||
2 years ago
|
type FenceAutocompleteOption = {
|
||
|
fenceDescription: string;
|
||
|
fenceId: string;
|
||
2 years ago
|
label: string;
|
||
|
};
|
||
|
|
||
2 years ago
|
const AddFenceInputGroup: FC = () => {
|
||
2 years ago
|
const isFirstRender = useIsFirstRender();
|
||
|
|
||
2 years ago
|
const [fenceTemplate, setFenceTemplate] = useProtectedState<
|
||
2 years ago
|
APIFenceTemplate | undefined
|
||
|
>(undefined);
|
||
2 years ago
|
const [fenceTypeValue, setInputFenceTypeValue] =
|
||
|
useState<FenceAutocompleteOption | null>(null);
|
||
2 years ago
|
const [isLoadingTemplate, setIsLoadingTemplate] =
|
||
|
useProtectedState<boolean>(true);
|
||
|
|
||
2 years ago
|
const fenceTypeOptions = useMemo<FenceAutocompleteOption[]>(
|
||
2 years ago
|
() =>
|
||
2 years ago
|
fenceTemplate
|
||
|
? Object.entries(fenceTemplate).map(
|
||
2 years ago
|
([id, { description: rawDescription }]) => {
|
||
|
const description =
|
||
|
typeof rawDescription === 'string'
|
||
|
? rawDescription
|
||
|
: 'No description.';
|
||
|
|
||
|
return {
|
||
2 years ago
|
fenceDescription: description,
|
||
|
fenceId: id,
|
||
2 years ago
|
label: id,
|
||
|
};
|
||
|
},
|
||
|
)
|
||
|
: [],
|
||
2 years ago
|
[fenceTemplate],
|
||
2 years ago
|
);
|
||
|
|
||
2 years ago
|
const fenceTypeElement = useMemo(
|
||
2 years ago
|
() => (
|
||
|
<Autocomplete
|
||
2 years ago
|
id="add-fence-select-type"
|
||
2 years ago
|
isOptionEqualToValue={(option, value) =>
|
||
2 years ago
|
option.fenceId === value.fenceId
|
||
2 years ago
|
}
|
||
|
label="Fence device type"
|
||
2 years ago
|
onChange={(event, newFenceType) => {
|
||
|
setInputFenceTypeValue(newFenceType);
|
||
2 years ago
|
}}
|
||
|
openOnFocus
|
||
2 years ago
|
options={fenceTypeOptions}
|
||
|
renderOption={(props, { fenceDescription, fenceId }, { selected }) => (
|
||
2 years ago
|
<Box
|
||
|
component="li"
|
||
|
sx={{
|
||
|
display: 'flex',
|
||
|
flexDirection: 'column',
|
||
|
|
||
|
'& > *': {
|
||
|
width: '100%',
|
||
|
},
|
||
|
}}
|
||
|
{...props}
|
||
|
>
|
||
|
<BodyText
|
||
|
inverted
|
||
|
sx={{
|
||
|
fontSize: '1.2em',
|
||
|
fontWeight: selected ? 400 : undefined,
|
||
|
}}
|
||
|
>
|
||
2 years ago
|
{fenceId}
|
||
2 years ago
|
</BodyText>
|
||
2 years ago
|
<BodyText selected={false}>{fenceDescription}</BodyText>
|
||
2 years ago
|
</Box>
|
||
|
)}
|
||
2 years ago
|
value={fenceTypeValue}
|
||
2 years ago
|
/>
|
||
|
),
|
||
2 years ago
|
[fenceTypeOptions, fenceTypeValue],
|
||
2 years ago
|
);
|
||
2 years ago
|
const fenceParameterElements = useMemo(
|
||
|
() => (
|
||
2 years ago
|
<CommonFenceInputGroup
|
||
|
fenceId={fenceTypeValue?.fenceId}
|
||
|
fenceTemplate={fenceTemplate}
|
||
2 years ago
|
/>
|
||
|
),
|
||
2 years ago
|
[fenceTemplate, fenceTypeValue],
|
||
2 years ago
|
);
|
||
2 years ago
|
|
||
|
const formContent = useMemo(
|
||
|
() =>
|
||
|
isLoadingTemplate ? (
|
||
|
<Spinner mt={0} />
|
||
|
) : (
|
||
2 years ago
|
<FlexBox sx={{ '& > div': { marginBottom: 0 } }}>
|
||
|
{fenceTypeElement}
|
||
2 years ago
|
{fenceParameterElements}
|
||
|
</FlexBox>
|
||
2 years ago
|
),
|
||
2 years ago
|
[fenceTypeElement, fenceParameterElements, isLoadingTemplate],
|
||
2 years ago
|
);
|
||
|
|
||
|
if (isFirstRender) {
|
||
|
api
|
||
|
.get<APIFenceTemplate>(`/fence/template`)
|
||
|
.then(({ data }) => {
|
||
2 years ago
|
setFenceTemplate(data);
|
||
2 years ago
|
})
|
||
|
.catch((error) => {
|
||
|
handleAPIError(error);
|
||
|
})
|
||
|
.finally(() => {
|
||
|
setIsLoadingTemplate(false);
|
||
|
});
|
||
|
}
|
||
|
|
||
2 years ago
|
return <>{formContent}</>;
|
||
2 years ago
|
};
|
||
|
|
||
2 years ago
|
export default AddFenceInputGroup;
|