2023-12-13 12:39:15 +08:00
|
|
|
import {useShopStore} from "./shop_store";
|
|
|
|
import {useClickAway} from "./options/useClickAway";
|
|
|
|
import {Modal} from "react-bootstrap";
|
|
|
|
import React from "react";
|
|
|
|
import {Validation} from "./validate";
|
2023-12-14 16:29:32 +08:00
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
import {useRenderCount} from "@uidotdev/usehooks";
|
2023-12-13 12:39:15 +08:00
|
|
|
|
2023-12-15 13:49:54 +08:00
|
|
|
const JSONExample = JSON.stringify([
|
|
|
|
{
|
|
|
|
"items": [
|
|
|
|
{
|
|
|
|
"pn": "1124",
|
|
|
|
"options": null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pn": "2118",
|
|
|
|
"options": null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pn": "2118",
|
|
|
|
"options": null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"pn": "2128",
|
|
|
|
"options": null
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"type": "rack"
|
|
|
|
}
|
|
|
|
]);
|
2023-12-13 12:39:15 +08:00
|
|
|
|
2023-12-07 17:08:22 +08:00
|
|
|
export function ImportJSON() {
|
2023-12-14 16:29:32 +08:00
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
const renderCount = useRenderCount();
|
|
|
|
|
|
|
|
const shouldShow = useShopStore((state) => state.importShouldOpen);
|
|
|
|
const data = useShopStore((state) => state.importValue);
|
|
|
|
const loadDescription = useShopStore((state) => state.loadDescription);
|
|
|
|
const updateImportDescription = useShopStore((state) => state.updateImportDescription);
|
|
|
|
const closeImport = useShopStore((state) => state.closeImport);
|
|
|
|
const showImport = useShopStore((state) => state.openImport);
|
|
|
|
|
2023-12-14 16:29:32 +08:00
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
console.log("ImportJSON renders: ", renderCount)
|
2023-12-13 12:39:15 +08:00
|
|
|
|
|
|
|
const ref = useClickAway((e) => {
|
|
|
|
if (e.type === "mousedown") // ignore touchstart
|
|
|
|
closeImport()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return (<>
|
|
|
|
<button
|
|
|
|
className="btn btn-sm btn-outline-primary m-0 mb-2"
|
|
|
|
style={{'cursor': 'pointer'}}
|
|
|
|
onClick={showImport}>Import JSON
|
|
|
|
</button>
|
2023-12-15 15:24:23 +08:00
|
|
|
<Modal show={shouldShow} animation={true} centered className="rfqFeedback">
|
|
|
|
<Modal.Body ref={ref}>
|
2023-12-13 12:39:15 +08:00
|
|
|
<div className="form-group">
|
|
|
|
<p className="small">
|
|
|
|
Input the JSON description below. Should be something like:
|
|
|
|
<br/>
|
|
|
|
{JSONExample}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="form-group w-100">
|
|
|
|
<textarea
|
|
|
|
onChange={(event) => {
|
|
|
|
updateImportDescription(event.target.value)
|
|
|
|
}}
|
|
|
|
value={data.value}
|
|
|
|
className="form-control w-100"
|
|
|
|
rows="5"
|
|
|
|
placeholder="Input JSON description here."/>
|
|
|
|
</div>
|
|
|
|
{data.error !== Validation.OK ? (
|
|
|
|
<div className="form-group">
|
|
|
|
<p className="text-danger">{data.error === Validation.Empty ? "Empty input" : "Invalid JSON"}</p>
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
<div className="d-flex flex-column flex-sm-row justify-content-end">
|
|
|
|
<a type="button" onClick={closeImport}
|
2023-12-14 14:17:18 +08:00
|
|
|
className="btn btn-sm btn-outline-primary m-0 mb-2 mt-2 mb-sm-0 me-sm-2">Close</a>
|
2023-12-13 12:39:15 +08:00
|
|
|
<a type="button" onClick={loadDescription}
|
2023-12-14 14:17:18 +08:00
|
|
|
className={`btn btn-sm btn-primary m-0 ms-sm-2 mt-2 ${data.error ? 'disabled' : ''}`}>Load
|
2023-12-13 12:39:15 +08:00
|
|
|
configuration</a>
|
|
|
|
</div>
|
|
|
|
</Modal.Body>
|
|
|
|
</Modal>
|
|
|
|
</>)
|
2023-12-07 17:08:22 +08:00
|
|
|
}
|