import React, { useState } from "react"; const checkLinksAreOk = (links, listOfElements) => { let linkSourceNames = links.map((link) => link.source.name); let linkTargetNames = links.map((link) => link.target.name); let allLinkNames = [...linkSourceNames, ...linkTargetNames]; let uniqueNames = [...new Set(allLinkNames)]; let listOfElementNames = listOfElements.map((element) => element.name); let anyInvalidNames = uniqueNames.indexOf( (name) => !listOfElementNames.includes(name) ); let anyElementsWithoutDistances = links.indexOf( (link) => !link.distance && link.distance != 0 ); if (anyInvalidNames == -1 && anyElementsWithoutDistances == -1) { return true; } else { return false; } }; export function ComparisonsChanger({ setLinks, listOfElements, show, moveToNextStep, }) { let [value, setValue] = useState(``); const [displayingDoneMessage, setDisplayingDoneMessage] = useState(false); const [displayingDoneMessageTimer, setDisplayingDoneMessageTimer] = useState(null); let handleTextChange = (event) => { setValue(event.target.value); }; let handleSubmitInner = (event) => { clearTimeout(displayingDoneMessageTimer); event.preventDefault(); try { let newData = JSON.parse(value); // setLinks // newData if (checkLinksAreOk(newData, listOfElements)) { setLinks(newData); moveToNextStep({ listOfElements, whileChangingStuff: true, newLinksFromChangingStuff: newData, }); setDisplayingDoneMessage(true); let timer = setTimeout(() => setDisplayingDoneMessage(false), 3000); setDisplayingDoneMessageTimer(timer); } else { throw Error("Links are not ok"); } } catch (error) { setDisplayingDoneMessage(false); let substituteText = `Error: ${error.message} Try something like: [ { "source": "x", "target": "y", "distance": 99999.99999999999, "reasoning": "blah blah" }, { "source": "y", "target": "z", "distance": 1, "reasoning": "blah blah" }, { "source": "x", "target": "z", "distance": 10, "reasoning": "blah blah" } ] Your old input was: ${value}`; setValue(substituteText); } }; return (
); }