|
1 | 1 | import { createSelector } from 'reselect';
|
2 | 2 | import uniqBy from 'lodash/uniqBy';
|
| 3 | +import sumBy from 'lodash/sumBy'; |
3 | 4 | import findIndex from 'lodash/findIndex';
|
4 | 5 | import { sortByKey } from 'utils/data';
|
5 | 6 | import { format } from 'd3-format';
|
@@ -80,30 +81,27 @@ export const parseData = createSelector(
|
80 | 81 | export const getSentence = createSelector(
|
81 | 82 | [getSortedData, getSettings, getIndicator, getCurrentLocation, getSentences],
|
82 | 83 | (data, settings, indicator, currentLocation, sentences) => {
|
83 |
| - if (!data || !data.length || !currentLocation) return null; |
84 |
| - const { |
85 |
| - initial, |
86 |
| - withGain, |
87 |
| - withIndicator, |
88 |
| - withGainAndIndicator |
89 |
| - } = sentences; |
| 84 | + if (!data || !data.length) return null; |
| 85 | + const { initial, withIndicator } = sentences; |
90 | 86 | const locationData =
|
91 | 87 | currentLocation && data.find(l => l.id === currentLocation.value);
|
92 | 88 | const gain = locationData && locationData.gain;
|
| 89 | + const globalPercent = gain ? 100 * gain / sumBy(data, 'gain') : 0; |
93 | 90 | const areaPercent = (locationData && locationData.percentage) || 0;
|
| 91 | + const indicatorName = indicator ? indicator.label : 'region-wide'; |
94 | 92 |
|
95 | 93 | const params = {
|
96 | 94 | location: currentLocation && currentLocation.label,
|
97 | 95 | gain: `${format('.3s')(gain)}ha`,
|
98 |
| - region: indicator ? indicator.label : 'region-wide', |
99 |
| - percentage: `${format('.1f')(areaPercent)}%`, |
| 96 | + indicator: indicatorName.toLowerCase(), |
| 97 | + indicator_alt: indicatorName.toLowerCase(), |
| 98 | + percent: areaPercent >= 0.1 ? `${format('.1f')(areaPercent)}%` : '<0.1%', |
| 99 | + globalPercent: |
| 100 | + globalPercent >= 0.1 ? `${format('.1f')(globalPercent)}%` : '<0.1%', |
100 | 101 | extentYear: settings.extentYear
|
101 | 102 | };
|
102 | 103 |
|
103 |
| - let sentence = gain ? withGain : initial; |
104 |
| - if (indicator) { |
105 |
| - sentence = gain ? withGainAndIndicator : withIndicator; |
106 |
| - } |
| 104 | + const sentence = indicator ? withIndicator : initial; |
107 | 105 |
|
108 | 106 | return {
|
109 | 107 | sentence,
|
|
0 commit comments