diff --git a/src/bin/BinConditioningInteraction.tsx b/src/bin/BinConditioningInteraction.tsx index bd6c3c7..0a7e694 100644 --- a/src/bin/BinConditioningInteraction.tsx +++ b/src/bin/BinConditioningInteraction.tsx @@ -21,7 +21,7 @@ import { describeMeasurement } from "pbHelpers/MeasurementDescriber"; import { pond, quack } from "protobuf-ts/pond"; import { useGlobalState, useInteractionsAPI, useSnackbar } from "providers"; import React, { useEffect, useState } from "react"; -import { avg, fahrenheitToCelsius, getTemperatureUnit } from "utils"; +import { avg, fahrenheitToCelsius } from "utils"; import { makeStyles } from "@mui/styles"; import { Mark } from "@mui/material/Slider/useSlider.types"; @@ -131,7 +131,7 @@ export default function BinConditioningInteraction(props: Props) { temp && hum ) { - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { //the emc calc needs the temp to be in celsius temp = fahrenheitToCelsius(temp); } @@ -139,7 +139,7 @@ export default function BinConditioningInteraction(props: Props) { setBaseEMC(emc === hum ? undefined : emc); } - }, [sliderVals, grain]); + }, [sliderVals, grain, user]); const updateInteraction = () => { interactionAPI @@ -178,7 +178,7 @@ export default function BinConditioningInteraction(props: Props) { let marks: Mark[] = []; if (condition.measurementType === quack.MeasurementType.MEASUREMENT_TYPE_TEMPERATURE) { - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { labelTail = "°F"; } else { labelTail = "°C"; @@ -220,7 +220,7 @@ export default function BinConditioningInteraction(props: Props) { quack.MeasurementType.MEASUREMENT_TYPE_TEMPERATURE ) { if ( - getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT + user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT ) { return value.toFixed(1) + "°F"; } else { diff --git a/src/bin/BinSettings.tsx b/src/bin/BinSettings.tsx index 58669d6..bbfb4b5 100644 --- a/src/bin/BinSettings.tsx +++ b/src/bin/BinSettings.tsx @@ -61,7 +61,7 @@ import { pond } from "protobuf-ts/pond"; import { useBinAPI, useBinYardAPI, useGlobalState, useLibraCartProxyAPI } from "providers"; import React, { useCallback, useEffect, useState } from "react"; // import { useHistory } from "react-router"; -import { getDistanceUnit, or, getTemperatureUnit, getGrainUnit } from "utils"; +import { getDistanceUnit, or, getGrainUnit } from "utils"; import { makeStyles } from "@mui/styles"; import { useNavigate } from "react-router-dom"; import BinSelector from "./BinSelector"; @@ -290,7 +290,7 @@ export default function BinSettings(props: Props) { let high = initForm.highTemp ?? 20; let low = initForm.lowTemp ?? 10; let target = initForm.inventory?.targetTemperature ?? 15; - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { high = parseFloat((high * 1.8 + 32).toFixed(2)); low = parseFloat((low * 1.8 + 32).toFixed(2)); target = parseFloat((target * 1.8 + 32).toFixed(2)); @@ -364,7 +364,7 @@ export default function BinSettings(props: Props) { } else { setInitialized(false); } - }, [bin, open, mode, openedBinYard]); + }, [bin, open, mode, openedBinYard, user]); useEffect(() => { if (mode === "remove") { @@ -2308,7 +2308,7 @@ export default function BinSettings(props: Props) { onChange={event => { let value = event?.target.value; let valueC = value; - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { valueC = ((Number(value) - 32) * (5 / 9)).toFixed(2); } @@ -2320,7 +2320,7 @@ export default function BinSettings(props: Props) { InputProps={{ endAdornment: ( - {getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT + {user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT ? "F" : "C"} @@ -2348,7 +2348,7 @@ export default function BinSettings(props: Props) { onChange={event => { let value = event?.target.value; let valueC = value; - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { valueC = ((Number(value) - 32) * (5 / 9)).toFixed(2); } setLowTempC(+valueC); @@ -2357,7 +2357,7 @@ export default function BinSettings(props: Props) { InputProps={{ endAdornment: ( - {getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT + {user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT ? "F" : "C"} @@ -2383,7 +2383,7 @@ export default function BinSettings(props: Props) { onChange={event => { let value = event?.target.value; let valueC = value; - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { valueC = ((Number(value) - 32) * (5 / 9)).toFixed(2); } setHighTempC(+valueC); @@ -2392,7 +2392,7 @@ export default function BinSettings(props: Props) { InputProps={{ endAdornment: ( - {getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT + {user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT ? "F" : "C"} diff --git a/src/bin/BinStorageConditions.tsx b/src/bin/BinStorageConditions.tsx index fa0e957..b0e7e8a 100644 --- a/src/bin/BinStorageConditions.tsx +++ b/src/bin/BinStorageConditions.tsx @@ -24,7 +24,7 @@ import Co2Icon from "products/CommonIcons/co2Icon"; import { pond, quack } from "protobuf-ts/pond"; import { useBinAPI, useGlobalState, useSnackbar } from "providers"; import React, { useEffect, useState } from "react"; -import { avg, getTemperatureUnit } from "utils"; +import { avg } from "utils"; import { makeStyles } from "@mui/styles"; import { Mark } from "@mui/material/Slider/useSlider.types"; import { CO2 } from "models/CO2"; @@ -134,7 +134,7 @@ export default function BinStorageConditions(props: Props) { const binAPI = useBinAPI(); const { openSnack } = useSnackbar(); const { bin, headspaceCO2, cables } = props; - const [{as}] = useGlobalState() + const [{as, user}] = useGlobalState() const [sliderTemps, setSliderTemps] = useState([-40, 40]); //boolean that if a cable is missing its filled to display an icon or something to let the user know not all of the cables have their fill set const [missingTopNodeWarning, setMissingTopNodeWarning] = useState(false); @@ -233,7 +233,7 @@ export default function BinStorageConditions(props: Props) { } setTempTargets(tempCounts); let tempVal = bin.settings.inventory?.targetTemperature ?? 0; - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { tempVal = tempVal * 1.8 + 32; } setTargetTemp(tempVal.toFixed(1)); @@ -244,7 +244,7 @@ export default function BinStorageConditions(props: Props) { setEmcTargets(emcCounts); setTargetEMC(bin.settings.inventory?.targetMoisture.toFixed(1) ?? ""); setEmcDeviation(bin.settings.inventory?.moistureTargetDeviation.toFixed(1) ?? ""); - }, [bin, cables]); + }, [bin, cables, user]); //useEffect that watches for changes in the target emc and deviation to update the targets useEffect(() => { @@ -301,7 +301,7 @@ export default function BinStorageConditions(props: Props) { settings.highTemp = sliderTemps[1]; if (settings.inventory) { let tempVal = parseFloat(targetTemp); - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { tempVal = Math.fround(((tempVal - 32) * 5) / 9); } settings.inventory.targetTemperature = tempVal; @@ -376,14 +376,14 @@ export default function BinStorageConditions(props: Props) { let mark: Mark[] = []; if (averageTemp) { let temp = averageTemp; - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { temp = temp * 1.8 + 32; } mark = [ { label: customMark( temp.toFixed(1) + - (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT + (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT ? "°F" : "°C"), "AVG" @@ -422,7 +422,7 @@ export default function BinStorageConditions(props: Props) { InputProps={{ endAdornment: ( - {getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT + {user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT ? "°F" : "°C"} @@ -451,7 +451,7 @@ export default function BinStorageConditions(props: Props) { max={sliderEdge} valueLabelDisplay="on" valueLabelFormat={value => { - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { return ((value * 9) / 5 + 32).toFixed(1) + "°F"; } return value.toFixed(1) + "°C"; diff --git a/src/bin/BinVisualizerV2.tsx b/src/bin/BinVisualizerV2.tsx index 2da6f78..1426dd0 100644 --- a/src/bin/BinVisualizerV2.tsx +++ b/src/bin/BinVisualizerV2.tsx @@ -40,7 +40,7 @@ import { FullScreen, useFullScreenHandle } from "react-full-screen"; import moment, { Moment } from "moment"; import ResponsiveDialog from "common/ResponsiveDialog"; import { getThemeType } from "theme"; -import { getGrainUnit, getTemperatureUnit, or } from "utils"; +import { getGrainUnit, or } from "utils"; import { useBinAPI } from "providers/pond/binAPI"; import BindaptIcon from "products/Bindapt/BindaptIcon"; import { @@ -605,7 +605,7 @@ export default function BinVisualizer(props: Props) { break; } if (valC) { - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { temp = CtoF(valC).toFixed(1) + "°F"; } else { temp = valC.toFixed(1) + "°C"; @@ -705,7 +705,7 @@ export default function BinVisualizer(props: Props) { break; } if (valC) { - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { temp = Math.abs(valC * 1.8).toFixed(1) + "°F"; //since this is a measurement of change a change in 1 degrre celsius is equivalent to 1.8 fahrenheit } else { temp = Math.abs(valC).toFixed(1) + "°C"; @@ -1676,7 +1676,7 @@ export default function BinVisualizer(props: Props) { fontWeight: 650, color: tempColour }}> - {ambient?.getTempString(getTemperatureUnit())} + {ambient?.getTempString(user.tempUnit())} diff --git a/src/bin/conditioning/modeChangeDialog.tsx b/src/bin/conditioning/modeChangeDialog.tsx index b31c8f8..cc17448 100644 --- a/src/bin/conditioning/modeChangeDialog.tsx +++ b/src/bin/conditioning/modeChangeDialog.tsx @@ -19,7 +19,6 @@ import { sameComponentID } from "pbHelpers/Component"; import moment from "moment"; import { lowerCase } from "lodash"; import { describeMeasurement } from "pbHelpers/MeasurementDescriber"; -import { fahrenheitToCelsius, getTemperatureUnit } from "utils"; import { GetGrainExtensionMap } from "grain"; import { PromiseProgress, Stage, Step as PromiseStep } from "common/PromiseProgress"; @@ -76,7 +75,7 @@ export default function ModeChangeDialog(props: Props){ changeOutdoorHumidity, presets } = props - const [{as}] = useGlobalState() + const [{as, user}] = useGlobalState() const [componentSets, setComponentSets] = useState([]) const interactionAPI = useInteractionsAPI() const componentAPI = useComponentAPI(); @@ -296,7 +295,7 @@ if (!selectedDevice) return; conditions.push(humidityCondition); //since the measurement describers function to stored does a conversion into celsius if the users pref is fahrenheit for temperature we need to convert the preset value into fahrenheit - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { temp = Math.round((temp * (9 / 5) + 32) * 100) / 100; } let tempVal = describeMeasurement( @@ -392,7 +391,7 @@ if (!selectedDevice) return; conditions.push(fanConditionOne); //since the measurement describers function toStored does a conversion into celsius for temperature if the users pref is fahrenheit we need to convert the preset value into fahrenheit - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { tempPreset = Math.round((tempPreset * (9 / 5) + 32) * 100) / 100; } let tempVal = describeMeasurement( @@ -631,7 +630,7 @@ if (!selectedDevice) return; InputProps={{ endAdornment: ( - {getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT + {user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT ? "°F" : "℃"} diff --git a/src/common/ResponsiveTable.tsx b/src/common/ResponsiveTable.tsx index 4c21901..e3afd3f 100644 --- a/src/common/ResponsiveTable.tsx +++ b/src/common/ResponsiveTable.tsx @@ -614,7 +614,7 @@ export default function ResponsiveTable(props: Props) { {customElement && - + {customElement} diff --git a/src/device/DevicePresetsFromPicker.tsx b/src/device/DevicePresetsFromPicker.tsx index 900df87..5faaa1b 100644 --- a/src/device/DevicePresetsFromPicker.tsx +++ b/src/device/DevicePresetsFromPicker.tsx @@ -33,7 +33,6 @@ import { describeMeasurement } from "pbHelpers/MeasurementDescriber"; import { pond, quack } from "protobuf-ts/pond"; import React, { useEffect, useState } from "react"; import CableTopNodeSummary from "bin/CableTopNodeSummary"; -import { getTemperatureUnit } from "utils"; import { DevicePreset } from "models/DevicePreset"; import { Ambient } from "models/Ambient"; import { useGlobalState } from "providers"; @@ -182,7 +181,7 @@ export default function DevicePresets(props: DevicePresetsProps) { const [heaters, setHeaters] = useState([]); const [fans, setFans] = useState([]); //const [subscribeBinToAutoTopNode, setSubscribeBinToAutoTopNode] = useState(false); - const [{as}] = useGlobalState(); + const [{as, user}] = useGlobalState(); useEffect(() => { if (deviceComponents.get(deviceIndex.toString())) return; @@ -445,7 +444,7 @@ export default function DevicePresets(props: DevicePresetsProps) { conditions.push(fanConditionOne); //since the measurement describers function toStored does a conversion into celsius for temperature if the users pref is fahrenheit we need to convert the preset value into fahrenheit - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { tempPreset = Math.round((tempPreset * (9 / 5) + 32) * 100) / 100; } let tempVal = describeMeasurement( @@ -541,7 +540,7 @@ export default function DevicePresets(props: DevicePresetsProps) { } //since the measurement describers function to stored does a conversion into celsius if the users pref is fahrenheit for temperature we need to convert the preset value into fahrenheit - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { temp = Math.round((temp * (9 / 5) + 32) * 100) / 100; } let tempVal = describeMeasurement( diff --git a/src/device/presets/devicePresetCard.tsx b/src/device/presets/devicePresetCard.tsx index 2839df3..30cbc63 100644 --- a/src/device/presets/devicePresetCard.tsx +++ b/src/device/presets/devicePresetCard.tsx @@ -18,10 +18,9 @@ import { cloneDeep } from "lodash"; import { DevicePreset } from "models/DevicePreset"; import { ObjectTypeString } from "objects/ObjectDescriber"; import { pond } from "protobuf-ts/pond"; -import { useSnackbar } from "providers"; +import { useGlobalState, useSnackbar } from "providers"; import { useDevicePresetAPI } from "providers/pond/devicePresetAPI"; import React, { useEffect, useState } from "react"; -import { getTemperatureUnit } from "utils"; interface Props { preset: DevicePreset; @@ -54,6 +53,7 @@ export default function DevicePresetCard(props: Props) { const [humString, setHumString] = useState("0"); const [hum, setHum] = useState(0); const isMobile = useMobile(); + const [{user}] = useGlobalState(); useEffect(() => { setPresetName(preset.name); @@ -63,11 +63,11 @@ export default function DevicePresetCard(props: Props) { setHum(preset.settings.humidity); setHumString(preset.settings.humidity.toString()); let displayTemp = preset.settings.temperature.toString(); - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { displayTemp = ((preset.settings.temperature * 9) / 5 + 32).toFixed(); } setTempString(displayTemp); - }, [preset]); + }, [preset, user]); const saveNewPreset = () => { let typestring; @@ -296,7 +296,7 @@ export default function DevicePresetCard(props: Props) { InputProps={{ endAdornment: ( - {getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT + {user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT ? "°F" : "°C"} @@ -307,7 +307,7 @@ export default function DevicePresetCard(props: Props) { onChange={e => { if (!isNaN(+e.target.value)) { let temp = +e.target.value; - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { temp = Math.round((((temp - 32) * 5) / 9) * 10) / 10; } setTempC(temp); @@ -324,7 +324,7 @@ export default function DevicePresetCard(props: Props) { max={40} valueLabelDisplay="auto" valueLabelFormat={value => { - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { return ((value * 9) / 5 + 32).toFixed() + "°F"; } return value.toFixed() + "°C"; @@ -350,7 +350,7 @@ export default function DevicePresetCard(props: Props) { max={40} valueLabelDisplay="auto" valueLabelFormat={value => { - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { return ((value * 9) / 5 + 32).toFixed() + "°F"; } return value.toFixed() + "°C"; @@ -371,7 +371,7 @@ export default function DevicePresetCard(props: Props) { InputProps={{ endAdornment: ( - {getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT + {user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT ? "°F" : "°C"} @@ -382,7 +382,7 @@ export default function DevicePresetCard(props: Props) { onChange={e => { if (!isNaN(+e.target.value)) { let temp = +e.target.value; - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { temp = Math.round((((temp - 32) * 5) / 9) * 10) / 10; } setTempC(temp); diff --git a/src/gate/GateDeltaTempGraph.tsx b/src/gate/GateDeltaTempGraph.tsx index d547b1c..cad0564 100644 --- a/src/gate/GateDeltaTempGraph.tsx +++ b/src/gate/GateDeltaTempGraph.tsx @@ -11,7 +11,6 @@ import { pond } from "protobuf-ts/pond" import { quack } from "protobuf-ts/quack" import { useGlobalState } from "providers" import { useEffect, useState } from "react" -import { getTemperatureUnit } from "utils" interface Props { @@ -190,7 +189,7 @@ export default function GateDeltaTempGraph(props: Props){ {"Delta Temp: "} 0 ? warmingColour : coolingColour, fontWeight: 500 }}> - {recent.value.toFixed(2) + (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT ? " °F" : " °C")} + {recent.value.toFixed(2) + (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT ? " °F" : " °C")}
@@ -213,8 +212,8 @@ export default function GateDeltaTempGraph(props: Props){ diff --git a/src/gate/GateList.tsx b/src/gate/GateList.tsx index 5910808..ba521cc 100644 --- a/src/gate/GateList.tsx +++ b/src/gate/GateList.tsx @@ -17,7 +17,6 @@ import moment from "moment"; import { pond } from "protobuf-ts/pond"; import { UnitMeasurement } from "models/UnitMeasurement"; import { quack } from "protobuf-ts/quack"; -import { getTemperatureUnit } from "utils"; import { teal } from "@mui/material/colors"; import { react } from "@babel/types"; import { getDeviceStateHelper } from "pbHelpers/DeviceState"; @@ -229,7 +228,7 @@ export default function GateList(props: Props) { }) if(ambientTemp && outletTemp){ let deltaTemp = outletTemp - ambientTemp - display = deltaTemp.toFixed(2) + (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT ? "°F" : "°C") + display = deltaTemp.toFixed(2) + (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT ? "°F" : "°C") }else{ display = "Sensor Missing" } diff --git a/src/models/user.ts b/src/models/user.ts index 6c46a31..a56ec5e 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -101,4 +101,8 @@ export class User { } return this.settings.features.includes(flag); } + + public tempUnit(): pond.TemperatureUnit { + return this.settings.temperatureUnit + } } diff --git a/src/objects/ObjectDescriber.tsx b/src/objects/ObjectDescriber.tsx index 643482a..3faad15 100644 --- a/src/objects/ObjectDescriber.tsx +++ b/src/objects/ObjectDescriber.tsx @@ -2,12 +2,12 @@ import { Column } from "common/ResponsiveTable"; import { Option } from "common/SearchSelect"; import GrainDescriber from "grain/GrainDescriber"; //import { Column } from "material-table"; -import { Bin, BinYard, Field } from "models"; +import { Bin, BinYard, Field, User } from "models"; //import { Gate } from "models/Gate"; import { GrainBag } from "models/GrainBag"; //import { ObjectHeater } from "models/ObjectHeater"; import { pond } from "protobuf-ts/pond"; -import { getDistanceUnit, getTemperatureUnit } from "utils"; +import { getDistanceUnit } from "utils"; interface Sort { order: string; //what to send to the backend list to sort by @@ -20,6 +20,8 @@ export interface ObjectExtension { isTransactionObject: boolean; //this will define the columns to be used for the object in a material table tableColumns: Column[]; + // Optional factory so columns can depend on the active user settings (units, formatting, etc.) + getTableColumns?: (user?: User) => Column[]; //this map will match the title of the column to what the backend needs to perform the ordering tableSort: Map; } @@ -32,6 +34,113 @@ const defaultObject: ObjectExtension = { tableSort: new Map() }; +const binColumns = (user?: User): Column[] => { + const temperatureUnit = + user?.tempUnit() ?? pond.TemperatureUnit.TEMPERATURE_UNIT_CELSIUS; + + return [ + { + title: "Name", + cellStyle: {padding: "16px"}, + render: row => { + return (
{row.settings.name}
); + } + }, + { + title: "Grain", + cellStyle: {padding: "16px"}, + render: row => { + let grain = row.settings.inventory?.grainType; + if (grain) { + return GrainDescriber(grain).name; + } + } + }, + { + title: "Bin Height", + cellStyle: {padding: "16px"}, + render: row => { + let d = row.settings.specs?.heightCm; + if (d) { + if (getDistanceUnit() === pond.DistanceUnit.DISTANCE_UNIT_METERS) { + return (d / 100).toFixed(2) + " m"; + } else { + return (d / 30.48).toFixed(2) + " ft"; + } + } + } + }, + { + title: "Bin Diameter", + cellStyle: {padding: "16px"}, + render: row => { + let d = row.settings.specs?.diameterCm; + if (d) { + if (getDistanceUnit() === pond.DistanceUnit.DISTANCE_UNIT_METERS) { + return (d / 100).toFixed(2) + " m"; + } else { + return (d / 30.48).toFixed(2) + " ft"; + } + } + } + }, + { + title: "Custom Grain Name", + cellStyle: {padding: "16px"}, + render: row => { + return row.settings.inventory?.customTypeName; + } + }, + { + title: "Grain Variant", + cellStyle: {padding: "16px"}, + render: row => { + return row.settings.inventory?.grainSubtype; + } + }, + { + title: "Grain Bushels", + cellStyle: {padding: "16px"}, + render: row => { + return (
{row.settings.inventory ? isNaN(row.settings.inventory.grainBushels) ? 0 : row.settings.inventory.grainBushels : 0}
); + } + }, + { + title: "Grain Capacity", + cellStyle: {padding: "16px"}, + render: row => { + return row.settings.specs?.bushelCapacity; + } + }, + { + title: "High Temp Warning", + cellStyle: {padding: "16px"}, + render: row => { + let t = row.settings.highTemp; + if (t) { + if (temperatureUnit === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + return (t * 1.8 + 32).toFixed(2) + " °F"; + } + return t.toFixed(2) + " °C"; + } + } + }, + { + title: "Low Temp Warning", + cellStyle: {padding: "16px"}, + render: row => { + let t = row.settings.lowTemp; + if (t) { + if (temperatureUnit === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { + return (t * 1.8 + 32).toFixed(2) + " °F"; + } + return t.toFixed(2) + " °C"; + } + } + } + ] as Column[]; +}; + export const ObjectExtensions: Map = new Map([ [pond.ObjectType.OBJECT_TYPE_UNKNOWN, defaultObject], [ @@ -50,109 +159,8 @@ export const ObjectExtensions: Map = new Map([ name: "Bin", inventoryGroup: "grain", isTransactionObject: true, - tableColumns: [ - { - title: "Name", - cellStyle: {padding: "16px"}, - render: row => { - return (
{row.settings.name}
); - } - }, - { - title: "Grain", - cellStyle: {padding: "16px"}, - render: row => { - let grain = row.settings.inventory?.grainType; - if (grain) { - return GrainDescriber(grain).name; - } - } - }, - { - title: "Bin Height", - cellStyle: {padding: "16px"}, - render: row => { - let d = row.settings.specs?.heightCm; - if (d) { - if (getDistanceUnit() === pond.DistanceUnit.DISTANCE_UNIT_METERS) { - return (d / 100).toFixed(2) + " m"; - } else { - return (d / 30.48).toFixed(2) + " ft"; - } - } - } - }, - { - title: "Bin Diameter", - cellStyle: {padding: "16px"}, - render: row => { - let d = row.settings.specs?.diameterCm; - if (d) { - if (getDistanceUnit() === pond.DistanceUnit.DISTANCE_UNIT_METERS) { - return (d / 100).toFixed(2) + " m"; - } else { - return (d / 30.48).toFixed(2) + " ft"; - } - } - } - }, - { - title: "Custom Grain Name", - cellStyle: {padding: "16px"}, - render: row => { - return row.settings.inventory?.customTypeName; - } - }, - { - title: "Grain Variant", - cellStyle: {padding: "16px"}, - render: row => { - return row.settings.inventory?.grainSubtype; - } - }, - { - title: "Grain Bushels", - cellStyle: {padding: "16px"}, - render: row => { - return (
{row.settings.inventory ? isNaN(row.settings.inventory.grainBushels) ? 0 : row.settings.inventory.grainBushels : 0}
); - } - }, - { - title: "Grain Capacity", - cellStyle: {padding: "16px"}, - render: row => { - return row.settings.specs?.bushelCapacity; - } - }, - { - title: "High Temp Warning", - cellStyle: {padding: "16px"}, - render: row => { - let t = row.settings.highTemp; - if (t) { - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { - return (t * 1.8 + 32).toFixed(2) + " °F"; - } else { - return t.toFixed(2) + " °C"; - } - } - } - }, - { - title: "Low Temp Warning", - cellStyle: {padding: "16px"}, - render: row => { - let t = row.settings.lowTemp; - if (t) { - if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) { - return (t * 1.8 + 32).toFixed(2) + " °F"; - } else { - return t.toFixed(2) + " °C"; - } - } - } - } - ] as Column[], + tableColumns: binColumns(), + getTableColumns: (user?: User) => binColumns(user), tableSort: new Map([ ["Name", { order: "name", numerical: false }], ["Grain", { order: "inventory.grainType", numerical: false }], @@ -708,8 +716,7 @@ export const ObjectExtensions: Map = new Map([ ]); export default function ObjectDescriber(type: pond.ObjectType): ObjectExtension { - let describer = ObjectExtensions.get(type); - return describer ? describer : defaultObject; + return ObjectExtensions.get(type) ?? defaultObject; } /** @@ -745,7 +752,8 @@ export function SearchableObjects(): Option[] { Object.values(pond.ObjectType).forEach(obj => { if (typeof obj !== "string") { let ext = ObjectDescriber(obj); - if (ext.tableColumns.length > 0) { + const columns = ext.getTableColumns ? ext.getTableColumns() : ext.tableColumns; + if (columns.length > 0) { options.push({ label: ext.name, value: obj diff --git a/src/objects/ObjectTable.tsx b/src/objects/ObjectTable.tsx index 7a7b677..2ef6c86 100644 --- a/src/objects/ObjectTable.tsx +++ b/src/objects/ObjectTable.tsx @@ -1,6 +1,5 @@ import { Box, Button, Grid2 as Grid } from "@mui/material"; // import { getTableIcons } from "common/ResponsiveTable"; -import SearchBar from "common/SearchBar"; import SearchSelect, { Option } from "common/SearchSelect"; // import MaterialTable, { MTableToolbar } from "material-table"; // import { Bin, BinYard, Field } from "models"; @@ -22,9 +21,8 @@ import { import { useCallback, useEffect, useState } from "react"; import TeamSearch from "teams/TeamSearch"; import BulkBinSettings from "./bulkEditForms/bulkBinSettings"; -import BulkGrainBagSettings from "./bulkEditForms/bulkGrainBagSettings"; import ResponsiveTable from "common/ResponsiveTable"; -import { cloneDeep, indexOf } from "lodash"; +import { cloneDeep } from "lodash"; //import BulkGateSettings from "./bulkEditForms/bulkGateSettings"; interface customButton { @@ -61,6 +59,9 @@ export default function ObjectTable(props: Props) { const [selectedUser, setSelectedUser] = useState(as ?? user.id()); const [selectedPageRows, setSelectedPageRows] = useState>(new Map())//key is the page value is an array of row indexes for that page const [tableLoading, setTableLoading] = useState(false); + + const objectExtension = ObjectDescriber(currentType); + const columns = objectExtension.getTableColumns ? objectExtension.getTableColumns(user) : objectExtension.tableColumns; //the api's to load all the objects available to look at in this table const binAPI = useBinAPI(); const binyardAPI = useBinYardAPI(); @@ -366,7 +367,7 @@ export default function ObjectTable(props: Props) { { if (by !== -1) { - let colName = ObjectDescriber(currentType).tableColumns[by].title?.toString(); + let colName = ObjectDescriber(currentType, user).tableColumns[by].title?.toString(); let order; if (colName) { - order = ObjectDescriber(currentType).tableSort.get(colName); + order = ObjectDescriber(currentType, user).tableSort.get(colName); setCurrentOrder(order?.order); setCurrentDirection(direction); setIsNumerical(order?.numerical); diff --git a/src/objects/bulkEditForms/bulkBinSettings.tsx b/src/objects/bulkEditForms/bulkBinSettings.tsx index f3e01a9..ea8da7a 100644 --- a/src/objects/bulkEditForms/bulkBinSettings.tsx +++ b/src/objects/bulkEditForms/bulkBinSettings.tsx @@ -4,7 +4,7 @@ import { GrainOptions } from "grain"; import { pond } from "protobuf-ts/pond"; import { useBinAPI, useGlobalState, useSnackbar } from "providers"; import React, { useState } from "react"; -import { fahrenheitToCelsius, getDistanceUnit, getTemperatureUnit } from "utils"; +import { fahrenheitToCelsius, getDistanceUnit } from "utils"; interface Props { selectedBins: pond.Bin[]; @@ -19,7 +19,7 @@ export default function BulkBinSettings(props: Props) { const gridItemWidth = 3; // bin settings variables const [name, setName] = useState(); - const [{as}] = useGlobalState(); + const [{as, user}] = useGlobalState(); const [grainType, setGrainType] = useState(); const [grainOption, setGrainOption] = useState