From 5d994ab37a77fc141aac9c9fb031ccf9a82ff8d1 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 3 Dec 2024 14:06:03 -0500 Subject: [PATCH] Changing all adapted files back to isValidGPSInput and deleting temporary test function --- .../groups/CreateGroupModalComponent.tsx | 4 +-- .../groups/EditGroupModalComponent.tsx | 4 +-- .../MapCalibrationInfoDisplayComponent.tsx | 4 +-- .../meters/CreateMeterModalComponent.tsx | 4 +-- .../meters/EditMeterModalComponent.tsx | 4 +-- src/client/app/utils/calibration.ts | 33 ++----------------- 6 files changed, 12 insertions(+), 41 deletions(-) diff --git a/src/client/app/components/groups/CreateGroupModalComponent.tsx b/src/client/app/components/groups/CreateGroupModalComponent.tsx index 1201fc2af..3ab3a5cbc 100644 --- a/src/client/app/components/groups/CreateGroupModalComponent.tsx +++ b/src/client/app/components/groups/CreateGroupModalComponent.tsx @@ -19,7 +19,7 @@ import '../../styles/modal.css'; import { tooltipBaseStyle } from '../../styles/modalStyle'; import { SelectOption, TrueFalseType } from '../../types/items'; import { UnitData } from '../../types/redux/units'; -import { GPSPoint, isValidGPSInputNew } from '../../utils/calibration'; +import { GPSPoint, isValidGPSInput } from '../../utils/calibration'; import { getGroupMenuOptionsForGroup, getMeterMenuOptionsForGroup, @@ -197,7 +197,7 @@ export default function CreateGroupModalComponent() { // If the user input a value then gpsInput should be a string. // null came from the DB and it is okay to just leave it - Not a string. if (typeof gpsInput === 'string') { - const {validGps,message} = isValidGPSInputNew(gpsInput); + const {validGps,message} = isValidGPSInput(gpsInput); if (validGps) { // Clearly gpsInput is a string but TS complains about the split so cast. const gpsValues = (gpsInput as string).split(',').map((value: string) => parseFloat(value)); diff --git a/src/client/app/components/groups/EditGroupModalComponent.tsx b/src/client/app/components/groups/EditGroupModalComponent.tsx index a7e17b52e..569494704 100644 --- a/src/client/app/components/groups/EditGroupModalComponent.tsx +++ b/src/client/app/components/groups/EditGroupModalComponent.tsx @@ -25,7 +25,7 @@ import { DataType } from '../../types/Datasources'; import { SelectOption, TrueFalseType } from '../../types/items'; import { GroupData } from '../../types/redux/groups'; import { UnitData } from '../../types/redux/units'; -import { GPSPoint, isValidGPSInputNew } from '../../utils/calibration'; +import { GPSPoint, isValidGPSInput } from '../../utils/calibration'; import { GroupCase, getCompatibilityChangeCase, @@ -288,7 +288,7 @@ export default function EditGroupModalComponent(props: EditGroupModalComponentPr // If the user input a value then gpsInput should be a string // null came from DB and it is okay to just leave it - Not a String. if (typeof gpsInput === 'string') { - const {validGps,message} = isValidGPSInputNew(gpsInput); + const {validGps,message} = isValidGPSInput(gpsInput); if (validGps) { // Clearly gpsInput is a string but TS complains about the split so cast. const gpsValues = (gpsInput as string).split(',').map((value: string) => parseFloat(value)); diff --git a/src/client/app/components/maps/MapCalibrationInfoDisplayComponent.tsx b/src/client/app/components/maps/MapCalibrationInfoDisplayComponent.tsx index d42f1bd4e..89bf1dcf4 100644 --- a/src/client/app/components/maps/MapCalibrationInfoDisplayComponent.tsx +++ b/src/client/app/components/maps/MapCalibrationInfoDisplayComponent.tsx @@ -3,7 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import * as React from 'react'; -import {GPSPoint, isValidGPSInputNew} from '../../utils/calibration'; +import {GPSPoint, isValidGPSInput} from '../../utils/calibration'; import {ChangeEvent, FormEvent} from 'react'; import {FormattedMessage} from 'react-intl'; @@ -88,7 +88,7 @@ export default class MapCalibrationInfoDisplayComponent extends React.Component< const longitudeIndex = 1; if (this.props.currentCartesianDisplay === 'x: undefined, y: undefined') { return; } const input = this.state.value; - const {validGps} = isValidGPSInputNew(input); + const {validGps} = isValidGPSInput(input); if (validGps) { const array = input.split(',').map((value: string) => parseFloat(value)); const gps: GPSPoint = { diff --git a/src/client/app/components/meters/CreateMeterModalComponent.tsx b/src/client/app/components/meters/CreateMeterModalComponent.tsx index 4e57a3dc2..13534f067 100644 --- a/src/client/app/components/meters/CreateMeterModalComponent.tsx +++ b/src/client/app/components/meters/CreateMeterModalComponent.tsx @@ -22,7 +22,7 @@ import '../../styles/modal.css'; import { tooltipBaseStyle } from '../../styles/modalStyle'; import { TrueFalseType } from '../../types/items'; import { MeterData, MeterTimeSortType, MeterType } from '../../types/redux/meters'; -import { GPSPoint, isValidGPSInputNew } from '../../utils/calibration'; +import { GPSPoint, isValidGPSInput } from '../../utils/calibration'; import { AreaUnitType } from '../../utils/getAreaUnitConversion'; import { showErrorNotification, showSuccessNotification } from '../../utils/notifications'; import { useTranslate } from '../../redux/componentHooks'; @@ -124,7 +124,7 @@ export default function CreateMeterModalComponent(props: CreateMeterModalProps): // If the user input a value then gpsInput should be a string. // null came from the DB and it is okay to just leave it - Not a string. if (typeof gpsInput === 'string') { - const {validGps, message} = isValidGPSInputNew(gpsInput); + const {validGps, message} = isValidGPSInput(gpsInput); if (validGps) { const gpsValues = gpsInput.split(',').map(value => parseFloat(value)); // It is valid and needs to be in this format for routing. diff --git a/src/client/app/components/meters/EditMeterModalComponent.tsx b/src/client/app/components/meters/EditMeterModalComponent.tsx index eec06fbf1..ca93e51a2 100644 --- a/src/client/app/components/meters/EditMeterModalComponent.tsx +++ b/src/client/app/components/meters/EditMeterModalComponent.tsx @@ -22,7 +22,7 @@ import { tooltipBaseStyle } from '../../styles/modalStyle'; import { TrueFalseType } from '../../types/items'; import { MeterData, MeterTimeSortType, MeterType } from '../../types/redux/meters'; import { UnitRepresentType } from '../../types/redux/units'; -import { GPSPoint, isValidGPSInputNew } from '../../utils/calibration'; +import { GPSPoint, isValidGPSInput } from '../../utils/calibration'; import { AreaUnitType } from '../../utils/getAreaUnitConversion'; import { getGPSString, nullToEmptyString } from '../../utils/input'; import { showErrorNotification } from '../../utils/notifications'; @@ -105,7 +105,7 @@ export default function EditMeterModalComponent(props: EditMeterModalComponentPr // If the user input a value then gpsInput should be a string. // null came from the DB and it is okay to just leave it - Not a string. if (typeof gpsInput === 'string') { - const {validGps, message} = isValidGPSInputNew(gpsInput); + const {validGps, message} = isValidGPSInput(gpsInput); if (validGps) { // Clearly gpsInput is a string but TS complains about the split so cast. const gpsValues = (gpsInput as string).split(',').map((value: string) => parseFloat(value)); diff --git a/src/client/app/utils/calibration.ts b/src/client/app/utils/calibration.ts index 08b951e85..84368103b 100644 --- a/src/client/app/utils/calibration.ts +++ b/src/client/app/utils/calibration.ts @@ -2,7 +2,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -import { showErrorNotification } from './notifications'; import { logToServer } from '../redux/actions/logs'; import { DataType } from '../types/Datasources'; import { MapMetadata } from '../types/redux/map'; @@ -78,7 +77,7 @@ export interface Dimensions { export function itemMapInfoOk(itemID: number, type: DataType, map: MapMetadata, gps?: GPSPoint): boolean { if (map === undefined) { return false; } if ((gps === null || gps === undefined) || map.origin === undefined || map.opposite === undefined) { return false; } - const {validGps} = isValidGPSInputNew(`${gps.latitude},${gps.longitude}`); + const {validGps} = isValidGPSInput(`${gps.latitude},${gps.longitude}`); if (!validGps) { logToServer('error', `Found invalid ${type === DataType.Meter ? 'meter' : 'group'} gps stored in database, id = ${itemID}`)(); return false; @@ -107,35 +106,7 @@ export function itemDisplayableOnMap(size: Dimensions, point: CartesianPoint): b * @param input The string to check for GPS values * @returns true if string is GPS and false otherwise. */ -export function isValidGPSInput(input: string): boolean { - if (input.indexOf(',') === -1) { // if there is no comma - // TODO It would be nice to tell user that comma is missing but need to check all uses to be sure don't get ''. - return false; - } else if (input.indexOf(',') !== input.lastIndexOf(',')) { // if there are multiple commas - return false; - } - // Works if value is not a number since parseFloat returns a NaN so treated as invalid later. - const array = input.split(',').map((value: string) => parseFloat(value)); - const latitudeIndex = 0; - const longitudeIndex = 1; - const latitudeConstraint = array[latitudeIndex] >= -90 && array[latitudeIndex] <= 90; - const longitudeConstraint = array[longitudeIndex] >= -180 && array[longitudeIndex] <= 180; - const result = latitudeConstraint && longitudeConstraint; - if (!result) { - // TODO It would be nice to return the error and then notify as desired. - showErrorNotification(translate('input.gps.range') + input); - } - return result; -} - -/** - * Checks if the string is a valid GPS representation. This requires it to be two numbers - * separated by a comma and the GPS values to be within allowed values. - * Note it causes a popup if the GPS values are not valid. - * @param input The string to check for GPS values - * @returns true if string is GPS and false otherwise. - */ -export function isValidGPSInputNew(input: string){ +export function isValidGPSInput(input: string){ let message = ''; let validGps = true; if (input.indexOf(',') === -1) { // if there is no comma