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

Fix/Fail to remove links / nodes in the live demo #216

Merged
merged 5 commits into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
module.exports = {
extends: ["eslint:recommended", "plugin:jest/recommended"],
globals: {
cy: true,
Cypress: true,
document: true,
module: true,
Promise: true,
Reflect: true,
window: true,
Cypress: true,
cy: true,
module: true,
__dirname: true,
},
parser: "babel-eslint",
Expand Down
42 changes: 40 additions & 2 deletions sandbox/Sandbox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default class Sandbox extends React.Component {
schema,
data,
fullscreen,
nodeIdToBeRemoved: null,
};
}

Expand Down Expand Up @@ -241,12 +242,45 @@ export default class Sandbox extends React.Component {
);
};

/**
* Before removing elements (nodes, links)
* from the graph data, this function is executed.
* https://github.com/oxyno-zeta/react-editable-json-tree#beforeremoveaction
*/
onBeforeRemoveGraphData = (key, keyPath, deep, oldValue) => {
if (keyPath && keyPath[0] && keyPath[0] === "nodes" && oldValue && oldValue.id) {
this.setState({
nodeIdToBeRemoved: oldValue.id,
});
}

return Promise.resolve();
};

/**
* Update graph data each time an update is triggered
* by JsonTree
* @param {Object} data update graph data (nodes and links)
*/
onGraphDataUpdate = data => this.setState({ data });
onGraphDataUpdate = data => {
const removedNodeIndex = data.nodes.findIndex(n => !n);
let removedNodeId = null;

if (removedNodeIndex !== -1 && this.state.nodeIdToBeRemoved) {
removedNodeId = this.state.nodeIdToBeRemoved;
}

const nodes = data.nodes.filter(Boolean);
const isValidLink = link => link && link.source !== removedNodeId && link.target !== removedNodeId;
const links = data.links.filter(isValidLink);

this.setState({
data: {
links,
nodes,
},
});
};

/**
* Build common piece of the interface that contains some interactions such as
Expand Down Expand Up @@ -391,7 +425,11 @@ export default class Sandbox extends React.Component {
Graph Data <small>(editable)</small>
</h4>
<div className="json-data-container">
<JsonTree data={this.state.data} onFullyUpdate={this.onGraphDataUpdate} />
<JsonTree
data={this.state.data}
beforeRemoveAction={this.onBeforeRemoveGraphData}
onFullyUpdate={this.onGraphDataUpdate}
/>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion sandbox/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
.container__form {
grid-column: 5/ 6;
grid-row: 1 / 4;
min-width: 400px;
min-width: 250px;
z-index: 3;
}

Expand Down