fix(striker-ui): enable pass previous values to CommonFenceInputGroup

main
Tsu-ba-me 2 years ago committed by digimer
parent 10dc9970fb
commit bea7ad25c6
  1. 41
      striker-ui/components/CommonFenceInputGroup.tsx
  2. 13
      striker-ui/types/APIFence.d.ts
  3. 2
      striker-ui/types/CommonFenceInputGroup.d.ts

@ -40,9 +40,13 @@ const MAP_TO_INPUT_BUILDER: MapToInputBuilder = {
),
};
const combineIds = (...pieces: string[]) => pieces.join('-');
const CommonFenceInputGroup: FC<CommonFenceInputGroupProps> = ({
fenceId,
fenceTemplate,
previousFenceName,
previousFenceParameters,
}) => {
const fenceParameterElements = useMemo(() => {
let result: ReactNode;
@ -50,6 +54,20 @@ const CommonFenceInputGroup: FC<CommonFenceInputGroupProps> = ({
if (fenceTemplate && fenceId) {
const { parameters: fenceParameters } = fenceTemplate[fenceId];
let mapToPreviousFenceParameterValues: FenceParameters = {};
if (previousFenceParameters) {
mapToPreviousFenceParameterValues = Object.entries(
previousFenceParameters,
).reduce<FenceParameters>((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<CommonFenceInputGroupProps> = ({
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<CommonFenceInputGroupProps> = ({
optional: [],
required: [
MAP_TO_INPUT_BUILDER.string({
id: `${fenceId}-name`,
id: combineIds(fenceId, 'name'),
isRequired: true,
label: 'Fence device name',
value: previousFenceName,
}),
],
},
);
result = (
<>
<FlexBox
sx={{
'& > div:first-child': { marginTop: 0 },
'& > div': { marginBottom: 0 },
}}
>
<ExpandablePanel expandInitially header="Required parameters">
<FlexBox margin=".4em 0">{requiredInputs}</FlexBox>
</ExpandablePanel>
<ExpandablePanel header="Optional parameters">
<FlexBox margin=".4em 0">{optionalInputs}</FlexBox>
</ExpandablePanel>
</>
</FlexBox>
);
}
return result;
}, [fenceId, fenceTemplate]);
}, [fenceId, fenceTemplate, previousFenceName, previousFenceParameters]);
return <>{fenceParameterElements}</>;
};

@ -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[];

@ -14,6 +14,8 @@ type MapToInputBuilder = Partial<
type CommonFenceInputGroupOptionalProps = {
fenceId?: string;
fenceTemplate?: APIFenceTemplate;
previousFenceName?: string;
previousFenceParameters?: FenceParameters;
};
type CommonFenceInputGroupProps = CommonFenceInputGroupOptionalProps;

Loading…
Cancel
Save