finished updating for US tons
This commit is contained in:
parent
9414e30cea
commit
62c2f6a63c
16 changed files with 180 additions and 211 deletions
|
|
@ -2,6 +2,7 @@ import {MenuItem, TextField } from "@mui/material"
|
|||
import { cloneDeep } from "lodash"
|
||||
import { pond } from "protobuf-ts/pond"
|
||||
import React, { useEffect, useRef, useState } from "react"
|
||||
import { getGrainUnit } from "utils"
|
||||
|
||||
interface Props {
|
||||
grainSettings?: pond.GrainSettings
|
||||
|
|
@ -18,9 +19,7 @@ export default function CustomGrainForm(props: Props) {
|
|||
const [constantB, setConstantB] = useState("0")
|
||||
const [constantC, setConstantC] = useState("0")
|
||||
const [kgPerBushel, setKgPerBushel] = useState("0")
|
||||
const [bushelsPerTonne, setBushelsPerTonne] = useState("0")//this is metric
|
||||
const [lbPerBushel, setLbPerBushel] = useState("0")
|
||||
const [bushelsPerTon, setBushelsPerTon] = useState("0")//this is US ton
|
||||
const [bushelsConversion, setBushelConversion] = useState("0")//this is either per tonne or per US ton depending on user preferences
|
||||
const valid = useRef(false)
|
||||
|
||||
useEffect(()=>{
|
||||
|
|
@ -32,9 +31,7 @@ export default function CustomGrainForm(props: Props) {
|
|||
setConstantB(grainSettings.b.toString())
|
||||
setConstantC(grainSettings.c.toString())
|
||||
setKgPerBushel(grainSettings.kgPerBushel.toString())
|
||||
setBushelsPerTonne(grainSettings.bushelsPerTonne.toString())
|
||||
setLbPerBushel(grainSettings.poundsPerBushel.toString())
|
||||
setBushelsPerTon(grainSettings.bushelsPerTon.toString())
|
||||
setBushelConversion(grainSettings.bushelsPerTonne.toString())
|
||||
setNewGrainSettings(grainSettings)
|
||||
}
|
||||
},[grainSettings])
|
||||
|
|
@ -46,18 +43,14 @@ export default function CustomGrainForm(props: Props) {
|
|||
constB: string,
|
||||
constC: string,
|
||||
kgPerBushel: string,
|
||||
bushelsPerTonne: string,
|
||||
lbPerBushel: string,
|
||||
bushelsPerTon: string) => {
|
||||
bushelsPerTonne: string) => {
|
||||
if (name === "") return false
|
||||
if (group === "") return false
|
||||
if (isNaN(parseFloat(constA))) return false
|
||||
if (isNaN(parseFloat(constB))) return false
|
||||
if (isNaN(parseFloat(constC))) return false
|
||||
if (isNaN(parseFloat(kgPerBushel))) return false
|
||||
if (isNaN(parseFloat(lbPerBushel))) return false
|
||||
if (isNaN(parseFloat(bushelsPerTonne))) return false
|
||||
if (isNaN(parseFloat(bushelsPerTon))) return false
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
@ -79,7 +72,7 @@ export default function CustomGrainForm(props: Props) {
|
|||
setName(name)
|
||||
let settings = cloneDeep(newGrainSettings)
|
||||
settings.name = name
|
||||
valid.current = validate(name, group, constantA, constantB, constantC, kgPerBushel, bushelsPerTonne, lbPerBushel, bushelsPerTon)
|
||||
valid.current = validate(name, group, constantA, constantB, constantC, kgPerBushel, bushelsConversion)
|
||||
settingsChanged(settings)
|
||||
}}/>
|
||||
<TextField
|
||||
|
|
@ -92,7 +85,7 @@ export default function CustomGrainForm(props: Props) {
|
|||
setGroup(group)
|
||||
let settings = cloneDeep(newGrainSettings)
|
||||
settings.group = group
|
||||
valid.current = validate(name, group, constantA, constantB, constantC, kgPerBushel, bushelsPerTonne, lbPerBushel, bushelsPerTon)
|
||||
valid.current = validate(name, group, constantA, constantB, constantC, kgPerBushel, bushelsConversion)
|
||||
settingsChanged(settings)
|
||||
}}/>
|
||||
<TextField
|
||||
|
|
@ -106,7 +99,7 @@ export default function CustomGrainForm(props: Props) {
|
|||
setEquation(enumVal)
|
||||
let settings = cloneDeep(newGrainSettings)
|
||||
settings.equation = enumVal
|
||||
valid.current = validate(name, group, constantA, constantB, constantC, kgPerBushel, bushelsPerTonne, lbPerBushel, bushelsPerTon)
|
||||
valid.current = validate(name, group, constantA, constantB, constantC, kgPerBushel, bushelsConversion)
|
||||
settingsChanged(settings)
|
||||
}}
|
||||
select>
|
||||
|
|
@ -136,7 +129,7 @@ export default function CustomGrainForm(props: Props) {
|
|||
onChange={(e) => {
|
||||
let val = e.target.value
|
||||
setConstantA(val)
|
||||
valid.current = validate(name, group, val, constantB, constantC, kgPerBushel, bushelsPerTonne, lbPerBushel, bushelsPerTon)
|
||||
valid.current = validate(name, group, val, constantB, constantC, kgPerBushel, bushelsConversion)
|
||||
let settings = cloneDeep(newGrainSettings)
|
||||
if(!isNaN(parseFloat(val))){
|
||||
settings.a = parseFloat(val)
|
||||
|
|
@ -154,7 +147,7 @@ export default function CustomGrainForm(props: Props) {
|
|||
onChange={(e) => {
|
||||
let val = e.target.value
|
||||
setConstantB(val)
|
||||
valid.current = validate(name, group, constantA, val, constantC, kgPerBushel, bushelsPerTonne, lbPerBushel, bushelsPerTon)
|
||||
valid.current = validate(name, group, constantA, val, constantC, kgPerBushel, bushelsConversion)
|
||||
let settings = cloneDeep(newGrainSettings)
|
||||
if(!isNaN(parseFloat(val))){
|
||||
settings.b = parseFloat(val)
|
||||
|
|
@ -172,7 +165,7 @@ export default function CustomGrainForm(props: Props) {
|
|||
onChange={(e) => {
|
||||
let val = e.target.value
|
||||
setConstantC(val)
|
||||
valid.current = validate(name, group, constantA, constantB, val, kgPerBushel, bushelsPerTonne, lbPerBushel, bushelsPerTon)
|
||||
valid.current = validate(name, group, constantA, constantB, val, kgPerBushel, bushelsConversion)
|
||||
let settings = cloneDeep(newGrainSettings)
|
||||
if(!isNaN(parseFloat(val))){
|
||||
settings.c = parseFloat(val)
|
||||
|
|
@ -190,14 +183,10 @@ export default function CustomGrainForm(props: Props) {
|
|||
onChange={(e) => {
|
||||
let val = e.target.value
|
||||
setKgPerBushel(val)
|
||||
valid.current = validate(name, group, constantA, constantB, constantC, val, bushelsPerTonne, lbPerBushel, bushelsPerTon)
|
||||
valid.current = validate(name, group, constantA, constantB, constantC, val, bushelsConversion)
|
||||
let settings = cloneDeep(newGrainSettings)
|
||||
if(!isNaN(parseFloat(val))){
|
||||
settings.kgPerBushel = parseFloat(val)
|
||||
//then calculate the kilograms to pounds and update it as well
|
||||
let newLb = Math.round((parseFloat(val)*2.205)*100)/100
|
||||
settings.poundsPerBushel = newLb
|
||||
setLbPerBushel(newLb.toString())
|
||||
}
|
||||
settingsChanged(settings)
|
||||
}}
|
||||
|
|
@ -206,66 +195,24 @@ export default function CustomGrainForm(props: Props) {
|
|||
sx={{marginBottom: 2}}
|
||||
fullWidth
|
||||
type="number"
|
||||
value={lbPerBushel}
|
||||
error={isNaN(parseFloat(lbPerBushel))}
|
||||
helperText={isNaN(parseFloat(lbPerBushel)) ? "Must be a valid number" : ""}
|
||||
value={bushelsConversion}
|
||||
error={isNaN(parseFloat(bushelsConversion))}
|
||||
helperText={isNaN(parseFloat(bushelsConversion)) ? "Must be a valid number" : ""}
|
||||
onChange={(e) => {
|
||||
let val = e.target.value
|
||||
setLbPerBushel(val)
|
||||
valid.current = validate(name, group, constantA, constantB, constantC, kgPerBushel, bushelsPerTonne, val, bushelsPerTon)
|
||||
setBushelConversion(val)
|
||||
valid.current = validate(name, group, constantA, constantB, constantC, kgPerBushel, val)
|
||||
let settings = cloneDeep(newGrainSettings)
|
||||
if(!isNaN(parseFloat(val))){
|
||||
settings.poundsPerBushel = parseFloat(val)
|
||||
//then calculate the pounds to kilograms and update it as well
|
||||
let newKg = Math.round((parseFloat(val)/2.205)*100)/100
|
||||
settings.kgPerBushel = newKg
|
||||
setKgPerBushel(newKg.toString())
|
||||
if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON){
|
||||
settings.bushelsPerTonne = parseFloat(val)/0.907
|
||||
}else{
|
||||
settings.bushelsPerTonne = parseFloat(val)
|
||||
}
|
||||
}
|
||||
settingsChanged(settings)
|
||||
}}
|
||||
label="lb per Bushel"/>
|
||||
<TextField
|
||||
sx={{marginBottom: 2}}
|
||||
fullWidth
|
||||
type="number"
|
||||
value={bushelsPerTonne}
|
||||
error={isNaN(parseFloat(bushelsPerTonne))}
|
||||
helperText={isNaN(parseFloat(bushelsPerTonne)) ? "Must be a valid number" : ""}
|
||||
onChange={(e) => {
|
||||
let val = e.target.value
|
||||
setBushelsPerTonne(val)
|
||||
valid.current = validate(name, group, constantA, constantB, constantC, kgPerBushel, val, lbPerBushel, bushelsPerTon)
|
||||
let settings = cloneDeep(newGrainSettings)
|
||||
if(!isNaN(parseFloat(val))){
|
||||
settings.bushelsPerTonne = parseFloat(val)
|
||||
let newTon = Math.round((parseFloat(val)*0.907)*100)/100
|
||||
settings.bushelsPerTon = newTon
|
||||
setBushelsPerTon(newTon.toString())
|
||||
}
|
||||
settingsChanged(settings)
|
||||
}}
|
||||
label="Bushels per Tonne"/>
|
||||
<TextField
|
||||
sx={{marginBottom: 2}}
|
||||
fullWidth
|
||||
type="number"
|
||||
value={bushelsPerTon}
|
||||
error={isNaN(parseFloat(bushelsPerTon))}
|
||||
helperText={isNaN(parseFloat(bushelsPerTon)) ? "Must be a valid number" : ""}
|
||||
onChange={(e) => {
|
||||
let val = e.target.value
|
||||
setBushelsPerTon(val)
|
||||
valid.current = validate(name, group, constantA, constantB, constantC, kgPerBushel, bushelsPerTonne, lbPerBushel, val)
|
||||
let settings = cloneDeep(newGrainSettings)
|
||||
if(!isNaN(parseFloat(val))){
|
||||
settings.bushelsPerTon = parseFloat(val)
|
||||
let newTonne = Math.round((parseFloat(val)/0.907)*100)/100
|
||||
settings.bushelsPerTonne = newTonne
|
||||
setBushelsPerTonne(newTonne.toString())
|
||||
}
|
||||
settingsChanged(settings)
|
||||
}}
|
||||
label="Bushels per Ton"/>
|
||||
label={"Bushels per " + (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE ? " Tonne" : "Ton")}/>
|
||||
</React.Fragment>
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -230,14 +230,8 @@ export default function GrainTransaction(props: Props) {
|
|||
//use the sources bushel per tonne if one was selected otherwise use the destination
|
||||
if (source) {
|
||||
bPerTonne = source.value.bushelsPerTonne();
|
||||
if(source.value.bushelsPerTon){
|
||||
bPerTon = source.value.bushelsPerTon()
|
||||
}
|
||||
} else if (dest) {
|
||||
bPerTonne = dest.value.bushelsPerTonne();
|
||||
if(dest.value.bushelsPerTon){
|
||||
bPerTon = dest.value.bushelsPerTon()
|
||||
}
|
||||
}
|
||||
|
||||
let newTransaction = pond.Transaction.create({
|
||||
|
|
@ -247,7 +241,6 @@ export default function GrainTransaction(props: Props) {
|
|||
bushels: finalVal,
|
||||
message: grainMessage,
|
||||
bushelsPerTonne: bPerTonne,
|
||||
bushelsPerTon: bPerTonne * 0.907
|
||||
})
|
||||
})
|
||||
});
|
||||
|
|
@ -542,18 +535,14 @@ export default function GrainTransaction(props: Props) {
|
|||
grainVal = (+grainVal * selectedDestination.value.bushelsPerTonne()).toFixed(2);
|
||||
}
|
||||
}
|
||||
}else if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE) {
|
||||
}else if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON) {
|
||||
if (!isNaN(parseFloat(e.target.value))) {
|
||||
if (selectedSource) {
|
||||
if(selectedSource.value.bushelsPerTonne){
|
||||
grainVal = (+grainVal * selectedSource.value.bushelsPerTon()).toFixed(2);
|
||||
}else{
|
||||
grainVal = (+grainVal * (selectedSource.value.bushelsPerTonne() * 0.907)).toFixed(2);
|
||||
}
|
||||
} else if (selectedDestination) {
|
||||
if(selectedDestination.value.bushelsPerTonne){
|
||||
grainVal = (+grainVal * selectedDestination.value.bushelsPerTon()).toFixed(2);
|
||||
}else{
|
||||
grainVal = (+grainVal * (selectedDestination.value.bushelsPerTonne() * 0.907)).toFixed(2);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue