Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Ranked gain widget parent location #3707

Merged
merged 4 commits into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 47 additions & 12 deletions app/javascript/components/widgets/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const selectWhitelists = state =>
adm1: state.whitelists.regions
}
: {});
export const setectCountryData = state =>
export const selectCountryData = state =>
(state.countryData
? {
adm0: state.countryData.countries,
Expand All @@ -51,6 +51,10 @@ const locationTypes = {
label: 'country',
value: 'admin'
},
global: {
label: 'global',
value: 'global'
},
geostore: {
label: 'your custom area',
value: 'geostore'
Expand Down Expand Up @@ -89,7 +93,7 @@ export const getActiveWhitelist = createSelector(
);

export const getLocationData = createSelector(
[selectLocationType, setectCountryData],
[selectLocationType, selectCountryData],
(type, countryData) => {
if (type === 'country' || type === 'global') return countryData;
return {};
Expand All @@ -113,6 +117,14 @@ export const getChildLocationData = createSelector(
}
);

export const getParentLocationData = createSelector(
[getLocationData, selectLocation],
(locationData, location) => {
if (!location.adm1 && !location.adm2) return null;
return locationData[location.adm2 ? 'adm1' : 'adm0'];
}
);

export const getLocationDict = createSelector(
[getLocationData, selectLocation],
(locationData, location) => {
Expand All @@ -136,16 +148,39 @@ export const getLocationDict = createSelector(
);

export const getLocationObject = createSelector(
[getActiveLocationData, selectLocation],
(adms, location) => {
const { type, adm0, adm1, adm2 } = location;
if (type !== 'country') {
return locationTypes[type];
[getAdminLevel, getActiveLocationData, selectLocation, getParentLocationData],
(adminLevel, adms, location, parent) => {
if (location.type !== 'country') {
return locationTypes[location.type];
}
if (!adms) return null;
return adm0
? adms.find(a => a.value === (adm2 || adm1 || adm0))
: { label: type, value: type };

const locationObject = location.adm0
? adms.find(a => a.value === location[adminLevel])
: { label: location.type, value: location.type };
let parentObject = {};

if (adminLevel === 'adm0') {
parentObject = { label: 'global', value: 'global' };
} else if (adminLevel === 'adm1') {
parentObject = parent.find(a => a.value === location.adm0) || {
label: location.type,
value: location.type
};
} else if (adminLevel === 'adm2') {
parentObject = parent.find(a => a.value === location.adm1) || {
label: location.type,
value: location.type
};
}

const returnObject = {
parentLabel: parentObject.label,
parentValue: parentObject.value,
...locationObject,
adminLevel
};

return returnObject;
}
);

Expand All @@ -155,7 +190,7 @@ export const getLocationName = createSelector(
);

export const getFAOLocationData = createSelector(
[setectCountryData],
[selectCountryData],
countryData => countryData.faoCountries
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const parseSentence = createSelector(
location: locationName !== 'global' ? `${locationName}'s` : locationName,
percentage:
plantationsPct < 0.1
? '<0.1%'
? '< 0.1%'
: formatNumber({ num: plantationsPct, unit: '%' }),
emissions: formatNumber({ num: emissions, unit: 't' }),
startYear,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export const parseSentence = createSelector(
location_alt: `${locationName}'s`,
percentage:
Math.abs(emissionFraction) < 0.1
? '<0.1%'
? '< 0.1%'
: `${format('.2r')(Math.abs(emissionFraction))}%`,
value: `${format('.3s')(
Math.abs(emissionsCount / (endYear - startYear))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export default {
globalWithIndicator:
'From 2001 to 2012, {gain} of tree cover was gained within {indicator} {location}.',
initial:
'From 2001 to 2012, {location} gained {gain} of tree cover equal to {gainPercent} of global total.',
'From 2001 to 2012, {location} gained {gain} of tree cover equal to {gainPercent} of the {parent} total.',
withIndicator:
'From 2001 to 2012, {location} gained {gain} of tree cover in {indicator} equal to {gainPercent} of global total.',
'From 2001 to 2012, {location} gained {gain} of tree cover in {indicator} equal to {gainPercent} of the {parent} total.',
regionInitial:
'From 2001 to 2012, {location} gained {gain} of tree cover {indicator} equal to {gainPercent} of all tree cover gain in {parent}.',
regionWithIndicator:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ const getTitle = state => state.config.title;
const getLocationType = state => state.locationType || null;
const getAllLocation = state => state.allLocation || null;

const getAdminLevel = state => state.adminLevel || null;
const getParentLocation = state => state[state.parentLevel] || null;

const haveData = (data, locationObject) =>
locationObject &&
data &&
Expand Down Expand Up @@ -137,19 +134,9 @@ export const parseSentence = createSelector(
getIndicator,
getLocationObject,
getLocationName,
getSentences,
getParentLocation,
getAdminLevel
getSentences
],
(
data,
indicator,
locationObject,
currentLabel,
sentences,
parent,
adminLevel
) => {
(data, indicator, locationObject, currentLabel, sentences) => {
if (
!data ||
!data.length ||
Expand All @@ -171,20 +158,22 @@ export const parseSentence = createSelector(
const gainPercent = gain ? 100 * gain / sumBy(data, 'gain') : 0;
const areaPercent = (locationData && locationData.percentage) || 0;

const adminLevel = locationObject.adminLevel || 'global';

const params = {
location: currentLabel === 'global' ? 'globally' : currentLabel,
gain: gain < 1 ? `${format('.3r')(gain)}ha` : `${format('.3s')(gain)}ha`,
indicator: (indicator && indicator.label.toLowerCase()) || 'region-wide',
percent: areaPercent >= 0.1 ? `${format('.2r')(areaPercent)}%` : '<0.1%',
percent: areaPercent >= 0.1 ? `${format('.2r')(areaPercent)}%` : '< 0.1%',
gainPercent:
gainPercent >= 0.1 ? `${format('.2r')(gainPercent)}%` : '<0.1%',
parent: parent && parent.label
gainPercent >= 0.1 ? `${format('.2r')(gainPercent)}%` : '< 0.1%',
parent: locationObject.parentLabel || null
};

let sentence = indicator ? withIndicator : initial;
if (adminLevel === 'region' || adminLevel === 'subRegion') {
if (adminLevel === 'adm1' || adminLevel === 'adm2') {
sentence = indicator ? regionWithIndicator : regionInitial;
} else if (adminLevel === 'global' || adminLevel === 'subRegion') {
} else if (adminLevel === 'global') {
sentence = indicator ? globalWithIndicator : globalInitial;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ export const parseSentence = createSelector(
? `${format('.3r')(lossArea)}ha`
: `${format('.3s')(lossArea)}ha`,
localPercent:
areaPercent >= 0.1 ? `${format('.2r')(areaPercent)}%` : '<0.1%',
areaPercent >= 0.1 ? `${format('.2r')(areaPercent)}%` : '< 0.1%',
globalPercent:
lossPercent >= 0.1 ? `${format('.2r')(lossPercent)}%` : '<0.1%',
lossPercent >= 0.1 ? `${format('.2r')(lossPercent)}%` : '< 0.1%',
extentYear: settings.extentYear
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const parseSentence = createSelector(
? `${format('.3r')(extent)}ha`
: `${format('.3s')(extent)}ha`,
primaryPercent:
primaryPercent >= 0.1 ? `${format('.2r')(primaryPercent)}%` : '<0.1%'
primaryPercent >= 0.1 ? `${format('.2r')(primaryPercent)}%` : '< 0.1%'
};
let sentence = forest_primary > 0 ? initial : noPrimary;
if (locationName === 'global') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ export const parseSentence = createSelector(
location: locationName !== 'global' ? `${locationName}'s` : locationName,
indicator: indicatorLabel,
percentage:
intactPercentage < 0.1 ? '<0.1%' : `${format('.2r')(intactPercentage)}%`
intactPercentage < 0.1
? '< 0.1%'
: `${format('.2r')(intactPercentage)}%`
};

let sentence =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const parseSentence = createSelector(
indicator: indicatorLabel,
percentage:
primaryPercentage < 0.1
? '<0.1%'
? '< 0.1%'
: `${format('.2r')(primaryPercentage)}%`,
extentYear: settings.extentYear
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ export const parseSentence = createSelector(

const topRegionPercent =
topRegion.percentage < 0.1
? '<0.1%'
? '< 0.1%'
: `${format('.2r')(topRegion.percentage)}%`;
const aveRegionPercent =
avgExtentPercentage < 0.1
? '<0.1%'
? '< 0.1%'
: `${format('.2r')(avgExtentPercentage)}%`;

const params = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const parseSentence = createSelector(
: `${format('.3s')(otherExtent)}ha`,
count: data.length - top.length,
topType: `${top[0].label}${endsWith(top[0].label, 's') ? '' : 's'}`,
percent: areaPerc >= 0.1 ? `${format('.2r')(areaPerc)}%` : '<0.1%'
percent: areaPerc >= 0.1 ? `${format('.2r')(areaPerc)}%` : '< 0.1%'
};
const sentence =
settings.type === 'bound1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ export const parseSentence = createSelector(
: `${format('.3s')(extent)}ha`,
indicator: indicator && indicator.label.toLowerCase(),
landPercentage:
landPercent >= 0.1 ? `${format('.2r')(landPercent)}%` : '<0.1%',
landPercent >= 0.1 ? `${format('.2r')(landPercent)}%` : '< 0.1%',
globalPercentage:
globalPercent >= 0.1 ? `${format('.2r')(globalPercent)}%` : '<0.1%'
globalPercent >= 0.1 ? `${format('.2r')(globalPercent)}%` : '< 0.1%'
};

let sentence = indicator ? withInd : initial;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export const parseSentence = createSelector(
percentage:
selectedFAO[0].net_perc >= 0.1
? `${format('2r')(selectedFAO[0].net_perc)}%`
: '<0.1%',
: '< 0.1%',
year: settings.year
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const parseSentence = createSelector(
const params = {
location: `${locationObject && locationObject && locationObject.label}'s`,
value: `${employees ? format('.3s')(employees) : 'no'}`,
percent: percentage >= 0.1 ? `${format('.2r')(percentage)}%` : '<0.1%',
percent: percentage >= 0.1 ? `${format('.2r')(percentage)}%` : '< 0.1%',
year
};

Expand Down