1
1
import { createSelector , createStructuredSelector } from 'reselect' ;
2
2
import sumBy from 'lodash/sumBy' ;
3
- import groupBy from 'lodash/groupBy' ;
4
3
import uniqBy from 'lodash/uniqBy' ;
5
4
import { formatNumber } from 'utils/format' ;
6
5
import { getColorPalette } from 'components/widgets/utils/colors' ;
7
- import { zeroFillYears } from 'components/widgets/utils/data' ;
6
+ import { zeroFillYearsFilter } from 'components/widgets/utils/data' ;
8
7
9
8
// get list data
10
- const getLossPlantations = ( state ) => state . data && state . data . lossPlantations ;
11
9
const getTotalLoss = ( state ) => state . data && state . data . totalLoss ;
12
10
const getSettings = ( state ) => state . settings ;
13
11
const getLocationName = ( state ) => state . locationLabel ;
12
+ const getTitle = ( state ) => state . title ;
14
13
const getColors = ( state ) => state . colors ;
15
14
const getSentence = ( state ) => state . sentence ;
15
+ const getAdminLevel = ( state ) => state . adminLevel ;
16
16
17
17
// get lists selected
18
18
export const parseData = createSelector (
19
- [ getLossPlantations , getTotalLoss , getSettings ] ,
20
- ( lossPlantations , totalLoss , settings ) => {
21
- if ( ! lossPlantations || ! totalLoss ) return null ;
19
+ [ getTotalLoss , getSettings ] ,
20
+ ( totalLoss , settings ) => {
21
+ if ( ! totalLoss ) return null ;
22
22
const { startYear, endYear, yearsRange } = settings ;
23
23
const years = yearsRange && yearsRange . map ( ( yearObj ) => yearObj . value ) ;
24
24
const fillObj = {
@@ -28,44 +28,68 @@ export const parseData = createSelector(
28
28
emissions : 0 ,
29
29
percentage : 0 ,
30
30
} ;
31
- const zeroFilledData = zeroFillYears (
32
- lossPlantations ,
31
+ const zeroFilledData = zeroFillYearsFilter (
32
+ totalLoss ,
33
33
startYear ,
34
34
endYear ,
35
35
years ,
36
36
fillObj
37
37
) ;
38
- const totalLossByYear = groupBy ( totalLoss , 'year' ) ;
39
- const parsedData = uniqBy (
40
- zeroFilledData
41
- . filter ( ( d ) => d . year >= startYear && d . year <= endYear )
42
- . map ( ( d ) => {
43
- const groupedPlantations = groupBy ( lossPlantations , 'year' ) [ d . year ] ;
44
- const summedPlatationsLoss =
45
- ( groupedPlantations && sumBy ( groupedPlantations , 'area' ) ) || 0 ;
46
- const summedPlatationsEmissions =
47
- ( groupedPlantations && sumBy ( groupedPlantations , 'emissions' ) ) || 0 ;
48
- const totalLossForYear =
49
- ( totalLossByYear [ d . year ] && totalLossByYear [ d . year ] [ 0 ] ) || { } ;
50
38
51
- const returnData = {
52
- ...d ,
53
- outsideAreaLoss : totalLossForYear . area - summedPlatationsLoss ,
54
- areaLoss : summedPlatationsLoss || 0 ,
55
- totalLoss : totalLossForYear . area || 0 ,
56
- outsideCo2Loss :
57
- totalLossByYear [ d . year ] ?. [ 0 ] ?. emissions -
58
- summedPlatationsEmissions ,
59
- co2Loss : summedPlatationsEmissions || 0 ,
60
- } ;
61
- return returnData ;
62
- } ) ,
63
- 'year'
64
- ) ;
39
+ const mappedData = zeroFilledData . map ( ( list ) => {
40
+ const naturalForestList = list . filter (
41
+ ( item ) => item . sbtn_natural_forests__class === 'Natural Forest'
42
+ ) ;
43
+
44
+ const nonNaturalForestList = list . filter (
45
+ ( item ) => item . sbtn_natural_forests__class === 'Non-Natural Forest'
46
+ ) ;
47
+ // eslint-disable-next-line no-unused-vars
48
+ const unknownList = list . filter (
49
+ ( item ) => item . sbtn_natural_forests__class === 'Unknown'
50
+ ) ;
51
+
52
+ const naturalForestArea = naturalForestList ?. reduce (
53
+ ( acc , curr ) => acc + curr . area ,
54
+ 0
55
+ ) ;
56
+ const naturalForestEmissions = naturalForestList ?. reduce (
57
+ ( acc , curr ) => acc + curr . emissions ,
58
+ 0
59
+ ) ;
60
+ const nonNaturalForestArea = nonNaturalForestList ?. reduce (
61
+ ( acc , curr ) => acc + curr . area ,
62
+ 0
63
+ ) ;
64
+ const nonNaturalForestEmissions = nonNaturalForestList ?. reduce (
65
+ ( acc , curr ) => acc + curr . emissions ,
66
+ 0
67
+ ) ;
68
+
69
+ return {
70
+ iso : nonNaturalForestList [ 0 ] ?. iso || '' ,
71
+ outsideAreaLoss : naturalForestArea || 0 ,
72
+ outsideCo2Loss : naturalForestEmissions || 0 ,
73
+ areaLoss : nonNaturalForestArea || 0 ,
74
+ co2Loss : nonNaturalForestEmissions || 0 ,
75
+ totalLoss : ( nonNaturalForestArea || 0 ) + ( naturalForestArea || 0 ) ,
76
+ year : nonNaturalForestList [ 0 ] ?. year || '' ,
77
+ } ;
78
+ } ) ;
79
+
80
+ const parsedData = uniqBy ( mappedData , 'year' ) ;
81
+
65
82
return parsedData ;
66
83
}
67
84
) ;
68
85
86
+ export const parseTitle = createSelector (
87
+ [ getTitle , getLocationName ] ,
88
+ ( title , name ) => {
89
+ return name === 'global' ? title . global : title . default ;
90
+ }
91
+ ) ;
92
+
69
93
export const parseConfig = createSelector ( [ getColors ] , ( colors ) => {
70
94
const colorRange = getColorPalette ( colors . ramp , 2 ) ;
71
95
return {
@@ -103,7 +127,7 @@ export const parseConfig = createSelector([getColors], (colors) => {
103
127
} ,
104
128
{
105
129
key : 'areaLoss' ,
106
- label : 'Plantations ' ,
130
+ label : 'Non-natural tree cover ' ,
107
131
color : colorRange [ 0 ] ,
108
132
unitFormat : ( value ) =>
109
133
formatNumber ( { num : value , unit : 'ha' , spaceUnit : true } ) ,
@@ -113,21 +137,18 @@ export const parseConfig = createSelector([getColors], (colors) => {
113
137
} ) ;
114
138
115
139
export const parseSentence = createSelector (
116
- [ parseData , getSettings , getLocationName , getSentence ] ,
117
- ( data , settings , locationName , sentence ) => {
140
+ [ parseData , getSettings , getLocationName , getSentence , getAdminLevel ] ,
141
+ ( data , settings , locationName , sentences , admLevel ) => {
118
142
if ( ! data ) return null ;
119
143
const { startYear, endYear } = settings ;
120
- const plantationsLoss = sumBy ( data , 'areaLoss' ) || 0 ;
121
144
const totalLoss = sumBy ( data , 'totalLoss' ) || 0 ;
122
145
const outsideLoss = sumBy ( data , 'outsideAreaLoss' ) || 0 ;
123
146
const outsideEmissions = sumBy ( data , 'outsideCo2Loss' ) || 0 ;
147
+ const sentenceSubkey = admLevel === 'global' ? 'global' : 'region' ;
148
+ const sentence = sentences [ sentenceSubkey ] ;
124
149
125
- const lossPhrase =
126
- plantationsLoss > outsideLoss ? 'plantations' : 'natural forest' ;
127
- const percentage =
128
- plantationsLoss > outsideLoss
129
- ? ( 100 * plantationsLoss ) / totalLoss
130
- : ( 100 * outsideLoss ) / totalLoss ;
150
+ const lossPhrase = 'natural forest' ;
151
+ const percentage = ( 100 * outsideLoss ) / totalLoss ;
131
152
const params = {
132
153
location : locationName ,
133
154
startYear,
@@ -139,6 +160,7 @@ export const parseSentence = createSelector(
139
160
spaceUnit : true ,
140
161
} ) ,
141
162
percentage : formatNumber ( { num : percentage , unit : '%' } ) ,
163
+ totalLoss : formatNumber ( { num : outsideLoss , unit : 'ha' } ) , // using outsideLoss (natural forest) value based on Michelle's feedback
142
164
} ;
143
165
144
166
return {
@@ -152,4 +174,5 @@ export default createStructuredSelector({
152
174
data : parseData ,
153
175
config : parseConfig ,
154
176
sentence : parseSentence ,
177
+ title : parseTitle ,
155
178
} ) ;
0 commit comments