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 { csvParse, extractNumericColumns, generateFile } from "./csv"; import { csvFormat } from "d3"; const ProductBlock = (props) => { const onChange = (field, val) => { const newProduct = { ...props.value }; newProduct[field] = val; props.onChange(newProduct); }; const onInitialAmountsFile = (contents) => { const data = csvParse({ contents: contents, requiredCols: ["latitude (deg)", "longitude (deg)", "name"], }); const result = {}; data.forEach((el) => { result[el["name"]] = { "latitude (deg)": el["latitude (deg)"], "longitude (deg)": el["longitude (deg)"], "amount (tonne)": extractNumericColumns(el, "amount"), }; }); onChange("initial amounts", result); }; const onInitialAmountsClear = () => { onChange("initial amounts", {}); }; const onInitialAmountsTemplate = () => { generateFile( "Initial amounts - Template.csv", csvFormat([ { name: "Washakie County", "latitude (deg)": "43.8356", "longitude (deg)": "-107.6602", "amount 1": "21902", "amount 2": "6160", "amount 3": "2721", "amount 4": "12917", "amount 5": "18048", }, { name: "Platte County", "latitude (deg)": "42.1314", "longitude (deg)": "-104.9676", "amount 1": "16723", "amount 2": "8709", "amount 3": "22584", "amount 4": "12278", "amount 5": "7196", }, { name: "Park County", "latitude (deg)": "44.4063", "longitude (deg)": "-109.4153", "amount 1": "14731", "amount 2": "11729", "amount 3": "15562", "amount 4": "7703", "amount 5": "23349", }, ]) ); }; const onInitialAmountsDownload = () => { const results = []; for (const [locationName, locationDict] of Object.entries( props.value["initial amounts"] )) { const row = { name: locationName, "latitude (deg)": locationDict["latitude (deg)"], "longitude (deg)": locationDict["longitude (deg)"], }; locationDict["amount (tonne)"].forEach((el, idx) => { row[`amount ${idx + 1}`] = el; }); results.push(row); } generateFile(`Initial amounts - ${props.name}.csv`, csvFormat(results)); }; let description = "Not initially available"; let notInitiallyAvailable = true; const nCenters = Object.keys(props.value["initial amounts"]).length; if (nCenters > 0) { description = `${nCenters} collection centers`; notInitiallyAvailable = false; } return ( <> General Information Disposal onChange("disposal cost ($/tonne)", v)} validate="floatList" /> onChange("disposal limit (tonne)", v)} validate="floatList" disabled={String(props.value["disposal limit (%)"]).length > 0} /> onChange("disposal limit (%)", v)} validate="floatList" disabled={props.value["disposal limit (tonne)"].length > 0} /> Transportation onChange("transportation cost ($/km/tonne)", v)} validate="floatList" /> onChange("transportation energy (J/km/tonne)", v)} validate="floatList" /> onChange("transportation emissions (tonne/km/tonne)", v) } validate="floatList" /> > ); }; export default ProductBlock;