Skip to content

Commit

Permalink
Fixing isValidGpsInputNew Logic to fix error notification for invalid…
Browse files Browse the repository at this point in the history
… Meters, and adapting MapCalibrationInfoDisplayComponents
  • Loading branch information
zhenga5 committed Dec 3, 2024
1 parent 03a07fd commit a3e8c09
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
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, isValidGPSInput} from '../../utils/calibration';
import {GPSPoint, isValidGPSInputNew} from '../../utils/calibration';
import {ChangeEvent, FormEvent} from 'react';
import {FormattedMessage} from 'react-intl';

Expand Down Expand Up @@ -88,7 +88,8 @@ export default class MapCalibrationInfoDisplayComponent extends React.Component<
const longitudeIndex = 1;
if (this.props.currentCartesianDisplay === 'x: undefined, y: undefined') { return; }
const input = this.state.value;
if (isValidGPSInput(input)) {
const {validGps} = isValidGPSInputNew(input);
if (validGps) {
const array = input.split(',').map((value: string) => parseFloat(value));
const gps: GPSPoint = {
longitude: array[longitudeIndex],
Expand Down
30 changes: 16 additions & 14 deletions src/client/app/utils/calibration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,26 @@ export function isValidGPSInputNew(input: string){
let validGps = true;
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 ''.
message = 'Input is missing a comma'; //Translate later
message = 'GPS Input is missing a comma'; //Translate later
validGps = false;
} else if (input.indexOf(',') !== input.lastIndexOf(',')) { // if there are multiple commas
message = 'Input has too many commas'; //Translate later
message = 'GPS Input has too many commas'; //Translate later
validGps = 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);
validGps = false;
message = translate('input.gps.range') + input;
if(validGps){
// 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);
validGps = false;
message = translate('input.gps.range') + input;
}
}
return {validGps,message};
}
Expand Down

0 comments on commit a3e8c09

Please sign in to comment.