|
| 1 | +import { createSelector, createStructuredSelector } from 'reselect'; |
| 2 | +import isEmpty from 'lodash/isEmpty'; |
| 3 | +import { formatNumber } from 'utils/format'; |
| 4 | + |
| 5 | +const getData = state => state.data; |
| 6 | +const getLocationName = state => state.locationName; |
| 7 | +const getColors = state => state.colors; |
| 8 | +const getSentences = state => state.config && state.config.sentences; |
| 9 | +const getTitle = state => state.config.title; |
| 10 | +const getSettings = state => state.settings; |
| 11 | + |
| 12 | +export const parseData = createSelector( |
| 13 | + [getData, getColors, getSettings], |
| 14 | + (data, colors, settings) => { |
| 15 | + if (isEmpty(data)) return null; |
| 16 | + const { adminData, plantData } = data; |
| 17 | + const { loss } = colors; |
| 18 | + |
| 19 | + const adminTotal = adminData |
| 20 | + .filter(d => d.year >= settings.startYear && d.year <= settings.endYear) |
| 21 | + .reduce((acc, next) => (next.emissions ? acc + next.emissions : acc), 0); |
| 22 | + |
| 23 | + const plantTotal = plantData |
| 24 | + .filter(d => d.year >= settings.startYear && d.year <= settings.endYear) |
| 25 | + .reduce((acc, next) => (next.emissions ? acc + next.emissions : acc), 0); |
| 26 | + const totalArea = adminTotal + plantTotal; |
| 27 | + const parsedData = [ |
| 28 | + { |
| 29 | + label: 'Natural forest', |
| 30 | + value: |
| 31 | + settings.unit === 'co2LossByYear' |
| 32 | + ? adminTotal |
| 33 | + : adminTotal / (44 / 12), |
| 34 | + unit: 't', |
| 35 | + color: loss.main, |
| 36 | + percentage: adminTotal / totalArea * 100 |
| 37 | + }, |
| 38 | + { |
| 39 | + label: 'Plantations', |
| 40 | + value: |
| 41 | + settings.unit === 'co2LossByYear' |
| 42 | + ? plantTotal |
| 43 | + : plantTotal / (44 / 12), |
| 44 | + unit: 't', |
| 45 | + color: loss.secondary, |
| 46 | + percentage: plantTotal / totalArea * 100 |
| 47 | + } |
| 48 | + ]; |
| 49 | + return parsedData; |
| 50 | + } |
| 51 | +); |
| 52 | + |
| 53 | +export const parseSentence = createSelector( |
| 54 | + [parseData, getLocationName, getSentences, getSettings], |
| 55 | + (parsedData, locationName, sentences, settings) => { |
| 56 | + if (!parsedData) return null; |
| 57 | + const { initial } = sentences; |
| 58 | + const { startYear, endYear } = settings; |
| 59 | + const plantationsPct = parsedData.find(d => d.label === 'Plantations') |
| 60 | + .percentage; |
| 61 | + const emissions = parsedData.find(d => d.label === 'Natural forest').value; |
| 62 | + |
| 63 | + const params = { |
| 64 | + location: locationName !== 'global' ? `${locationName}'s` : locationName, |
| 65 | + percentage: |
| 66 | + plantationsPct < 0.1 |
| 67 | + ? '<0.1%' |
| 68 | + : formatNumber({ num: plantationsPct, unit: '%' }), |
| 69 | + emissions: formatNumber({ num: emissions, unit: 't' }), |
| 70 | + startYear, |
| 71 | + endYear, |
| 72 | + variable: settings.unit === 'co2LossByYear' ? 'CO₂' : 'carbon' |
| 73 | + }; |
| 74 | + |
| 75 | + return { |
| 76 | + sentence: initial, |
| 77 | + params |
| 78 | + }; |
| 79 | + } |
| 80 | +); |
| 81 | + |
| 82 | +export const parseTitle = createSelector([getTitle], title => title.initial); |
| 83 | + |
| 84 | +export default createStructuredSelector({ |
| 85 | + data: parseData, |
| 86 | + sentence: parseSentence, |
| 87 | + title: parseTitle |
| 88 | +}); |
0 commit comments