From c1e0c75423bd42e2e392de925fa10aa0b4666515 Mon Sep 17 00:00:00 2001 From: Ricardo van der Heijden <20791917+ricardovdheijden@users.noreply.github.com> Date: Mon, 9 Oct 2023 13:40:31 +0200 Subject: [PATCH] 336 - Removes duplicate code --- .../WFOWorkflowStepList.tsx | 48 ++++++++----------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/packages/orchestrator-ui-components/src/components/WFOWorkflowSteps/WFOWorkflowStepList/WFOWorkflowStepList.tsx b/packages/orchestrator-ui-components/src/components/WFOWorkflowSteps/WFOWorkflowStepList/WFOWorkflowStepList.tsx index 884f44d62..71f0f52ac 100644 --- a/packages/orchestrator-ui-components/src/components/WFOWorkflowSteps/WFOWorkflowStepList/WFOWorkflowStepList.tsx +++ b/packages/orchestrator-ui-components/src/components/WFOWorkflowSteps/WFOWorkflowStepList/WFOWorkflowStepList.tsx @@ -26,6 +26,17 @@ export const WFOWorkflowStepList = React.forwardRef( })); const [stepListItems, setStepListItems] = useState(initialStepListItems); + const updateStepListItem = ( + stepListItemToUpdate: StepListItem, + updateFunction: (stepListItem: StepListItem) => StepListItem, + ) => + setStepListItems( + stepListItems.map((stepListItem) => + stepListItem === stepListItemToUpdate + ? updateFunction(stepListItem) + : stepListItem, + ), + ); const allStepsAreExpanded = stepListItems.every( (item) => item.isExpanded, @@ -40,34 +51,17 @@ export const WFOWorkflowStepList = React.forwardRef( ); }; - const toggleExpandedStateStepListItem = ( - stepListItem: StepListItem, - ) => { - setStepListItems( - stepListItems.map((item) => - item.step === stepListItem.step - ? { - ...item, - isExpanded: !item.isExpanded, - } - : item, - ), - ); - }; + const toggleExpandedStateStepListItem = (stepListItem: StepListItem) => + updateStepListItem(stepListItem, (item) => ({ + ...item, + isExpanded: !item.isExpanded, + })); - const handleExpandStepListItem = (stepListItem: StepListItem) => { - setStepListItems( - stepListItems.map((item) => { - if (item.step === stepListItem.step) { - return { - ...item, - isExpanded: true, - }; - } - return item; - }), - ); - }; + const handleExpandStepListItem = (stepListItem: StepListItem) => + updateStepListItem(stepListItem, (item) => ({ + ...item, + isExpanded: true, + })); return ( <>