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: data links error #246

Merged
merged 1 commit into from
Oct 10, 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
10 changes: 8 additions & 2 deletions src/components/graph/graph.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import CONST from "./graph.const";
import DEFAULT_CONFIG from "./graph.config";
import ERRORS from "../../err";

import { isDeepEqual, isEmptyObject, merge, pick, antiPick, throwErr } from "../../utils";
import { isDeepEqual, isEmptyObject, merge, pick, antiPick, throwErr, throwWarning } from "../../utils";
import { computeNodeDegree } from "./collapse.helper";

const NODE_PROPS_WHITELIST = ["id", "highlighted", "x", "y", "index", "vy", "vx"];
Expand Down Expand Up @@ -205,9 +205,10 @@ function _tagOrphanNodes(nodes, linksMatrix) {
* Some integrity validations on links and nodes structure. If some validation fails the function will
* throw an error.
* @param {Object} data - Same as {@link #initializeGraphState|data in initializeGraphState}.
* @throws can throw the following error msg:
* @throws can throw the following error or warning msg:
* INSUFFICIENT_DATA - msg if no nodes are provided
* INVALID_LINKS - if links point to nonexistent nodes
* INSUFFICIENT_LINKS - if no links are provided
* @returns {undefined}
* @memberof Graph/helper
*/
Expand All @@ -216,6 +217,11 @@ function _validateGraphData(data) {
throwErr("Graph", ERRORS.INSUFFICIENT_DATA);
}

if (!data.links || !data.links.length) {
throwWarning("Graph", ERRORS.INSUFFICIENT_LINKS);
data.links = [];
}

const n = data.links.length;

for (let i = 0; i < n; i++) {
Expand Down
2 changes: 2 additions & 0 deletions src/err.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/*eslint max-len: ["error", 200]*/
export default {
GRAPH_NO_ID_PROP: "id prop not defined! id property is mandatory and it should be unique.",
INSUFFICIENT_LINKS:
"you are passing invalid data to react-d3-graph. You must include a links array in the data object you're passing down to the <Graph> component.",
INVALID_LINKS:
"you provided a invalid links data structure. Links source and target attributes must point to an existent node",
INSUFFICIENT_DATA:
Expand Down
15 changes: 14 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,17 @@ function throwErr(component, msg) {
throw Error(error);
}

export { isDeepEqual, isEmptyObject, deepClone, merge, pick, antiPick, throwErr };
/**
* Helper function for customized warning logging.
* @param {string} component - the name of the component where the warning is to be thrown.
* @param {string} msg - the message contain a more detailed explanation about the error.
* @returns {Warning} the thrown warning.
* @memberof utils
*/
function throwWarning(component, msg) {
const warning = `react-d3-graph :: ${component} :: ${msg}`;

console.warn(warning);
}

export { isDeepEqual, isEmptyObject, deepClone, merge, pick, antiPick, throwErr, throwWarning };