Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat #1117: Add EnableAll menu item when routes include multiple disabled steps #1568

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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>
</>
);
};
Loading