|
|
@ -1,38 +1,25 @@ |
|
|
|
import { Box } from '@mui/material'; |
|
|
|
import { Box } from '@mui/material'; |
|
|
|
import { FC, useMemo, useState } from 'react'; |
|
|
|
import { FC, useMemo, useState } from 'react'; |
|
|
|
|
|
|
|
|
|
|
|
import api from '../lib/api'; |
|
|
|
|
|
|
|
import Autocomplete from './Autocomplete'; |
|
|
|
import Autocomplete from './Autocomplete'; |
|
|
|
import CommonFenceInputGroup from './CommonFenceInputGroup'; |
|
|
|
import CommonFenceInputGroup from './CommonFenceInputGroup'; |
|
|
|
import FlexBox from './FlexBox'; |
|
|
|
import FlexBox from './FlexBox'; |
|
|
|
import handleAPIError from '../lib/handleAPIError'; |
|
|
|
|
|
|
|
import Spinner from './Spinner'; |
|
|
|
import Spinner from './Spinner'; |
|
|
|
import { BodyText } from './Text'; |
|
|
|
import { BodyText } from './Text'; |
|
|
|
import useIsFirstRender from '../hooks/useIsFirstRender'; |
|
|
|
|
|
|
|
import useProtectedState from '../hooks/useProtectedState'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type FenceAutocompleteOption = { |
|
|
|
const AddFenceInputGroup: FC<AddFenceInputGroupProps> = ({ |
|
|
|
fenceDescription: string; |
|
|
|
fenceTemplate: externalFenceTemplate, |
|
|
|
fenceId: string; |
|
|
|
loading: isExternalLoading, |
|
|
|
label: string; |
|
|
|
}) => { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const AddFenceInputGroup: FC = () => { |
|
|
|
|
|
|
|
const isFirstRender = useIsFirstRender(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const [fenceTemplate, setFenceTemplate] = useProtectedState< |
|
|
|
|
|
|
|
APIFenceTemplate | undefined |
|
|
|
|
|
|
|
>(undefined); |
|
|
|
|
|
|
|
const [fenceTypeValue, setInputFenceTypeValue] = |
|
|
|
const [fenceTypeValue, setInputFenceTypeValue] = |
|
|
|
useState<FenceAutocompleteOption | null>(null); |
|
|
|
useState<FenceAutocompleteOption | null>(null); |
|
|
|
const [isLoadingTemplate, setIsLoadingTemplate] = |
|
|
|
|
|
|
|
useProtectedState<boolean>(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const fenceTypeOptions = useMemo<FenceAutocompleteOption[]>( |
|
|
|
const fenceTypeOptions = useMemo<FenceAutocompleteOption[]>( |
|
|
|
() => |
|
|
|
() => |
|
|
|
fenceTemplate |
|
|
|
externalFenceTemplate |
|
|
|
? Object.entries(fenceTemplate).map( |
|
|
|
? Object.entries(externalFenceTemplate) |
|
|
|
([id, { description: rawDescription }]) => { |
|
|
|
.sort(([a], [b]) => (a > b ? 1 : -1)) |
|
|
|
|
|
|
|
.map(([id, { description: rawDescription }]) => { |
|
|
|
const description = |
|
|
|
const description = |
|
|
|
typeof rawDescription === 'string' |
|
|
|
typeof rawDescription === 'string' |
|
|
|
? rawDescription |
|
|
|
? rawDescription |
|
|
@ -43,10 +30,9 @@ const AddFenceInputGroup: FC = () => { |
|
|
|
fenceId: id, |
|
|
|
fenceId: id, |
|
|
|
label: id, |
|
|
|
label: id, |
|
|
|
}; |
|
|
|
}; |
|
|
|
}, |
|
|
|
}) |
|
|
|
) |
|
|
|
|
|
|
|
: [], |
|
|
|
: [], |
|
|
|
[fenceTemplate], |
|
|
|
[externalFenceTemplate], |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const fenceTypeElement = useMemo( |
|
|
|
const fenceTypeElement = useMemo( |
|
|
@ -96,40 +82,26 @@ const AddFenceInputGroup: FC = () => { |
|
|
|
() => ( |
|
|
|
() => ( |
|
|
|
<CommonFenceInputGroup |
|
|
|
<CommonFenceInputGroup |
|
|
|
fenceId={fenceTypeValue?.fenceId} |
|
|
|
fenceId={fenceTypeValue?.fenceId} |
|
|
|
fenceTemplate={fenceTemplate} |
|
|
|
fenceTemplate={externalFenceTemplate} |
|
|
|
/> |
|
|
|
/> |
|
|
|
), |
|
|
|
), |
|
|
|
[fenceTemplate, fenceTypeValue], |
|
|
|
[externalFenceTemplate, fenceTypeValue], |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const formContent = useMemo( |
|
|
|
const content = useMemo( |
|
|
|
() => |
|
|
|
() => |
|
|
|
isLoadingTemplate ? ( |
|
|
|
isExternalLoading ? ( |
|
|
|
<Spinner mt={0} /> |
|
|
|
<Spinner /> |
|
|
|
) : ( |
|
|
|
) : ( |
|
|
|
<FlexBox sx={{ '& > div': { marginBottom: 0 } }}> |
|
|
|
<FlexBox> |
|
|
|
{fenceTypeElement} |
|
|
|
{fenceTypeElement} |
|
|
|
{fenceParameterElements} |
|
|
|
{fenceParameterElements} |
|
|
|
</FlexBox> |
|
|
|
</FlexBox> |
|
|
|
), |
|
|
|
), |
|
|
|
[fenceTypeElement, fenceParameterElements, isLoadingTemplate], |
|
|
|
[fenceTypeElement, fenceParameterElements, isExternalLoading], |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if (isFirstRender) { |
|
|
|
return <>{content}</>; |
|
|
|
api |
|
|
|
|
|
|
|
.get<APIFenceTemplate>(`/fence/template`) |
|
|
|
|
|
|
|
.then(({ data }) => { |
|
|
|
|
|
|
|
setFenceTemplate(data); |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.catch((error) => { |
|
|
|
|
|
|
|
handleAPIError(error); |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.finally(() => { |
|
|
|
|
|
|
|
setIsLoadingTemplate(false); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return <>{formContent}</>; |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
export default AddFenceInputGroup; |
|
|
|
export default AddFenceInputGroup; |
|
|
|