Skip to content

Commit

Permalink
Merge pull request #13042 from kialam/fix-topology-enabled-state-on-r…
Browse files Browse the repository at this point in the history
…edraw

Fix enable/disable node state on browser resize.
  • Loading branch information
AlexSCorey committed Oct 12, 2022
2 parents 50614b9 + 8a959e9 commit ab5ea46
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
25 changes: 13 additions & 12 deletions awx/ui/src/screens/TopologyView/MeshGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ const Loader = styled(ContentLoading)`
width: 100%;
background: white;
`;
function MeshGraph({ data, showLegend, zoom, setShowZoomControls }) {
const [storedNodes, setStoredNodes] = useState(null);
function MeshGraph({
data,
showLegend,
zoom,
setShowZoomControls,
storedNodes,
}) {
const [isNodeSelected, setIsNodeSelected] = useState(false);
const [selectedNode, setSelectedNode] = useState(null);
const [simulationProgress, setSimulationProgress] = useState(null);
Expand Down Expand Up @@ -100,19 +105,14 @@ function MeshGraph({ data, showLegend, zoom, setShowZoomControls }) {
// update mesh when user toggles enabled/disabled slider
useEffect(() => {
if (instance?.id) {
const updatedNodes = storedNodes.map((n) =>
const updatedNodes = storedNodes.current.map((n) =>
n.id === instance.id ? { ...n, enabled: instance.enabled } : n
);
setStoredNodes(updatedNodes);
storedNodes.current = updatedNodes;
updateNodeSVG(storedNodes.current);
}
}, [instance]); // eslint-disable-line react-hooks/exhaustive-deps

useEffect(() => {
if (storedNodes) {
updateNodeSVG(storedNodes);
}
}, [storedNodes]);

const draw = () => {
let width;
let height;
Expand All @@ -137,6 +137,9 @@ function MeshGraph({ data, showLegend, zoom, setShowZoomControls }) {
const mesh = svg.append('g').attr('class', 'mesh');

const graph = data;
if (storedNodes?.current) {
graph.nodes = storedNodes.current;
}

/* WEB WORKER */
const worker = webWorker();
Expand All @@ -162,7 +165,6 @@ function MeshGraph({ data, showLegend, zoom, setShowZoomControls }) {
}

function ended({ nodes, links }) {
setStoredNodes(nodes);
// Remove loading screen
d3.select('.simulation-loader').style('visibility', 'hidden');
setShowZoomControls(true);
Expand Down Expand Up @@ -247,7 +249,6 @@ function MeshGraph({ data, showLegend, zoom, setShowZoomControls }) {
.attr('fill', DEFAULT_NODE_COLOR)
.attr('stroke-dasharray', (d) => (d.enabled ? `1 0` : `5`))
.attr('stroke', DEFAULT_NODE_STROKE_COLOR);

// node type labels
node
.append('text')
Expand Down
5 changes: 4 additions & 1 deletion awx/ui/src/screens/TopologyView/TopologyView.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useCallback, useState } from 'react';
import React, { useEffect, useCallback, useState, useRef } from 'react';
import { t } from '@lingui/macro';
import { PageSection, Card, CardBody } from '@patternfly/react-core';
import ContentError from 'components/ContentError';
Expand All @@ -10,6 +10,7 @@ import useZoom from './utils/useZoom';
import { CHILDSELECTOR, PARENTSELECTOR } from './constants';

function TopologyView() {
const storedNodes = useRef(null);
const [showLegend, setShowLegend] = useState(true);
const [showZoomControls, setShowZoomControls] = useState(false);
const {
Expand All @@ -20,6 +21,7 @@ function TopologyView() {
} = useRequest(
useCallback(async () => {
const { data } = await MeshAPI.read();
storedNodes.current = data.nodes;
return {
meshData: data,
};
Expand Down Expand Up @@ -64,6 +66,7 @@ function TopologyView() {
showLegend={showLegend}
zoom={zoom}
setShowZoomControls={setShowZoomControls}
storedNodes={storedNodes}
/>
)}
</CardBody>
Expand Down

0 comments on commit ab5ea46

Please sign in to comment.