diff --git a/src/bin/BinComponentTypes.tsx b/src/bin/BinComponentTypes.tsx index 4495f29..e5f64e3 100644 --- a/src/bin/BinComponentTypes.tsx +++ b/src/bin/BinComponentTypes.tsx @@ -129,7 +129,7 @@ export default function BinComponentTypes(props: Props) { const [selectedComponentKey, setSelectedComponentKey] = useState(""); const [selectedComponentSubtype, setSelectedComponentSubtype] = useState(0); const [selectedComponentTopNode, setSelectedComponentTopNode] = useState(0); - const [{ user }] = useGlobalState(); + const [{ user, as }] = useGlobalState(); useEffect(() => { if (user.settings.temperatureUnit) { @@ -152,7 +152,7 @@ export default function BinComponentTypes(props: Props) { p.type = type; p.node = topNode ?? 0; binAPI - .updateComponentPreferences(bin, component, p) + .updateComponentPreferences(bin, component, p, as) .then(() => { snackbar.success("Component preferences updated"); preferences.set(component, p!); @@ -163,7 +163,7 @@ export default function BinComponentTypes(props: Props) { }); } }, - [bin, binAPI, preferences, setPreferences, snackbar] + [bin, binAPI, preferences, setPreferences, snackbar, as] ); useEffect(() => { diff --git a/src/bin/BinComponents.tsx b/src/bin/BinComponents.tsx index 6c2bb0f..aa501a1 100644 --- a/src/bin/BinComponents.tsx +++ b/src/bin/BinComponents.tsx @@ -30,7 +30,7 @@ import { Component, Device } from "models"; import { pond } from "protobuf-ts/pond"; import React, { useCallback, useEffect, useState } from "react"; import { useComponentAPI, useDeviceAPI, useSnackbar } from "hooks"; -import { useBinAPI } from "providers"; +import { useBinAPI, useGlobalState } from "providers"; import { GetComponentIcon } from "pbHelpers/ComponentType"; import { CheckBox as CheckBoxIcon, CheckBoxOutlineBlank, Remove } from "@mui/icons-material"; import ResponsiveDialog from "common/ResponsiveDialog"; @@ -85,6 +85,7 @@ interface Option { export default function BinComponents(props: Props) { const { components, bin, setComponents, updateBinStatus, binGrain } = props; + const [{as}] = useGlobalState(); const classes = useStyles(); const binAPI = useBinAPI(); const theme = useTheme(); @@ -208,7 +209,7 @@ export default function BinComponents(props: Props) { // }, [selectedDevice, componentAPI, deviceComponents, snackbar]); const removeComponent = (component: string) => { - binAPI.removeComponent(bin, component).then(() => { + binAPI.removeComponent(bin, component, as).then(() => { if (components && setComponents) { if (components.delete(component)) { let newComponents = new Map(components); @@ -312,7 +313,7 @@ export default function BinComponents(props: Props) { type: preset, node: component.settings.grainFilledTo ?? 0 }); - binAPI.addComponent(bin, device, component.key(), preferences).then(resp => { + binAPI.addComponent(bin, device, component.key(), preferences, as).then(resp => { if (components && setComponents) { if (components.set(component.key(), component)) { let newComponents = new Map(components); @@ -337,7 +338,7 @@ export default function BinComponents(props: Props) { } }); } else { - binAPI.removeComponent(bin, component.key()).then(resp => { + binAPI.removeComponent(bin, component.key(), as).then(resp => { if (components && setComponents) { if (components.delete(component.key())) { let newComponents = new Map(components); @@ -350,7 +351,7 @@ export default function BinComponents(props: Props) { }; const removeAll = () => { - binAPI.removeAllComponents(bin).then(resp => { + binAPI.removeAllComponents(bin, as).then(resp => { if (setComponents) { setComponents(new Map()); } diff --git a/src/bin/BinFansStatusTable.tsx b/src/bin/BinFansStatusTable.tsx index 2457abe..dd2781d 100644 --- a/src/bin/BinFansStatusTable.tsx +++ b/src/bin/BinFansStatusTable.tsx @@ -195,7 +195,7 @@ export default function BinsFansStatusTable(props: Props) { let bin = selectedBinRow.bin; bin.settings.fanId = newFanID; binAPI - .updateBin(bin.key(), bin.settings) + .updateBin(bin.key(), bin.settings, as) .then(resp => { //when an update succeeds update the table with the fan information //let dataMap = tableDataMap diff --git a/src/bin/BinHistory.tsx b/src/bin/BinHistory.tsx index 2634452..928fbc9 100644 --- a/src/bin/BinHistory.tsx +++ b/src/bin/BinHistory.tsx @@ -3,7 +3,7 @@ import { createStyles, makeStyles } from "@mui/styles"; //import { MatchParams } from "navigation/Routes"; import DiffHistory, { ListResult, Record } from "common/DiffHistory"; import { useSnackbar, useMobile } from "hooks"; -import { useBinAPI } from "providers"; +import { useBinAPI, useGlobalState } from "providers"; import { Bin } from "models"; import { TranslateKey, TranslateValue } from "pbHelpers/Bin"; import { pond } from "protobuf-ts/pond"; @@ -37,6 +37,7 @@ export default function BinHistory(props: Props) { //const params = useParams(); const {binID, drawer} = props const classes = useStyles(); + const [{as}] = useGlobalState(); const { openSnack } = useSnackbar(); const binAPI = useBinAPI(); //const binID = props.binID ?? params.binID; @@ -46,14 +47,14 @@ export default function BinHistory(props: Props) { useEffect(() => { binAPI - .getBin(binID) + .getBin(binID, as) .then((response: any) => { setBin(Bin.any(response.data)); }) .catch(err => { openSnack("There was a problem loading your bin"); }); - }, [binAPI, binID, openSnack]); + }, [binAPI, binID, openSnack, as]); let list = (limit: number, offset: number): Promise => { return new Promise(resolve => { diff --git a/src/bin/BinSettings.tsx b/src/bin/BinSettings.tsx index e540b20..46d3b59 100644 --- a/src/bin/BinSettings.tsx +++ b/src/bin/BinSettings.tsx @@ -408,7 +408,7 @@ export default function BinSettings(props: Props) { if (form.inventory) form.inventory.inventoryControl = inventoryControl; binAPI - .addBin(form) + .addBin(form, as) .then(resp => { openSnack("Successfully created bin"); if (!coords) { @@ -451,7 +451,7 @@ export default function BinSettings(props: Props) { form.autoGrainNode = autoTopNode; if (form.inventory) form.inventory.inventoryControl = inventoryControl; binAPI - .updateBin(bin.key(), form) + .updateBin(bin.key(), form, as) .then(response => { openSnack("Updated " + bin.name()); close(true); @@ -491,7 +491,7 @@ export default function BinSettings(props: Props) { const removeBin = () => { if (bin && bin.key()) { binAPI - .removeBin(bin.key()) + .removeBin(bin.key(), as) .then(response => { openSnack(bin.name() + " was removed"); navigate("/bins"); diff --git a/src/bin/BinStorageConditions.tsx b/src/bin/BinStorageConditions.tsx index 4f71818..0b42db7 100644 --- a/src/bin/BinStorageConditions.tsx +++ b/src/bin/BinStorageConditions.tsx @@ -22,7 +22,7 @@ import { GrainCable } from "models/GrainCable"; import { UnitMeasurement } from "models/UnitMeasurement"; import Co2Icon from "products/CommonIcons/co2Icon"; import { pond } from "protobuf-ts/pond"; -import { useBinAPI, useSnackbar } from "providers"; +import { useBinAPI, useGlobalState, useSnackbar } from "providers"; import React, { useEffect, useState } from "react"; import { avg, getTemperatureUnit } from "utils"; import { makeStyles } from "@mui/styles"; @@ -133,6 +133,7 @@ export default function BinStorageConditions(props: Props) { const binAPI = useBinAPI(); const { openSnack } = useSnackbar(); const { bin, headspaceCO2, cables } = props; + const [{as}] = 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); @@ -309,7 +310,7 @@ export default function BinStorageConditions(props: Props) { } binAPI - .updateBin(bin.key(), settings) + .updateBin(bin.key(), settings, as) .then(_resp => { openSnack("Updated bin thresholds"); }) diff --git a/src/bin/BinVisualizerV2.tsx b/src/bin/BinVisualizerV2.tsx index 8459e2a..5236ebf 100644 --- a/src/bin/BinVisualizerV2.tsx +++ b/src/bin/BinVisualizerV2.tsx @@ -228,6 +228,7 @@ export default function BinVisualizer(props: Props) { binPresets } = props; const binAPI = useBinAPI(); + const [{as}] = useGlobalState() const isMobile = useMobile(); const classes = useStyles(); const theme = useTheme(); @@ -1814,7 +1815,7 @@ export default function BinVisualizer(props: Props) { setModeTime(moment(newTimestamp)); status.lastModeChange = newTimestamp; binAPI - .updateBinStatus(bin.key(), status) + .updateBinStatus(bin.key(), status, as) .then(resp => { openSnack("Reset the storage time"); }) @@ -2066,7 +2067,7 @@ export default function BinVisualizer(props: Props) { b.settings.outdoorHumidity = Number(outdoorHumidityInput); b.settings.mode = newPreset; - binAPI.updateBin(bin.key(), b.settings).then(resp => { + binAPI.updateBin(bin.key(), b.settings, as).then(resp => { refresh(); }); }; diff --git a/src/bin/BinyardDisplay.tsx b/src/bin/BinyardDisplay.tsx index ed83912..2e5ba40 100644 --- a/src/bin/BinyardDisplay.tsx +++ b/src/bin/BinyardDisplay.tsx @@ -287,7 +287,7 @@ export default function BinyardDisplay(props: Props) { }, []); const duplicateBin = (bin: Bin) => { - binAPI.addBin(bin.settings).then(resp => { + binAPI.addBin(bin.settings, as).then(resp => { loadBins(); }); }; diff --git a/src/bin/CableTopNodeSummary.tsx b/src/bin/CableTopNodeSummary.tsx index e1a7a78..85a536f 100644 --- a/src/bin/CableTopNodeSummary.tsx +++ b/src/bin/CableTopNodeSummary.tsx @@ -2,7 +2,7 @@ import { Button, Card, Grid2 as Grid, Slider } from "@mui/material"; import { useMobile } from "hooks"; import { GrainCable } from "models/GrainCable"; import { pond } from "protobuf-ts/pond"; -import { useBinAPI } from "providers"; +import { useBinAPI, useGlobalState } from "providers"; import React, { useEffect, useState } from "react"; interface Props { @@ -21,6 +21,7 @@ interface CableNodeInfo { export default function CableTopNodeSummary(props: Props) { const { binKey, cables, componentDevMap } = props; + const [{as}] = useGlobalState() const [cableNodes, setCableNodes] = useState([]); const isMobile = useMobile(); const binAPI = useBinAPI(); @@ -53,7 +54,7 @@ export default function CableTopNodeSummary(props: Props) { ); }); binAPI - .updateTopNodes(binKey, newTopNodes) + .updateTopNodes(binKey, newTopNodes, as) .then(resp => { console.log("Set Top Nodes for cables, components and interactions have been updated"); }) diff --git a/src/bin/GrainNodeInteractions.tsx b/src/bin/GrainNodeInteractions.tsx index fcc321e..e01cdce 100644 --- a/src/bin/GrainNodeInteractions.tsx +++ b/src/bin/GrainNodeInteractions.tsx @@ -86,7 +86,7 @@ export default function GrainNodeInteractions(props: Props) { const [nodeTemp, setNodeTemp] = useState(); const [nodeHum, setNodeHum] = useState(); const [nodeMoist, setNodeMoist] = useState(); - const [{ user }] = useGlobalState(); + const [{ user, as }] = useGlobalState(); const tempDescriber = describeMeasurement(quack.MeasurementType.MEASUREMENT_TYPE_TEMPERATURE); const humDescriber = describeMeasurement(quack.MeasurementType.MEASUREMENT_TYPE_PERCENT); const moistureDescriber = describeMeasurement(quack.MeasurementType.MEASUREMENT_TYPE_GRAIN_EMC); @@ -195,7 +195,7 @@ export default function GrainNodeInteractions(props: Props) { }); //update the bins preferences to have the new top node for the cable binAPI - .updateComponentPreferences(binKey, cable.key(), pref) + .updateComponentPreferences(binKey, cable.key(), pref, as) .then(resp => { openSnack("Updated cables top node"); updateComponentCallback(cable.key()); diff --git a/src/bin/graphs/BinGraphs.tsx b/src/bin/graphs/BinGraphs.tsx index e5427f4..861994d 100644 --- a/src/bin/graphs/BinGraphs.tsx +++ b/src/bin/graphs/BinGraphs.tsx @@ -111,7 +111,7 @@ export default function BinGraphs(props: Props) { const [recentPlenumTrend, setRecentPlenumTrend] = useState(); const [recentCableTrend, setRecentCableTrend] = useState(); const [recentWaterContent, setRecentWaterContent] = useState(); - const [{ user }] = useGlobalState(); + const [{ user, as }] = useGlobalState(); const [measurementsLoading, setMeasurementsLoading] = useState(false); const isMobile = useMobile(); const [{ showErrors }, dispatch] = useGlobalState(); @@ -142,7 +142,8 @@ export default function BinGraphs(props: Props) { bin.key(), startDate.toISOString(), endDate.toISOString(), - showErrors + showErrors, + as ) .then(resp => { resp.data.measurements.forEach((um: any) => { diff --git a/src/bin/graphs/BinLevelOverTime.tsx b/src/bin/graphs/BinLevelOverTime.tsx index 647ba12..cefb99e 100644 --- a/src/bin/graphs/BinLevelOverTime.tsx +++ b/src/bin/graphs/BinLevelOverTime.tsx @@ -12,7 +12,7 @@ import BarGraph, { BarData, RefArea } from "charts/BarGraph"; import { Bin } from "models"; import moment, { Moment } from "moment"; import { pond, quack } from "protobuf-ts/pond"; -import { useBinAPI } from "providers"; +import { useBinAPI, useGlobalState } from "providers"; import { useEffect, useState } from "react"; import { Legend } from "recharts"; import { getGrainUnit } from "utils"; @@ -30,6 +30,7 @@ interface Props { export default function BinLevelOverTime(props: Props) { const { bin, fertilizerBin, colour, startDate, endDate, customHeight } = props; const binAPI = useBinAPI(); + const [{as}] = useGlobalState(); const [showCable, setShowCable] = useState(false); const [manualData, setManualData] = useState([]); const [cableData, setCableData] = useState([]); @@ -157,7 +158,7 @@ export default function BinLevelOverTime(props: Props) { if (cableDataLoading || bin.key() === "") return; setCableDataLoading(true); binAPI - .listBinMeasurements(bin.key(), startDate, endDate, 500, 0, "asc") + .listBinMeasurements(bin.key(), startDate, endDate, 500, 0, "asc", undefined, undefined, undefined, undefined, as) .then(resp => { resp.data.measurements.forEach(objMeasurement => { //set the bushel cable measurements @@ -186,7 +187,7 @@ export default function BinLevelOverTime(props: Props) { .finally(() => { setCableDataLoading(false); }); - }, [binAPI, bin, startDate, endDate, fertilizerBin]); // eslint-disable-line react-hooks/exhaustive-deps + }, [binAPI, bin, startDate, endDate, fertilizerBin, as]); // eslint-disable-line react-hooks/exhaustive-deps const legend = () => { return ( diff --git a/src/bin/graphs/BinWaterLevel.tsx b/src/bin/graphs/BinWaterLevel.tsx index 1fe68c1..182fc4c 100644 --- a/src/bin/graphs/BinWaterLevel.tsx +++ b/src/bin/graphs/BinWaterLevel.tsx @@ -40,7 +40,7 @@ export default function BinWaterLevel(props: Props) { bin.settings.inventory?.targetMoisture.toString() ?? "" ); const [reference, setReference] = useState(0); - // const [{ newStructure }] = useGlobalState(); + const [{ as }] = useGlobalState(); const [loading, setLoading] = useState(false); useEffect(() => { @@ -70,7 +70,8 @@ export default function BinWaterLevel(props: Props) { plenumKey, cableKey, pressureKey, - fanKey + fanKey, + as ) .then(resp => { let lastData: DataPoint | undefined; @@ -93,7 +94,7 @@ export default function BinWaterLevel(props: Props) { }); } } - }, [binAPI, range, bin, grainMoisture, plenumKey, cableKey, pressureKey]); // eslint-disable-line react-hooks/exhaustive-deps + }, [binAPI, range, bin, grainMoisture, plenumKey, cableKey, pressureKey, as]); // eslint-disable-line react-hooks/exhaustive-deps return ( diff --git a/src/contract/contractList.tsx b/src/contract/contractList.tsx index 345ccc6..aec7497 100644 --- a/src/contract/contractList.tsx +++ b/src/contract/contractList.tsx @@ -39,6 +39,7 @@ import { Label, Pie, PieChart, ResponsiveContainer, Text } from "recharts"; import { stringToMaterialColour } from "utils"; import { makeStyles } from "@mui/styles"; import { useNavigate } from "react-router-dom"; +import { useGlobalState } from "providers"; interface Props { contracts: Contract[]; @@ -59,6 +60,7 @@ const useStyles = makeStyles((theme: Theme) => ({ export default function ContractsList(props: Props) { const { contracts, contractPermissions, updateList } = props; const theme = useTheme(); + const [{as}] = useGlobalState(); //will need map for contracts that have supported grain types const [supportedTypes, setSupportedTypes] = useState>(new Map()); //will need need map for custom contracts @@ -115,7 +117,7 @@ export default function ContractsList(props: Props) { const deleteContract = () => { if (currentContract) { contractAPI - .removeContract(currentContract.key()) + .removeContract(currentContract.key(), as) .then(resp => { let c = contracts; contracts.splice(c.indexOf(currentContract), 1); diff --git a/src/contract/contractSettings.tsx b/src/contract/contractSettings.tsx index 5d7cab4..9d137c8 100644 --- a/src/contract/contractSettings.tsx +++ b/src/contract/contractSettings.tsx @@ -45,6 +45,7 @@ export default function ContractSettings(props: Props) { const { open, close, contract } = props; const contractAPI = useContractAPI(); const [name, setName] = useState(""); + const [{as}] = useGlobalState(); const [contractID, setContractID] = useState(""); //id const [holder, setHolder] = useState(""); //holder const [deliveryStart, setDeliveryStart] = useState(moment().format("YYYY-MM-DD")); //deliverystart @@ -136,7 +137,7 @@ export default function ContractSettings(props: Props) { }); taskAPI - .addTask(task) + .addTask(task, as) .then(resp => { console.log("Task Created"); }) @@ -171,7 +172,7 @@ export default function ContractSettings(props: Props) { settings.contractDate = contractDate; contractAPI - .updateContract(contract.key(), name, settings) + .updateContract(contract.key(), name, settings, as) .then(resp => { console.log("Updated Contract"); close(); @@ -198,7 +199,7 @@ export default function ContractSettings(props: Props) { settings.contractDate = contractDate; contractAPI - .addContract(name, settings) + .addContract(name, settings, as) .then(resp => { if (createDeliveryTask) { createTask(resp.data.key); @@ -214,7 +215,7 @@ export default function ContractSettings(props: Props) { const deleteContract = () => { if (contract) { contractAPI - .removeContract(contract.key()) + .removeContract(contract.key(), as) .then(resp => { console.log("contract deleted"); close(); diff --git a/src/contract/contractTransactionTable.tsx b/src/contract/contractTransactionTable.tsx index 2dd1dd5..11fc465 100644 --- a/src/contract/contractTransactionTable.tsx +++ b/src/contract/contractTransactionTable.tsx @@ -21,7 +21,7 @@ import moment from "moment"; import ObjectDescriber from "objects/ObjectDescriber"; import DeleteIcon from "products/AgIcons/Delete"; import { pond } from "protobuf-ts/pond"; -import { useBinAPI, useGrainBagAPI, useSnackbar, useTransactionAPI, useUserAPI } from "providers"; +import { useBinAPI, useGlobalState, useGrainBagAPI, useSnackbar, useTransactionAPI, useUserAPI } from "providers"; import React, { useEffect, useState } from "react"; import TransactionDataDisplay from "transactions/transactionDataDisplay"; import ContractTransactionFile from "./contractTransactionFile"; @@ -51,6 +51,7 @@ export default function ContractTransactionTable(props: Props) { const [displayData, setDisplayData] = useState([]); const [tablePage, setTablePage] = useState(0) const [pageSize, setPageSize] = useState(10) + const [{as}] = useGlobalState(); const userAPI = useUserAPI(); const [userProfiles, setUserProfiles] = useState>(new Map()); const [openRevokeDialog, setOpenRevokeDialog] = useState(false); @@ -97,7 +98,7 @@ export default function ContractTransactionTable(props: Props) { setTransactionSource(undefined); switch (transaction.transaction.fromObject) { case pond.ObjectType.OBJECT_TYPE_BIN: - binAPI.getBin(transaction.transaction.fromKey).then(resp => { + binAPI.getBin(transaction.transaction.fromKey, as).then(resp => { let bin = Bin.create(resp.data); setTransactionSource(bin); }); @@ -366,8 +367,8 @@ export default function ContractTransactionTable(props: Props) { rows={displayData} columns={columns} total={tableData.length} - handleRowsPerPageChange={()=>{}} - setPage={()=>{}} + handleRowsPerPageChange={(e)=>{setPageSize(e.target.value)}} + setPage={(page)=>{setTablePage(page)}} /> {/* (-1); - const [yearIndex, setYearIndex] = useState(0); - const [bins, setBins] = useState([]); - const [fields, setFields] = useState([]); - const [hYears, setHYears] = useState>(new Map()); - const [currentBushels, setCurrentBushels] = useState(0); - const { openSnack } = useSnackbar(); - const [increaseMode, setIncreaseMode] = useState<"correction" | "purchased" | "grown">( - "correction" - ); - const [decreaseMode, setDecreaseMode] = useState<"correction" | "sold" | "moved">("correction"); - - useEffect(() => { - if (activeBinSettings.inventory && dialogControl) { - setCurrentBushels(activeBinSettings.inventory.grainBushels); - } - }, [activeBinSettings, dialogControl]); - - const loadBins = useCallback(() => { - if (!dialogControl) return; - binAPI - .listBins(50, 0, "asc", "name", undefined, as) - .then(resp => { - setBins(resp.data.bins.map(b => Bin.any(b))); - }) - .catch(); - }, [binAPI, as, dialogControl]); - - const loadHYears = useCallback(() => { - if (fields.length > 0 && fieldIndex > -1) { - hYearAPI.listHarvestYears(50, 0, "asc", "year", fields[fieldIndex].fieldName()).then(resp => { - let tempMap = new Map(); - resp.data.harvestYear.forEach(hy => { - let tempHY = HarvestYear.any(hy); - tempMap.set(tempHY.year(), tempHY); - }); - setHYears(tempMap); - }); - } - }, [hYearAPI, fields, fieldIndex]); - - useEffect(() => { - loadHYears(); - }, [loadHYears]); - - useEffect(() => { - loadBins(); - fieldAPI - .listFields(500, 0, "asc", "fieldName", undefined, as) - .then(resp => { - setFields(resp.data.fields.map(f => Field.any(f))); - }) - .catch(); - }, [loadBins, fieldAPI, as]); - - const grainName = (bin: Bin) => { - return bin.storage() === pond.BinStorage.BIN_STORAGE_SUPPORTED_GRAIN - ? GrainDescriber(bin.grain()).name - : bin.customType(); - }; - - const dialogContent = () => { - if (newGrainInventory < currentBushels) { - return ( - - - - } - label="Correction" - onChange={() => setDecreaseMode("correction")} - /> - {/* Doesnt Actually Do Anything yet */} - {/* } - label="Sold" - onChange={() => setDecreaseMode("sold")} - /> */} - } - label="Moved" - onChange={() => setDecreaseMode("moved")} - /> - - - {decreaseMode === "moved" && ( - setBinIndex(+e.target.value)}> - {bins.map((option, i) => ( - - {option.name()} - {grainName(option)} - - ))} - - )} - - ); - } else { - return ( - - - - } - label="Correction" - onChange={() => setIncreaseMode("correction")} - /> - {/* Doesnt Actually Do Anything yet */} - {/* } - label="Purchased" - onChange={() => setIncreaseMode("purchased")} - /> */} - } - label="Grown" - onChange={() => setIncreaseMode("grown")} - /> - - - {increaseMode === "grown" && ( - - setFieldIndex(+e.target.value)}> - - No Field Selected - - {fields.map((option, i) => ( - - {option.fieldName()} - - ))} - - setYearIndex(+e.target.value)} - disabled={fieldIndex < 0}> - - New Harvest Year - - {Array.from(hYears.values()).length > 0 && - Array.from(hYears.values()).map(option => ( - - {option.year()} - - ))} - - - )} - - ); - } - }; - - const increaseInv = () => { - let bin = activeBinSettings; - let binInv = pond.BinInventory.fromObject({ - ...bin.inventory - }); - if (binInv.grainBushels === 0 && bin.mode === pond.BinMode.BIN_MODE_NONE) { - bin.mode = pond.BinMode.BIN_MODE_STORAGE; - } - binInv.grainBushels = newGrainInventory; - - if (fields.length > 0 && fieldIndex > -1) { - if (fields[fieldIndex].crop() !== activeBinSettings.inventory?.grainType) { - openSnack("Grain type of this bin does not match the Field"); - return; - } else if (yearIndex === 0 && hYears.has(moment().year())) { - openSnack("Current year already exists for this field"); - return; - } - binInv.grainSubtype = fields[fieldIndex].subtype(); - } - - bin.inventory = binInv; - - binAPI - .updateBin(activeBinSettings.key, bin) - .then(() => { - if (increaseMode === "grown") { - if (yearIndex === 0) { - let hy = HarvestYear.create(); - hy.settings.field = fields[fieldIndex].fieldName(); - hy.settings.grain = fields[fieldIndex].crop(); - hy.settings.year = moment().year(); - hy.settings.bushels = newGrainInventory - currentBushels; - hYearAPI.addHarvestYear(hy.settings).then(resp => { - loadHYears(); - }); - } else { - let hy = hYears.get(yearIndex); - if (hy) { - hy.settings.bushels = hy.totalBushels() + (newGrainInventory - currentBushels); - hYearAPI.updateHarvestYear(hy.key(), hy.settings).then(resp => { - loadHYears(); - }); - } - } - } else if (increaseMode === "purchased") { - } - }) - .catch(err => { - openSnack("Failed to update grain inventory"); - }) - .finally(() => { - if (afterUpdate) { - loadBins(); - afterUpdate(); - } - closeDialog(); - }); - }; - - const decreaseInv = () => { - let binFrom = activeBinSettings; - let binTo: Bin; - let binFromInv = pond.BinInventory.fromObject({ - ...binFrom.inventory - }); - - if (decreaseMode === "moved") { - //TODO-CS: re-think the logic for moving between bins since the addition of custom types, - //possibly remove restrictions and just have a confirmation for the user to change the destination bins grain type - binTo = bins[binIndex]; - if (binTo.settings.inventory && binTo.settings.specs) { - let binToInv = binTo.settings.inventory; - //check to make sure the grain types of both bins are the same - if (binFromInv.grainType !== binToInv.grainType) { - if ( - binToInv.grainType === pond.Grain.GRAIN_NONE || - binToInv.grainType === pond.Grain.GRAIN_INVALID || - binToInv.grainBushels < 1 - ) { - binTo.settings.inventory.grainType = binFromInv.grainType; - } else { - openSnack("Grain types of both bins must match"); - return; - } - } - //check if the other bin has enough space to move the grain - let space = binTo.settings.specs.bushelCapacity - binTo.settings.inventory.grainBushels; - if (space < currentBushels - newGrainInventory) { - openSnack("Bin does not have enough space"); - return; - } - binToInv.grainBushels = binToInv.grainBushels + (currentBushels - newGrainInventory); - } - } - - binFromInv.grainBushels = newGrainInventory; - binFrom.inventory = binFromInv; - - binAPI - .updateBin(activeBinSettings.key, binFrom) - .then(() => { - //update inventory of the bin that the grain was moved to if it was a decrease - if (decreaseMode === "moved") { - binAPI - .updateBin(bins[binIndex].key(), binTo.settings) - .then(() => { - openSnack("Grain inventory updated"); - }) - .catch(err => { - openSnack("Failed to update grain inventory"); - }) - .finally(() => { - if (afterUpdate) { - afterUpdate(binTo); - } - }); - } - }) - .catch(err => { - openSnack("Failed to update grain inventory"); - }) - .finally(() => { - if (decreaseMode !== "moved" && afterUpdate) { - afterUpdate(); - } - closeDialog(); - }); - }; - - const setNewInventory = () => { - if (currentBushels > newGrainInventory) { - decreaseInv(); - } else { - increaseInv(); - } - }; - - const closeDialog = (useCallback?: boolean) => { - onClose(useCallback); - }; - - const disableButton = () => { - if (increaseMode === "grown") { - if (fields.length === 0 || fieldIndex < 0) { - return true; - } - } - return false; - }; - - return ( - closeDialog(true)}> - Grain Adjustment for {activeBinSettings.name} - {dialogContent()} - - - - - - ); -} diff --git a/src/harvestPlan/DuplicateHarvestPlan.tsx b/src/harvestPlan/DuplicateHarvestPlan.tsx index 8800ffd..505673d 100644 --- a/src/harvestPlan/DuplicateHarvestPlan.tsx +++ b/src/harvestPlan/DuplicateHarvestPlan.tsx @@ -10,7 +10,7 @@ import ResponsiveDialog from "common/ResponsiveDialog"; import { Field, HarvestPlan, Task } from "models"; import moment from "moment"; import { pond } from "protobuf-ts/pond"; -import { useHarvestPlanAPI, useTaskAPI } from "providers"; +import { useGlobalState, useHarvestPlanAPI, useTaskAPI } from "providers"; import { useCallback, useEffect, useState } from "react"; interface Props { @@ -22,6 +22,7 @@ interface Props { export default function DuplicateHarvestPlan(props: Props) { const { open, close, fields, plan } = props; + const [{as}] = useGlobalState(); const [newFieldIndex, setNewFieldIndex] = useState(0); const [planTasks, setPlanTasks] = useState([]); const [title, setTitle] = useState(""); @@ -30,7 +31,7 @@ export default function DuplicateHarvestPlan(props: Props) { const [ready, setReady] = useState(false); const loadTasks = useCallback(() => { - taskAPI.listTasks(50, 0, "asc", "start", plan.key()).then(resp => { + taskAPI.listTasks(50, 0, "asc", "start", plan.key(), undefined).then(resp => { setPlanTasks(resp.data.tasks.map(t => Task.any(t))); setReady(true); }); @@ -51,7 +52,7 @@ export default function DuplicateHarvestPlan(props: Props) { let tasks = planTasks; tasks.forEach(task => { task.settings.objectKey = resp.data.harvestPlan; - taskAPI.addTask(task.settings); + taskAPI.addTask(task.settings, as); }); }); close(); diff --git a/src/harvestPlan/HarvestSettings.tsx b/src/harvestPlan/HarvestSettings.tsx index 2e3bce4..a736217 100644 --- a/src/harvestPlan/HarvestSettings.tsx +++ b/src/harvestPlan/HarvestSettings.tsx @@ -25,7 +25,7 @@ import { useMobile, useSnackbar } from "hooks"; import { Field, HarvestPlan, Task } from "models"; import moment from "moment"; import { pond } from "protobuf-ts/pond"; -import { useHarvestPlanAPI, useTaskAPI } from "providers"; +import { useGlobalState, useHarvestPlanAPI, useTaskAPI } from "providers"; import React, { useCallback, useEffect, useState } from "react"; import TaskSettings from "tasks/TaskSettings"; @@ -161,6 +161,7 @@ const steps = ["Pre-Seeding", "Seeding", "Post-Seeding", "Harvest", "Fall"]; export default function HarvestSettings(props: Props) { const { open, plan, close, field, update } = props; + const [{as}] = useGlobalState() const [planKey, setPlanKey] = useState(); const [newPlan, setNewPlan] = useState(HarvestPlan.create()); const [activeStep, setActiveStep] = useState(0); @@ -185,11 +186,11 @@ export default function HarvestSettings(props: Props) { const loadTasks = useCallback(() => { if (planKey) { - taskAPI.getMultiTasks([planKey]).then(resp => { + taskAPI.getMultiTasks([planKey], as).then(resp => { setPlanTasks(resp.data.tasks.map(t => Task.any(t))); }); } - }, [taskAPI, planKey]); + }, [taskAPI, planKey, as]); useEffect(() => { loadTasks(); diff --git a/src/maps/mapDrawers/BinDrawer.tsx b/src/maps/mapDrawers/BinDrawer.tsx index 41c15aa..5f60598 100644 --- a/src/maps/mapDrawers/BinDrawer.tsx +++ b/src/maps/mapDrawers/BinDrawer.tsx @@ -4,7 +4,7 @@ import MapMarkerSettings from "maps/MapMarkerSettings"; import { Bin as IBin } from "models"; //import Bin from "pages/Bin"; import { pond } from "protobuf-ts/pond"; -import { useBinAPI, useSnackbar } from "providers"; +import { useBinAPI, useGlobalState, useSnackbar } from "providers"; import React, { lazy, useEffect, useState } from "react"; const Bin = lazy(() => import("pages/Bin")); @@ -21,6 +21,7 @@ interface Props { export default function BinDrawer(props: Props) { const { open, onClose, selectedBin, bins, removeMarker, updateMarker, moveMap } = props; + const [{as}] = useGlobalState() const [bin, setBin] = useState(IBin.create()); const [openMarkerSettings, setOpenMarkerSettings] = useState(false); const binAPI = useBinAPI(); @@ -96,7 +97,7 @@ export default function BinDrawer(props: Props) { settings.location = null; binAPI - .updateBin(bin.key(), settings) + .updateBin(bin.key(), settings, as) .then(resp => { openSnack("Removed bin marker"); removeMarker(bin.key()); @@ -135,7 +136,7 @@ export default function BinDrawer(props: Props) { let settings = bin.settings; settings.theme = newTheme; binAPI - .updateBin(bin.key(), settings) + .updateBin(bin.key(), settings, as) .then(resp => { openSnack("marker settings updated"); updateMarker(bin.key(), settings); diff --git a/src/maps/mapObjectControllers/AgMapController.tsx b/src/maps/mapObjectControllers/AgMapController.tsx index e398e04..068b1b6 100644 --- a/src/maps/mapObjectControllers/AgMapController.tsx +++ b/src/maps/mapObjectControllers/AgMapController.tsx @@ -983,7 +983,7 @@ import { Result } from "@mapbox/mapbox-gl-geocoder"; const updateBinMarkers = (bin: IBin, long: number, lat: number, newMarker?: boolean) => { bin.setLocation(long, lat); binAPI - .updateBin(bin.key(), bin.settings) + .updateBin(bin.key(), bin.settings, as) .then(resp => { openSnack("Bin Location Updated"); //remove from options diff --git a/src/objects/bulkEditForms/bulkBinSettings.tsx b/src/objects/bulkEditForms/bulkBinSettings.tsx index 63e3cdc..f3e01a9 100644 --- a/src/objects/bulkEditForms/bulkBinSettings.tsx +++ b/src/objects/bulkEditForms/bulkBinSettings.tsx @@ -2,7 +2,7 @@ import { Button, Grid2 as Grid, InputAdornment, TextField } from "@mui/material" import SearchSelect, { Option } from "common/SearchSelect"; import { GrainOptions } from "grain"; import { pond } from "protobuf-ts/pond"; -import { useBinAPI, useSnackbar } from "providers"; +import { useBinAPI, useGlobalState, useSnackbar } from "providers"; import React, { useState } from "react"; import { fahrenheitToCelsius, getDistanceUnit, getTemperatureUnit } from "utils"; @@ -19,6 +19,7 @@ export default function BulkBinSettings(props: Props) { const gridItemWidth = 3; // bin settings variables const [name, setName] = useState(); + const [{as}] = useGlobalState(); const [grainType, setGrainType] = useState(); const [grainOption, setGrainOption] = useState