diff options
author | Ashutosh Bishnoi <mail2bishnoi@gmail.com> | 2021-02-14 13:40:40 +0530 |
---|---|---|
committer | Ashutosh Bishnoi <mail2bishnoi@gmail.com> | 2021-02-14 13:40:40 +0530 |
commit | 5970936c860285576848d170dd91bfcc488c941e (patch) | |
tree | 087d399c90f29743f0250cbf45616027f6bb191b /modern/src | |
parent | 3e5ef37008fd009821181b1d539622a44b8cd745 (diff) | |
download | trackermap-web-5970936c860285576848d170dd91bfcc488c941e.tar.gz trackermap-web-5970936c860285576848d170dd91bfcc488c941e.tar.bz2 trackermap-web-5970936c860285576848d170dd91bfcc488c941e.zip |
Code improvement and deduplication
Diffstat (limited to 'modern/src')
-rw-r--r-- | modern/src/attributes/positionAttributes.js | 4 | ||||
-rw-r--r-- | modern/src/common/converter.js | 24 | ||||
-rw-r--r-- | modern/src/reports/ChartReportPage.js | 4 | ||||
-rw-r--r-- | modern/src/settings/MaintenancePage.js | 47 |
4 files changed, 49 insertions, 30 deletions
diff --git a/modern/src/attributes/positionAttributes.js b/modern/src/attributes/positionAttributes.js index c5665da4..b1efe3df 100644 --- a/modern/src/attributes/positionAttributes.js +++ b/modern/src/attributes/positionAttributes.js @@ -13,8 +13,8 @@ export default { name: t('positionIgnition'), type: 'boolean', }, - 'serviceOdometer': { - name: t('positionServiceOdometer'), + 'odometer': { + name: t('positionOdometer'), type: 'number', dataType: 'distance', }, diff --git a/modern/src/common/converter.js b/modern/src/common/converter.js index 930dba15..26e2a82b 100644 --- a/modern/src/common/converter.js +++ b/modern/src/common/converter.js @@ -1,4 +1,4 @@ -export const speedConverter = (value, unit, back = false) => { +const speedConverter = (value, unit, back = false) => { let factor; switch (unit) { case 'kmh': @@ -11,10 +11,10 @@ export const speedConverter = (value, unit, back = false) => { default: factor = 1; } - return back ? (value / factor).toFixed(2):(value * factor).toFixed(2); + return back ? value / factor: value * factor; }; -export const distanceConverter = (value, unit, back = false) => { +const distanceConverter = (value, unit, back = false) => { let factor; switch (unit) { case 'mi': @@ -28,4 +28,20 @@ export const distanceConverter = (value, unit, back = false) => { factor = 0.001; } return back ? value / factor : value * factor; -}
\ No newline at end of file +} + +export const speedFromKnots = (value, unit) => { + return speedConverter(value, unit); +} + +export const speedToKnots = (value, unit) => { + return speedConverter(value, unit, true); +} + +export const distanceFromMeters = (value, unit) => { + return distanceConverter(value, unit); +} + +export const distanceToMeters = (value, unit) => { + return distanceConverter(value, unit, true); +} diff --git a/modern/src/reports/ChartReportPage.js b/modern/src/reports/ChartReportPage.js index 543c8a9e..80424cce 100644 --- a/modern/src/reports/ChartReportPage.js +++ b/modern/src/reports/ChartReportPage.js @@ -5,7 +5,7 @@ import ReportFilter from './ReportFilter'; import Graph from './Graph'; import { useAttributePreference } from '../common/preferences'; import { formatDate } from '../common/formatter'; -import { speedConverter } from '../common/converter'; +import { speedFromKnots } from '../common/converter'; import t from '../common/localization'; const Filter = ({ children, setItems }) => { @@ -19,7 +19,7 @@ const Filter = ({ children, setItems }) => { const positions = await response.json(); let formattedPositions = positions.map(position => { return { - speed: Number(speedConverter(position.speed, speedUnit)), + speed: Number(speedFromKnots(position.speed, speedUnit)), altitude: position.altitude, accuracy: position.accuracy, fixTime: formatDate(position.fixTime) diff --git a/modern/src/settings/MaintenancePage.js b/modern/src/settings/MaintenancePage.js index b5486f7a..5ac98c93 100644 --- a/modern/src/settings/MaintenancePage.js +++ b/modern/src/settings/MaintenancePage.js @@ -1,5 +1,6 @@ import React, { useState } from 'react'; import t from '../common/localization'; +import { prefixString } from '../common/stringUtils'; import EditItemView from '../EditItemView'; import { Accordion, AccordionSummary, AccordionDetails, makeStyles, Typography, TextField, FormControl, InputLabel, MenuItem, Select, } from '@material-ui/core'; import InputAdornment from '@material-ui/core/InputAdornment'; @@ -7,7 +8,7 @@ import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; import EditAttributesView from '../attributes/EditAttributesView'; import positionAttributes from '../attributes/positionAttributes'; import { useAttributePreference } from '../common/preferences'; -import { distanceConverter, speedConverter } from '../common/converter'; +import { speedFromKnots, speedToKnots, distanceFromMeters, distanceToMeters } from '../common/converter'; const useStyles = makeStyles(() => ({ details: { @@ -24,15 +25,18 @@ const MaintenancePage = () => { const speedUnit = useAttributePreference('speedUnit'); const distanceUnit = useAttributePreference('distanceUnit'); - const options = []; - - Object.entries(positionAttributes).map(([key, value]) => { - if (value.type === 'number') { - options.push({key, name: value.name, type: value.type}) + const convertToList = (attributes) => { + let otherList = []; + for (const key in attributes) { + const value = attributes[key]; + if (value.type === 'number') { + otherList.push({key, name: value.name, type: value.type}); + } } - }); + return otherList; + } - const handleChange = event => { + const onMaintenanceTypeChange = event => { const newValue = event.target.value; setItem({...item, type: newValue, start: 0, period: 0}); @@ -40,10 +44,10 @@ const MaintenancePage = () => { if (attribute && attribute.dataType) { switch (attribute.dataType) { case 'distance': - setLabels({ ...labels, start: distanceUnit, period: distanceUnit}); + setLabels({ ...labels, start: t(prefixString('shared', distanceUnit)), period: t(prefixString('shared', distanceUnit))}); break; case 'speed': - setLabels({ ...labels, start: speedUnit, period: speedUnit}); + setLabels({ ...labels, start: t(prefixString('shared', speedUnit)), period: t(prefixString('shared', speedUnit))}); break; default: break; @@ -51,22 +55,21 @@ const MaintenancePage = () => { } } - const rawToValue = (rawValue) => { + const rawToValue = (value) => { const attribute = positionAttributes[item.type]; if (attribute && attribute.dataType) { switch (attribute.dataType) { case 'speed': - return speedConverter(rawValue, speedUnit, false); + return speedFromKnots(value, speedUnit); case 'distance': - return distanceConverter(rawValue, distanceUnit, false); + return distanceFromMeters(value, distanceUnit); default: - return rawValue; + return value; } } - return rawValue; - + return value; } const valueToRaw = (value) => { @@ -75,9 +78,9 @@ const MaintenancePage = () => { if (attribute && attribute.dataType) { switch (attribute.dataType) { case 'speed': - return speedConverter(value, speedUnit, true); + return speedToKnots(value, speedUnit); case 'distance': - return distanceConverter(value, distanceUnit, true); + return distanceToMeters(value, distanceUnit); default: return value; } @@ -107,10 +110,10 @@ const MaintenancePage = () => { <InputLabel>{t('sharedType')}</InputLabel> <Select value={item.type || ''} - onChange={handleChange}> - {options.map((option) => ( - <MenuItem key={option.key} value={option.key}>{option.name}</MenuItem> - ))} + onChange={onMaintenanceTypeChange}> + {convertToList(positionAttributes).map(({ key, name })=>( + <MenuItem key={key} value={key}>{name}</MenuItem> + ))} </Select> </FormControl> <TextField |