Skip to content

Commit 2111935

Browse files
authored
Merge pull request #3416 from Vizzuality/feature/plantation-sentence
Plantations Extent Sentence
2 parents 0456fb4 + 316ec0e commit 2111935

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

app/javascript/components/widgets/widgets/land-cover/primary-forest/selectors.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const parseData = createSelector(
4646
export const getSentence = createSelector(
4747
[parseData, getSettings, getCurrentLocation, getIndicator, getSentences],
4848
(parsedData, settings, currentLabel, indicator, sentences) => {
49-
if (!parsedData || !currentLabel || !indicator) return null;
49+
if (!parsedData || !currentLabel) return null;
5050
const { initial, withIndicator } = sentences;
5151
const totalExtent = parsedData
5252
.filter(d => d.label !== 'Non-Forest')
@@ -57,8 +57,8 @@ export const getSentence = createSelector(
5757
totalExtent *
5858
100;
5959

60-
let indicatorLabel = indicator.label;
61-
switch (indicator.value) {
60+
let indicatorLabel = indicator && indicator.label;
61+
switch (indicator && indicator.value) {
6262
case 'primary_forest__mining':
6363
indicatorLabel = 'Mining concessions';
6464
break;
@@ -84,7 +84,9 @@ export const getSentence = createSelector(
8484
};
8585

8686
const sentence =
87-
indicator.value === 'primary_forest' ? initial : withIndicator;
87+
indicator && indicator.value === 'primary_forest'
88+
? initial
89+
: withIndicator;
8890

8991
return {
9092
sentence,

app/javascript/components/widgets/widgets/land-cover/tree-cover-plantations/initial-state.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ export default {
1414
},
1515
sentences: {
1616
initialSpecies:
17-
'In {location}, {firstSpecies} and {secondSpecies} represent the largest plantation area by {type}, spanning {extent}.',
17+
'In {location}, {firstSpecies} and {secondSpecies} represent the largest plantation area by {type}, spanning {extent} and {percent} of land area.',
1818
singleSpecies:
19-
'In {location}, {firstSpecies} represent the largest plantation area by {type}, spanning {extent}.',
20-
remainingSpecies:
21-
'The remaining {other} is distributed between {count} other plantation species.',
19+
'In {location}, {firstSpecies} represent the largest plantation area by {type}, spanning {extent} and {percent} of land area.',
2220
initialTypes:
23-
'In {location}, the largest plantation area by type are {topType}, spanning {extent}.'
21+
'In {location}, the largest plantation area by type are {topType}, spanning {extent} and {percent} of land area.'
2422
}
2523
},
2624
settings: {

app/javascript/components/widgets/widgets/land-cover/tree-cover-plantations/selectors.js

+7-12
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,13 @@ export const parseData = createSelector(
3838
);
3939

4040
export const getSentence = createSelector(
41-
[parseData, getSettings, getCurrentLocation, getSentences],
42-
(data, settings, currentLabel, sentences) => {
41+
[getData, parseData, getSettings, getCurrentLocation, getSentences],
42+
(rawData, data, settings, currentLabel, sentences) => {
4343
if (isEmpty(data) || !sentences) return null;
44-
const {
45-
initialSpecies,
46-
singleSpecies,
47-
remainingSpecies,
48-
initialTypes
49-
} = sentences;
44+
const { initialSpecies, singleSpecies, initialTypes } = sentences;
5045
const top =
5146
settings.type === 'bound2' ? data.slice(0, 2) : data.slice(0, 1);
52-
47+
const areaPerc = 100 * sumBy(top, 'value') / rawData.totalArea;
5348
const params = {
5449
location: currentLabel,
5550
firstSpecies: top[0].label.toLowerCase(),
@@ -58,13 +53,13 @@ export const getSentence = createSelector(
5853
extent: `${format('.3s')(sumBy(top, 'value'))}ha`,
5954
other: `${format('.3s')(sumBy(data.slice(2), 'value'))}ha`,
6055
count: data.length - top.length,
61-
topType: `${top[0].label}${endsWith(top[0].label, 's') ? '' : 's'}`
56+
topType: `${top[0].label}${endsWith(top[0].label, 's') ? '' : 's'}`,
57+
percent: areaPerc >= 0.1 ? `${format('.2r')(areaPerc)}%` : '<0.1%'
6258
};
6359
const sentence =
6460
settings.type === 'bound1'
6561
? initialTypes
66-
: `${top.length > 1 ? initialSpecies : singleSpecies} ${data.length >
67-
top.length && remainingSpecies}`;
62+
: `${top.length > 1 ? initialSpecies : singleSpecies}`;
6863

6964
return {
7065
sentence,

0 commit comments

Comments
 (0)