import Section from "../common/Section";
import Card from "../common/Card";
import Form from "../common/Form";
import TextInputRow from "../common/TextInputRow";
import FileInputRow from "../common/FileInputRow";
import DictInputRow from "../common/DictInputRow";
import { csvFormat, csvParse, generateFile } from "./csv";
const PlantBlock = (props) => {
const onChange = (val, field1, field2, field3) => {
const newPlant = { ...props.value };
if (field3 !== undefined) {
newPlant[field1][field2][field3] = val;
} else if (field2 !== undefined) {
newPlant[field1][field2] = val;
} else {
newPlant[field1] = val;
}
props.onChange(newPlant);
};
const onCandidateLocationsTemplate = () => {
generateFile(
"Candidate locations - Template.csv",
csvFormat([
{
name: "Washakie County",
"latitude (deg)": "43.8356",
"longitude (deg)": "-107.6602",
"initial capacity (tonne)": "0",
"area cost factor": "0.88",
},
{
name: "Platte County",
"latitude (deg)": "42.1314",
"longitude (deg)": "-104.9676",
"initial capacity (tonne)": "0",
"area cost factor": "1.29",
},
{
name: "Park County",
"latitude (deg)": "44.4063",
"longitude (deg)": "-109.4153",
"initial capacity (tonne)": "0",
"area cost factor": "0.99",
},
{
name: "Goshen County",
"latitude (deg)": "42.0853",
"longitude (deg)": "-104.3534",
"initial capacity (tonne)": "0",
"area cost factor": "1",
},
])
);
};
const onCandidateLocationsFile = (contents) => {
const data = csvParse({
contents: contents,
requiredCols: [
"name",
"latitude (deg)",
"longitude (deg)",
"area cost factor",
"initial capacity (tonne)",
],
});
const result = {};
data.forEach((el) => {
let { name, ...props } = el;
result[name] = props;
});
onChange(result, "locations");
};
const onCandidateLocationsDownload = () => {
const result = [];
for (const [locationName, locationDict] of Object.entries(
props.value["locations"]
)) {
result.push({
name: locationName,
...locationDict,
});
}
generateFile(`Candidate locations - ${props.name}.csv`, csvFormat(result));
};
const onCandidateLocationsClear = () => {
onChange({}, "locations");
};
let description = "No locations set";
const nCenters = Object.keys(props.value["locations"]).length;
if (nCenters > 0) description = `${nCenters} locations`;
const shouldDisableMaxCap =
props.value["minimum capacity (tonne)"] ===
props.value["maximum capacity (tonne)"];
return (
<>
>
);
};
export default PlantBlock;