Skip to content

Commit

Permalink
feat #1117: Add EnableAll menu item when routes include multiple disa…
Browse files Browse the repository at this point in the history
…bled steps
  • Loading branch information
ibek committed Oct 12, 2024
1 parent c9f7a97 commit 497fb6e
Showing 1 changed file with 51 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BanIcon, CheckIcon } from '@patternfly/react-icons';
import { ContextMenuItem } from '@patternfly/react-topology';
import { FunctionComponent, PropsWithChildren, useCallback, useContext } from 'react';
import { BanIcon, CheckIcon, PowerOffIcon } from '@patternfly/react-icons';
import { ContextMenuItem, useVisualizationController } from '@patternfly/react-topology';
import { FunctionComponent, PropsWithChildren, useCallback, useContext, useMemo } from 'react';
import { IDataTestID } from '../../../../models';
import { IVisualizationNode } from '../../../../models/visualization/base-visual-entity';
import { EntitiesContext } from '../../../../providers/entities.provider';
Expand All @@ -10,11 +10,32 @@ interface ItemDisableStepProps extends PropsWithChildren<IDataTestID> {
vizNode: IVisualizationNode;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function findAllDisabledNodes(node: any): IVisualizationNode[] | null {
const allDisabledNodes = [];
if (node?.data?.vizNode?.getComponentSchema()?.definition?.disabled) {
allDisabledNodes.push(node.data.vizNode);

Check warning on line 17 in packages/ui/src/components/Visualization/Custom/ContextMenu/ItemDisableStep.tsx

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/components/Visualization/Custom/ContextMenu/ItemDisableStep.tsx#L17

Added line #L17 was not covered by tests
}
if (node?.children) {
for (const child of node.children) {
const result = findAllDisabledNodes(child);

Check warning on line 21 in packages/ui/src/components/Visualization/Custom/ContextMenu/ItemDisableStep.tsx

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/components/Visualization/Custom/ContextMenu/ItemDisableStep.tsx#L20-L21

Added lines #L20 - L21 were not covered by tests
allDisabledNodes.push(...(result || []));
}
}
return allDisabledNodes;
}

export const ItemDisableStep: FunctionComponent<ItemDisableStepProps> = (props) => {
const entitiesContext = useContext(EntitiesContext);
const controller = useVisualizationController();

const isDisabled = !!props.vizNode.getComponentSchema()?.definition?.disabled;

const allDisabledNodes = useMemo(() => {
return findAllDisabledNodes(controller?.getGraph()) || [];
}, [controller]);
const isMultiDisabled = allDisabledNodes.length > 1;

const onToggleDisableNode = useCallback(() => {
const newModel = props.vizNode.getComponentSchema()?.definition || {};
setValue(newModel, 'disabled', !isDisabled);
Expand All @@ -23,17 +44,34 @@ export const ItemDisableStep: FunctionComponent<ItemDisableStepProps> = (props)
entitiesContext?.updateEntitiesFromCamelResource();
}, [entitiesContext, isDisabled, props.vizNode]);

const onEnableAllNodes = useCallback(() => {
allDisabledNodes.forEach((node) => {

Check warning on line 48 in packages/ui/src/components/Visualization/Custom/ContextMenu/ItemDisableStep.tsx

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/components/Visualization/Custom/ContextMenu/ItemDisableStep.tsx#L48

Added line #L48 was not covered by tests
const newModel = node.getComponentSchema()?.definition || {};
setValue(newModel, 'disabled', false);
node.updateModel(newModel);

Check warning on line 51 in packages/ui/src/components/Visualization/Custom/ContextMenu/ItemDisableStep.tsx

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/components/Visualization/Custom/ContextMenu/ItemDisableStep.tsx#L50-L51

Added lines #L50 - L51 were not covered by tests
});

entitiesContext?.updateEntitiesFromCamelResource();

Check warning on line 54 in packages/ui/src/components/Visualization/Custom/ContextMenu/ItemDisableStep.tsx

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/components/Visualization/Custom/ContextMenu/ItemDisableStep.tsx#L54

Added line #L54 was not covered by tests
}, [allDisabledNodes, entitiesContext]);

return (
<ContextMenuItem onClick={onToggleDisableNode} data-testid={props['data-testid']}>
{isDisabled ? (
<>
<CheckIcon /> Enable
</>
) : (
<>
<BanIcon /> Disable
</>
<>
<ContextMenuItem onClick={onToggleDisableNode} data-testid={props['data-testid']}>
{isDisabled ? (
<>
<CheckIcon /> Enable
</>
) : (
<>
<BanIcon /> Disable
</>
)}
</ContextMenuItem>
{isDisabled && isMultiDisabled && (
<ContextMenuItem onClick={onEnableAllNodes}>
<PowerOffIcon /> Enable All
</ContextMenuItem>
)}
</ContextMenuItem>
</>
);
};

0 comments on commit 497fb6e

Please sign in to comment.