From dfa0a38a2996e7cb39a1f5e42ab84d098f060f11 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Fri, 28 Jan 2022 12:50:45 -0500 Subject: [PATCH] feat: Changes in preparation for being able to change comparisons --- lib/comparisonView.js | 51 +++++++++++++++++++--- lib/comparisonsChanger.js | 89 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 6 deletions(-) create mode 100644 lib/comparisonsChanger.js diff --git a/lib/comparisonView.js b/lib/comparisonView.js index 60eeda2..9ed7765 100644 --- a/lib/comparisonView.js +++ b/lib/comparisonView.js @@ -7,6 +7,7 @@ import { DisplayElement } from './displayElement' import { DisplayAsMarkdown } from './displayAsMarkdown' import { CreateTable, buildRows } from './findPaths' import { DataSetChanger } from "./datasetChanger" +import { ComparisonsChanger } from "./comparisonsChanger" import { pushToMongo } from "./pushToMongo.js" import { increasingList, maxMergeSortSteps, expectedNumMergeSortSteps } from "./utils.js" @@ -51,6 +52,7 @@ export default function ComparisonView({ listOfElementsForView }) { let initialOrderedList = [] let initialShowAdvancedOptions = false let initialShowComparisons = false + let initialShowLoadComparisons = false let initialShowChangeDataSet = false let initialNumSteps = 0; let initialMaxSteps = maxMergeSortSteps(listOfElementsForView.length) @@ -73,6 +75,7 @@ export default function ComparisonView({ listOfElementsForView }) { let [showAdvancedOptions, changeShowAdvanceOptions] = useState(initialShowAdvancedOptions); let [showComparisons, changeShowComparisons] = useState(initialShowComparisons); + let [showLoadComparisons, changeShowLoadComparisons] = useState(initialShowLoadComparisons); let [showChangeDataSet, changeshowChangeDataSet] = useState(initialShowChangeDataSet); let [numSteps, changeNumSteps] = useState(initialNumSteps); let [maxSteps, changeMaxSteps] = useState(initialMaxSteps) @@ -80,11 +83,11 @@ export default function ComparisonView({ listOfElementsForView }) { let [tableRows, setTableRows] = useState(initialTableRows) /* Convenience utils: restart + changeDataSet */ - let restart = (posList) => { + let restart = (posList) => {//({posList, initialBinaryComparisons2, initialQuantitativeComparisons2}) => { setToComparePair([posList[posList.length - 2], posList[posList.length - 1]]) setSliderValue(initialSliderValue) - setBinaryComparisons(initialBinaryComparisons) - setQuantitativeComparisons(initialQuantitativeComparisons) + setBinaryComparisons(initialBinaryComparisons2 || initialBinaryComparisons) + setQuantitativeComparisons(initialQuantitativeComparisons2 || initialQuantitativeComparisons) setIsListOrdered(initialIsListOdered) setOrderedList(initialOrderedList) changeNumSteps(initialNumSteps) @@ -107,6 +110,28 @@ export default function ComparisonView({ listOfElementsForView }) { changeMaxSteps(maxMergeSortSteps(newListLength)) restart(newPosList) + // restart({posList: newPosList}) + } + + let changeComparisons = (links, listOfElements) => { + let quantitativeComparisons2 = [] + let binaryComparisons2 = [] + for(let link of links){ + let {source, target, distance, reasoning} = link + let searchByName = (name, candidate) => candidate.name == name + let testForSource = (candidate) => searchByName(source, candidate) + let testForTarget = (candidate) => searchByName(target, candidate) + let element1 = listOfElements.findIndex(testForSource) + let element2 = listOfElements.findIndex(testForTarget) + if(element1 == -1 || element2 == -1){ + throw new Error("Comparisons include unknown elements, please retry") + } + quantitativeComparisons2.push([element1, element2, distance, reasoning]) + binaryComparisons2.push([element1,element2]) + } + // return ({quantitativeComparisons: quantitativeComparisons2, binaryComparisons: binaryComparisons2}) + //restart({posList, initialBinaryComparisons2=initialBinaryComparisons, initialQuantitativeComparisons2=initialQuantitativeComparisons}) + restart(posList) } // Manipulations @@ -377,23 +402,32 @@ export default function ComparisonView({ listOfElementsForView }) {
{/* Button: Restart */} -
+
{/* Button: Show comparisons */} -
+
+ {/* Button: Load comparisons */} +
+ +
{/* Button: Change dataset */} -
+
+ {/* Load comparisons section */} +
+ + {/**/} +
diff --git a/lib/comparisonsChanger.js b/lib/comparisonsChanger.js new file mode 100644 index 0000000..622f508 --- /dev/null +++ b/lib/comparisonsChanger.js @@ -0,0 +1,89 @@ +import React, { useState } from 'react'; + +export function ComparisonsChanger({handleSubmit}){ + let [value, setValue] = useState(`[ + Blah, blah`) + const [displayingDoneMessage, setDisplayingDoneMessage] = useState(false) + const [displayingDoneMessageTimer, setDisplayingDoneMessageTimer] = useState(null) + + let handleChange = (event) => { + setValue(event.target.value) + } + + let handleSubmitInner = (event) => { + clearTimeout(displayingDoneMessageTimer) + event.preventDefault(); + //console.log(event) + console.log("value@handleSubmitInner@ComparisonsChanger") + //console.log(typeof(value)); + console.log(value); + try{ + let newData = JSON.parse(value) + //console.log(typeof(newData)) + //console.log(newData) + handleSubmit(newData) + if(!newData.length || newData.length < 2){ + throw Error("Not enough objects") + } + setDisplayingDoneMessage(true) + let timer = setTimeout(() => setDisplayingDoneMessage(false), 3000); + setDisplayingDoneMessageTimer(timer) + }catch(error){ + setDisplayingDoneMessage(false) + //alert(error) + //console.log(error) + 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 ( +
+
+
+