From 92afc55b4e6e6b65498f1c15b0aee40c0a32424f Mon Sep 17 00:00:00 2001 From: mmehta2669 Date: Mon, 25 Nov 2024 19:59:21 -0600 Subject: [PATCH 1/4] create unit changes --- .../meters/CreateMeterModalComponent.tsx | 29 ++++++++++++---- .../unit/CreateUnitModalComponent.tsx | 33 ++++++++++++++++++- src/client/app/types/redux/units.ts | 26 ++++++++------- src/server/migrations/1.0.0-2.0.0/index.js | 1 + .../units/alter_units_table_add_columns.sql | 8 +++++ src/server/models/Unit.js | 6 ++-- src/server/routes/units.js | 23 ++++++++++--- src/server/sql/unit/insert_new_unit.sql | 4 +-- src/server/sql/unit/update_unit.sql | 4 ++- 9 files changed, 105 insertions(+), 29 deletions(-) create mode 100644 src/server/migrations/1.0.0-2.0.0/sql/units/alter_units_table_add_columns.sql diff --git a/src/client/app/components/meters/CreateMeterModalComponent.tsx b/src/client/app/components/meters/CreateMeterModalComponent.tsx index 1c0ba7702..f66ba4891 100644 --- a/src/client/app/components/meters/CreateMeterModalComponent.tsx +++ b/src/client/app/components/meters/CreateMeterModalComponent.tsx @@ -29,6 +29,7 @@ import { useTranslate } from '../../redux/componentHooks'; import TimeZoneSelect from '../TimeZoneSelect'; import TooltipHelpComponent from '../TooltipHelpComponent'; import TooltipMarkerComponent from '../TooltipMarkerComponent'; +import { selectUnitDataById } from '../../redux/api/unitsApi'; interface CreateMeterModalProps { onCreateMeter?: (meterIdentifier: string) => void; // Define the type of the callback function @@ -55,6 +56,7 @@ export default function CreateMeterModalComponent(props: CreateMeterModalProps): const [meterDetails, setMeterDetails] = useState(defaultValues); const unitIsSelected = meterDetails.unitId !== -999; const defaultGaphicUnitIsSelected = meterDetails.defaultGraphicUnit !== -999; + const unitsDataById = useAppSelector(selectUnitDataById); const { compatibleGraphicUnits, incompatibleGraphicUnits, compatibleUnits } = useAppSelector(state => // Type assertion due to conflicting GPS Property @@ -92,6 +94,21 @@ export default function CreateMeterModalComponent(props: CreateMeterModalProps): const handleTimeZoneChange = (timeZone: string) => { setMeterDetails({ ...meterDetails, ['timeZone']: timeZone }); }; + + const handleUnitChange = (e: React.ChangeEvent) => { + const selectedUnitId = Number(e.target.value); + const selectedUnit = unitsDataById[selectedUnitId]; + + if (selectedUnit) { + setMeterDetails(details => ({ + ...details, + unitId: selectedUnitId, + minVal: selectedUnit.minVal, + maxVal: selectedUnit.maxVal + })); + } + }; + // Reset the state to default values const resetState = () => { setMeterDetails(defaultValues); @@ -245,9 +262,7 @@ export default function CreateMeterModalComponent(props: CreateMeterModalProps): { - handleNumberChange(e); - }} + onChange={handleUnitChange} invalid={!unitIsSelected}>