Skip to content

Commit

Permalink
feat(widget): move logic to a proper selector
Browse files Browse the repository at this point in the history
  • Loading branch information
wri7tno committed Jan 11, 2024
1 parent 09e2875 commit c57bb02
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 48 deletions.
10 changes: 2 additions & 8 deletions components/widget/components/widget-footer/component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class WidgetFooter extends PureComponent {
static propTypes = {
type: PropTypes.string,
simple: PropTypes.bool,
alerts: PropTypes.oneOfType([PropTypes.array, PropTypes.func]),
alerts: PropTypes.array,
statements: PropTypes.array,
locationType: PropTypes.string,
showAttributionLink: PropTypes.bool,
Expand All @@ -20,13 +20,7 @@ class WidgetFooter extends PureComponent {
renderAlert = (alerts, alertSystem, type, locationType) => {
if (!alerts) return null;

let alertList = alerts;

if (typeof alerts === 'function') {
alertList = alerts();
}

return alertList.map((alert, index) => {
return alerts.map((alert, index) => {
if (alert.system === alertSystem || alertSystem === 'all') {
return (
<WidgetAlert
Expand Down
46 changes: 6 additions & 40 deletions components/widgets/forest-change/tree-loss-primary/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,46 +38,12 @@ export default {
subcategories: ['forest-loss'],
types: ['global', 'country', 'wdpa', 'aoi'],
admins: ['global', 'adm0', 'adm1', 'adm2'],
alerts: () => {
const countriesWithNewWarningText = [
'CMR',
'CIV',
'COD',
'GNQ',
'GAB',
'GHA',
'GIN',
'GNB',
'LBR',
'MDG',
'COG',
'SLE',
];

const matches = location.pathname.match(
new RegExp('/dashboards/country/([A-Z]){3,}/')
);
const country = matches[0].substring(
matches[0].length - 4,
matches[0].length - 1
);

if (countriesWithNewWarningText.includes(country)) {
return [
{
text: 'Testing {location}.',
visible: ['global', 'country', 'geostore', 'aoi', 'wdpa', 'use'],
},
];
}

return [
{
text: 'The methods behind this data have changed over time. Be cautious comparing old and new data, especially before/after 2015. [Read more here](https://www.globalforestwatch.org/blog/data-and-research/tree-cover-loss-satellite-data-trend-analysis/).',
visible: ['global', 'country', 'geostore', 'aoi', 'wdpa', 'use'],
},
];
},
alerts: [
{
text: 'The methods behind this data have changed over time. Be cautious comparing old and new data, especially before/after 2015. [Read more here](https://www.globalforestwatch.org/blog/data-and-research/tree-cover-loss-satellite-data-trend-analysis/).',
visible: ['global', 'country', 'geostore', 'aoi', 'wdpa', 'use'],
},
],
large: true,
visible: ['dashboard', 'analysis'],
chartType: 'composedChart',
Expand Down
34 changes: 34 additions & 0 deletions components/widgets/forest-change/tree-loss-primary/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const getPrimaryLoss = (state) => state.data && state.data.primaryLoss;
const getAdminLoss = (state) => state.data && state.data.adminLoss;
const getSettings = (state) => state.settings;
const getLocationLabel = (state) => state.locationLabel;
const getAlerts = (state) => state.alerts || [];
const getAdm0 = (state) => state.adm0;
const getIndicator = (state) => state.indicator;
const getColors = (state) => state.colors;
const getSentence = (state) => state && state.sentence;
Expand Down Expand Up @@ -162,6 +164,37 @@ export const parseTitle = createSelector(
}
);

export const parseAlerts = createSelector(
[getAlerts, getLocationLabel, getAdm0],
(alerts, locationLabel, adm0) => {
const countriesWithNewWarningText = [
'CMR',
'CIV',
'COD',
'GNQ',
'GAB',
'GHA',
'GIN',
'GNB',
'LBR',
'MDG',
'COG',
'SLE',
];

if (countriesWithNewWarningText.includes(adm0)) {
return [
{
text: `The methods behind this data have changed over time, resulting in an underreporting of tree cover loss in ${locationLabel} prior to 2015. We advise against comparing the data before/after 2015 in ${locationLabel}. [Read more here](https://www.globalforestwatch.org/blog/data-and-research/tree-cover-loss-satellite-data-trend-analysis/).`,
visible: ['global', 'country', 'geostore', 'aoi', 'wdpa', 'use'],
},
];
}

return alerts;
}
);

const parseSentence = createSelector(
[
parseData,
Expand Down Expand Up @@ -249,4 +282,5 @@ export default createStructuredSelector({
config: parseConfig,
sentence: parseSentence,
title: parseTitle,
alerts: parseAlerts,
});
34 changes: 34 additions & 0 deletions components/widgets/forest-change/tree-loss/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const getLocationLabel = (state) => state.locationLabel;
const getIndicator = (state) => state.indicator;
const getColors = (state) => state.colors;
const getSentence = (state) => state && state.sentence;
const getAlerts = (state) => state.alerts || [];
const getAdm0 = (state) => state.adm0;

const parseData = createSelector(
[getLoss, getExtent, getSettings],
Expand Down Expand Up @@ -161,8 +163,40 @@ const parseSentence = createSelector(
}
);

export const parseAlerts = createSelector(
[getAlerts, getLocationLabel, getAdm0],
(alerts, locationLabel, adm0) => {
const countriesWithNewWarningText = [
'CMR',
'CIV',
'COD',
'GNQ',
'GAB',
'GHA',
'GIN',
'GNB',
'LBR',
'MDG',
'COG',
'SLE',
];

if (countriesWithNewWarningText.includes(adm0)) {
return [
{
text: `The methods behind this data have changed over time, resulting in an underreporting of tree cover loss in ${locationLabel} prior to 2015. We advise against comparing the data before/after 2015 in ${locationLabel}. [Read more here](https://www.globalforestwatch.org/blog/data-and-research/tree-cover-loss-satellite-data-trend-analysis/).`,
visible: ['global', 'country', 'geostore', 'aoi', 'wdpa', 'use'],
},
];
}

return alerts;
}
);

export default createStructuredSelector({
data: zeroFillData,
config: parseConfig,
sentence: parseSentence,
alerts: parseAlerts,
});

0 comments on commit c57bb02

Please sign in to comment.