Skip to content

Commit

Permalink
476 fix units
Browse files Browse the repository at this point in the history
  • Loading branch information
jbperidypathtech committed Jul 18, 2023
1 parent 7d2fa0f commit c88327f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
isAnalysisLayerActiveSelector,
} from 'context/analysisResultStateSlice';
import { legendToStops } from 'components/MapView/Layers/layer-utils';
import { AggregationOperations, LegendDefinition } from 'config/types';
import { AggregationOperations, LegendDefinition, units } from 'config/types';
import {
BaselineLayerResult,
ExposedPopulationResult,
Expand Down Expand Up @@ -113,7 +113,7 @@ function AnalysisLayer({ before }: { before?: string }) {
formattedProperties[statisticKey],
t,
precision,
)} %`,
)} ${units[statisticKey] || ''}`,
coordinates,
},
}),
Expand All @@ -126,7 +126,7 @@ function AnalysisLayer({ before }: { before?: string }) {
formattedProperties.stats_intersect_area || null,
t,
precision,
)} km²`,
)} ${units.stats_intersect_area}`,
coordinates,
},
}),
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,11 @@ export enum AggregationOperations {
'Area exposed' = 'intersect_percentage',
}

export const units: Partial<Record<AggregationOperations | string, string>> = {
intersect_percentage: '%',
stats_intersect_area: 'km²',
};

export const aggregationOperationsToDisplay: Record<
AggregationOperations,
string
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/utils/analysis-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ export function getAnalysisTableColumns(
id: statistic,
label: aggregationOperationsToDisplay[statistic],
format: (value: string | number) =>
getRoundedData(100 * (Number(value) || 0), undefined, 2, '%'),
getRoundedData(value, undefined, 2, statistic),
},
];

Expand All @@ -739,7 +739,7 @@ export function getAnalysisTableColumns(
id: 'stats_intersect_area',
label: 'Area exposed in sq km',
format: (value: string | number) =>
getRoundedData(value as number, undefined, 2, 'km²'),
getRoundedData(value as number, undefined, 2, statistic),
});
}

Expand Down
21 changes: 13 additions & 8 deletions frontend/src/utils/data-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,32 @@ import { TFunction as _TFunction } from 'i18next';
import { isNumber } from 'lodash';
import { TableRowType } from 'context/tableStateSlice';
import { i18nTranslator } from 'i18n';
import { AggregationOperations, units } from 'config/types';

export type TFunction = _TFunction;

export function getRoundedData(
data: number | string | null,
t?: i18nTranslator,
decimals: number = 3,
unit?: string,
statistic?: AggregationOperations | string,
): string {
if (isNumber(data) && Number.isNaN(data)) {
/* eslint-disable fp/no-mutation */
let result = data;
if (isNumber(result) && Number.isNaN(result)) {
return '-';
}
let result = '';
if (isNumber(data)) {
// eslint-disable-next-line fp/no-mutation
result = parseFloat(data.toFixed(decimals)).toLocaleString();
if (statistic === AggregationOperations['Area exposed']) {
result = 100 * (Number(result) || 0);
}
if (isNumber(result)) {
result = parseFloat(result.toFixed(decimals)).toLocaleString();
} else {
// TODO - investigate why we received string 'null' values in data.
// eslint-disable-next-line fp/no-mutation
result = data && data !== 'null' ? data : 'No Data';
result = result && result !== 'null' ? result : 'No Data';
/* eslint-enable fp/no-mutation */
}
const unit = statistic && units[statistic];
return `${t ? t(result) : result} ${unit || ''}`;
}

Expand Down

0 comments on commit c88327f

Please sign in to comment.