replacing the getTemperatureUnit function in units that uses the local storage with a function in the user model to just get it from the user settings

This commit is contained in:
csawatzky 2026-03-25 15:45:59 -06:00
parent a288c9503a
commit ce41a2dc05
16 changed files with 188 additions and 178 deletions

View file

@ -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<any>[];
// Optional factory so columns can depend on the active user settings (units, formatting, etc.)
getTableColumns?: (user?: User) => Column<any>[];
//this map will match the title of the column to what the backend needs to perform the ordering
tableSort: Map<string, Sort>;
}
@ -32,6 +34,113 @@ const defaultObject: ObjectExtension = {
tableSort: new Map()
};
const binColumns = (user?: User): Column<any>[] => {
const temperatureUnit =
user?.tempUnit() ?? pond.TemperatureUnit.TEMPERATURE_UNIT_CELSIUS;
return [
{
title: "Name",
cellStyle: {padding: "16px"},
render: row => {
return (<div>{row.settings.name}</div>);
}
},
{
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 (<div>{row.settings.inventory ? isNaN(row.settings.inventory.grainBushels) ? 0 : row.settings.inventory.grainBushels : 0}</div>);
}
},
{
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<Bin>[];
};
export const ObjectExtensions: Map<pond.ObjectType, ObjectExtension> = new Map([
[pond.ObjectType.OBJECT_TYPE_UNKNOWN, defaultObject],
[
@ -50,109 +159,8 @@ export const ObjectExtensions: Map<pond.ObjectType, ObjectExtension> = new Map([
name: "Bin",
inventoryGroup: "grain",
isTransactionObject: true,
tableColumns: [
{
title: "Name",
cellStyle: {padding: "16px"},
render: row => {
return (<div>{row.settings.name}</div>);
}
},
{
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 (<div>{row.settings.inventory ? isNaN(row.settings.inventory.grainBushels) ? 0 : row.settings.inventory.grainBushels : 0}</div>);
}
},
{
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<Bin>[],
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<pond.ObjectType, ObjectExtension> = 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

View file

@ -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<string>(as ?? user.id());
const [selectedPageRows, setSelectedPageRows] = useState<Map<number, number[]>>(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) {
<ResponsiveTable
rows={tableData}
columns={ObjectDescriber(currentType).tableColumns}
columns={columns}
onRowClick={rowClickFunction}
total={objectTotal}
pageSize={pageSize}
@ -383,7 +384,7 @@ export default function ObjectTable(props: Props) {
key="objectList"
title={ObjectDescriber(currentType).name}
icons={getTableIcons()}
columns={ObjectDescriber(currentType).tableColumns}
columns={ObjectDescriber(currentType, user).tableColumns}
data={tableData}
page={tablePage}
totalCount={objectTotal}
@ -399,10 +400,10 @@ export default function ObjectTable(props: Props) {
}}
onOrderChange={(by, direction) => {
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);

View file

@ -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<string | undefined>();
const [{as}] = useGlobalState();
const [{as, user}] = useGlobalState();
const [grainType, setGrainType] = useState<pond.Grain | undefined>();
const [grainOption, setGrainOption] = useState<Option | null>();
const [height, setHeight] = useState<number | undefined>();
@ -43,7 +43,7 @@ export default function BulkBinSettings(props: Props) {
const convertedTemp = (enteredTemp: number) => {
let t = enteredTemp;
if (getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) {
if (user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT) {
t = fahrenheitToCelsius(enteredTemp);
}
return t;
@ -204,7 +204,7 @@ export default function BulkBinSettings(props: Props) {
InputProps={{
endAdornment: (
<InputAdornment position="end">
{getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT
{user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT
? "°F"
: "°C"}
</InputAdornment>
@ -224,7 +224,7 @@ export default function BulkBinSettings(props: Props) {
InputProps={{
endAdornment: (
<InputAdornment position="end">
{getTemperatureUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT
{user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT
? "°F"
: "°C"}
</InputAdornment>