utility-function-extractor/lib/datasetChanger.js

110 lines
2.8 KiB
JavaScript
Raw Normal View History

2022-01-30 15:29:45 +00:00
import React, { useState } from "react";
2022-01-30 15:29:45 +00:00
export function DataSetChanger({ handleSubmit }) {
let [value, setValue] = useState(`[
{
"name": "Some element. The name field is necessary",
"url": "http://www.example.com",
"somethirdfield": "a"
},
{
"name": "Another element",
"url": "http://www.example1.com",
"somethirdfield": "b"
},
{
"name": "A third element",
"url": "http://www.example2.com",
"isReferenceValue": true,
"somethirdfield": "c"
}
2022-01-30 15:29:45 +00:00
]`);
const [displayingDoneMessage, setDisplayingDoneMessage] = useState(false);
const [displayingDoneMessageTimer, setDisplayingDoneMessageTimer] =
useState(null);
let handleChange = (event) => {
2022-01-30 15:29:45 +00:00
setValue(event.target.value);
};
let handleSubmitInner = (event) => {
2022-01-30 15:29:45 +00:00
clearTimeout(displayingDoneMessageTimer);
event.preventDefault();
//console.log(event)
2022-01-30 15:29:45 +00:00
console.log("value@handleSubmitInner@DataSetChanger");
//console.log(typeof(value));
console.log(value);
2022-01-30 15:29:45 +00:00
try {
let newData = JSON.parse(value);
//console.log(typeof(newData))
//console.log(newData)
2022-01-30 15:29:45 +00:00
handleSubmit(newData);
if (!newData.length || newData.length < 2) {
throw Error("Not enough objects");
}
2022-01-30 15:29:45 +00:00
setDisplayingDoneMessage(true);
let timer = setTimeout(() => setDisplayingDoneMessage(false), 3000);
2022-01-30 15:29:45 +00:00
setDisplayingDoneMessageTimer(timer);
} catch (error) {
setDisplayingDoneMessage(false);
//alert(error)
//console.log(error)
let substituteText = `Error: ${error.message}
Try something like:
[
{
"name": "Some element. The name field is necessary",
"url": "http://www.example.com",
"somethirdfield": "a"
},
{
"name": "Another element",
"url": "http://www.example1.com",
"somethirdfield": "b"
},
{
"name": "Another element",
"url": "http://www.example2.com",
"isReferenceValue": true,
"somethirdfield": "c"
}
]
2022-01-30 15:29:45 +00:00
Your old input was: ${value}`;
setValue(substituteText);
}
2022-01-30 15:29:45 +00:00
};
return (
<form onSubmit={handleSubmitInner} className="inline">
2022-01-30 15:29:45 +00:00
<br />
<br />
<textarea
value={value}
onChange={handleChange}
rows="10"
cols="50"
className=""
/>
2022-01-30 15:29:45 +00:00
<br />
<button
className="bg-transparent hover:bg-blue-500 text-blue-700 font-semibold hover:text-white py-2 px-4 border border-blue-500 hover:border-transparent rounded mt-5 p-10"
2022-01-30 15:29:45 +00:00
onClick={handleSubmitInner}
>
Change dataset
</button>
&nbsp;
<button
2022-01-30 15:29:45 +00:00
className={
displayingDoneMessage
? "bg-transparent text-blue-700 font-semibold py-2 px-4 border border-blue-500 rounded mt-5 p-10"
: "hidden"
}
>
Done!
</button>
</form>
);
2022-01-30 15:29:45 +00:00
}