From bea7ad25c6b5e36736e1052a502673780543fb7e Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 17 Feb 2023 22:08:15 -0500 Subject: [PATCH] fix(striker-ui): enable pass previous values to CommonFenceInputGroup --- .../components/CommonFenceInputGroup.tsx | 41 +++++++++++++++---- striker-ui/types/APIFence.d.ts | 13 ++++++ striker-ui/types/CommonFenceInputGroup.d.ts | 2 + 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/striker-ui/components/CommonFenceInputGroup.tsx b/striker-ui/components/CommonFenceInputGroup.tsx index ce0f63e9..809eed0a 100644 --- a/striker-ui/components/CommonFenceInputGroup.tsx +++ b/striker-ui/components/CommonFenceInputGroup.tsx @@ -40,9 +40,13 @@ const MAP_TO_INPUT_BUILDER: MapToInputBuilder = { ), }; +const combineIds = (...pieces: string[]) => pieces.join('-'); + const CommonFenceInputGroup: FC = ({ fenceId, fenceTemplate, + previousFenceName, + previousFenceParameters, }) => { const fenceParameterElements = useMemo(() => { let result: ReactNode; @@ -50,6 +54,20 @@ const CommonFenceInputGroup: FC = ({ if (fenceTemplate && fenceId) { const { parameters: fenceParameters } = fenceTemplate[fenceId]; + let mapToPreviousFenceParameterValues: FenceParameters = {}; + + if (previousFenceParameters) { + mapToPreviousFenceParameterValues = Object.entries( + previousFenceParameters, + ).reduce((previous, [parameterId, parameterValue]) => { + const newKey = combineIds(fenceId, parameterId); + + previous[newKey] = parameterValue; + + return previous; + }, {}); + } + const { optional: optionalInputs, required: requiredInputs } = Object.entries(fenceParameters).reduce<{ optional: ReactElement[]; @@ -71,15 +89,18 @@ const CommonFenceInputGroup: FC = ({ const buildInput = MAP_TO_INPUT_BUILDER[contentType] ?? MAP_TO_INPUT_BUILDER.string; - const fenceJoinParameterId = `${fenceId}-${parameterId}`; + const fenceJoinParameterId = combineIds(fenceId, parameterId); + const initialValue = + mapToPreviousFenceParameterValues[fenceJoinParameterId] ?? + parameterDefault; const parameterIsRequired = isRequired === '1'; const parameterInput = buildInput({ id: fenceJoinParameterId, - isChecked: CHECKED_STATES.includes(parameterDefault), + isChecked: CHECKED_STATES.includes(initialValue), isRequired: parameterIsRequired, label: parameterId, selectOptions: parameterSelectOptions, - value: parameterDefault, + value: initialValue, }); if (parameterIsRequired) { @@ -94,28 +115,34 @@ const CommonFenceInputGroup: FC = ({ optional: [], required: [ MAP_TO_INPUT_BUILDER.string({ - id: `${fenceId}-name`, + id: combineIds(fenceId, 'name'), isRequired: true, label: 'Fence device name', + value: previousFenceName, }), ], }, ); result = ( - <> + div:first-child': { marginTop: 0 }, + '& > div': { marginBottom: 0 }, + }} + > {requiredInputs} {optionalInputs} - + ); } return result; - }, [fenceId, fenceTemplate]); + }, [fenceId, fenceTemplate, previousFenceName, previousFenceParameters]); return <>{fenceParameterElements}; }; diff --git a/striker-ui/types/APIFence.d.ts b/striker-ui/types/APIFence.d.ts index 2ce11650..f908675e 100644 --- a/striker-ui/types/APIFence.d.ts +++ b/striker-ui/types/APIFence.d.ts @@ -5,6 +5,19 @@ type FenceParameterType = | 'select' | 'string'; +type FenceParameters = { + [parameterId: string]: string; +}; + +type APIFenceOverview = { + [fenceUUID: string]: { + fenceAgent: string; + fenceParameters: FenceParameters; + fenceName: string; + fenceUUID: string; + }; +}; + type APIFenceTemplate = { [fenceId: string]: { actions: string[]; diff --git a/striker-ui/types/CommonFenceInputGroup.d.ts b/striker-ui/types/CommonFenceInputGroup.d.ts index ea18d068..b4709f90 100644 --- a/striker-ui/types/CommonFenceInputGroup.d.ts +++ b/striker-ui/types/CommonFenceInputGroup.d.ts @@ -14,6 +14,8 @@ type MapToInputBuilder = Partial< type CommonFenceInputGroupOptionalProps = { fenceId?: string; fenceTemplate?: APIFenceTemplate; + previousFenceName?: string; + previousFenceParameters?: FenceParameters; }; type CommonFenceInputGroupProps = CommonFenceInputGroupOptionalProps;