Skip to content

Commit

Permalink
Changing all adapted files back to isValidGPSInput and deleting tempo…
Browse files Browse the repository at this point in the history
…rary test function
  • Loading branch information
zhenga5 committed Dec 3, 2024
1 parent eb66706 commit 5d994ab
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions src/client/app/components/groups/EditGroupModalComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/client/app/components/meters/EditMeterModalComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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));
Expand Down
33 changes: 2 additions & 31 deletions src/client/app/utils/calibration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5d994ab

Please sign in to comment.