From 82474318f3d1e9aa4edcbf2d568b3d94e9ddb875 Mon Sep 17 00:00:00 2001 From: Daniel Caldas <11733994+danielcaldas@users.noreply.github.com> Date: Fri, 11 Jan 2019 23:49:18 +0000 Subject: [PATCH] Fix/0 number id edge case (#161) * Change custom-node sample to have 0 valued id * Quick fix for 0 number id edge case --- sandbox/data/custom-node/custom-node.data.js | 46 ++++++++++---------- src/components/graph/graph.helper.js | 8 ++-- src/components/graph/graph.renderer.jsx | 4 +- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/sandbox/data/custom-node/custom-node.data.js b/sandbox/data/custom-node/custom-node.data.js index b494af54d..bfe864f25 100644 --- a/sandbox/data/custom-node/custom-node.data.js +++ b/sandbox/data/custom-node/custom-node.data.js @@ -1,50 +1,50 @@ module.exports = { links: [ { - source: 1, - target: 2 + source: 0, + target: 2, }, { - source: 1, - target: 3 + source: 0, + target: 3, }, { - source: 1, - target: 4 + source: 0, + target: 4, }, { source: 3, - target: 4 - } + target: 4, + }, ], nodes: [ { - id: 1, - name: 'Mary', - gender: 'female', + id: 0, + name: "Mary", + gender: "female", hasCar: false, - hasBike: false + hasBike: false, }, { id: 2, - name: 'Roy', - gender: 'male', + name: "Roy", + gender: "male", hasCar: false, - hasBike: true + hasBike: true, }, { id: 3, - name: 'Frank', - gender: 'male', + name: "Frank", + gender: "male", hasCar: true, - hasBike: true + hasBike: true, }, { id: 4, - name: 'Melanie', - gender: 'female', + name: "Melanie", + gender: "female", hasCar: true, - hasBike: false - } - ] + hasBike: false, + }, + ], }; diff --git a/src/components/graph/graph.helper.js b/src/components/graph/graph.helper.js index 52202d9f6..0517311ae 100644 --- a/src/components/graph/graph.helper.js +++ b/src/components/graph/graph.helper.js @@ -69,8 +69,8 @@ function _createForceSimulation(width, height, gravity) { */ function _initializeLinks(graphLinks, config) { return graphLinks.reduce((links, l) => { - const source = l.source.id || l.source; - const target = l.target.id || l.target; + const source = l.source.id !== undefined && l.source.id !== null ? l.source.id : l.source; + const target = l.target.id !== undefined && l.target.id !== null ? l.target.id : l.target; if (!links[source]) { links[source] = {}; @@ -245,8 +245,8 @@ function checkForGraphElementsChanges(nextProps, currentState) { const stateD3Nodes = currentState.d3Nodes.map(n => utils.antiPick(n, NODE_PROPERTIES_DISCARD_TO_COMPARE)); const stateD3Links = currentState.d3Links.map(l => ({ // FIXME: solve this source data inconsistency later - source: l.source.id || l.source, - target: l.target.id || l.target, + source: l.source.id !== undefined && l.source.id !== null ? l.source.id : l.source, + target: l.target.id !== undefined && l.target.id !== null ? l.target.id : l.target, })); const graphElementsUpdated = !( utils.isDeepEqual(nextNodes, stateD3Nodes) && utils.isDeepEqual(nextLinks, stateD3Links) diff --git a/src/components/graph/graph.renderer.jsx b/src/components/graph/graph.renderer.jsx index 90cc08171..e022b12ac 100644 --- a/src/components/graph/graph.renderer.jsx +++ b/src/components/graph/graph.renderer.jsx @@ -37,8 +37,8 @@ function _renderLinks(nodes, links, linksMatrix, config, linkCallbacks, highligh return outLinks.map(link => { const { source, target } = link; // FIXME: solve this source data inconsistency later - const sourceId = source.id || source; - const targetId = target.id || target; + const sourceId = source.id !== undefined && source.id !== null ? source.id : source; + const targetId = target.id !== undefined && target.id !== null ? target.id : target; const key = `${sourceId}${CONST.COORDS_SEPARATOR}${targetId}`; const props = buildLinkProps( { ...link, source: `${sourceId}`, target: `${targetId}` },