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

Add a table for phase data in tooltips; COUNTRY=cameroon #867

Merged
merged 15 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 14 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
163 changes: 148 additions & 15 deletions frontend/src/components/MapView/MapTooltip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,86 @@ import {
withStyles,
WithStyles,
LinearProgress,
Typography,
} from '@material-ui/core';
import { isEqual } from 'lodash';
import { tooltipSelector } from 'context/tooltipStateSlice';
import { isEmpty, isEqual, sum } from 'lodash';
import { PopupData, tooltipSelector } from 'context/tooltipStateSlice';
import { isEnglishLanguageSelected, useSafeTranslation } from 'i18n';
import { TFunction } from 'utils/data-utils';
import { ClassNameMap } from '@material-ui/styles';

// This function prepares phasePopulationTable for rendering and is specific
// to the data structure of the phase classification layer.
// Note - this is a bit hacky for now and will likely need to be revamped if we
// encounter other complex needs for tooltips.
const generatePhasePopulationTable = (
ericboucher marked this conversation as resolved.
Show resolved Hide resolved
popupData: PopupData,
t: TFunction,
classes: ClassNameMap,
) => {
const phasePopulations: Record<string, number> = Object.entries(
popupData,
).reduce((acc: any, cur: any) => {
const [key, value] = cur;
if (key.includes('Population in phase ')) {
// extract number from string looking like "Phase classification 1"
const phaseNumber = key.replace('Population in phase ', '');
if (phaseNumber) {
return { ...acc, [phaseNumber]: value.data };
}
}
return acc;
}, {});

if (isEmpty(phasePopulations)) {
return null;
}

// calculate total population
// eslint-disable-next-line no-param-reassign, fp/no-mutation
phasePopulations.Total =
sum(Object.values(phasePopulations)) - phasePopulations['3 to 5'];

const phasePopulationTable = (
<div>
<Typography display="inline" variant="h4" color="inherit">
{t('Ref. period')}: {popupData['Reference period start']?.data} -{' '}
{popupData['Reference period end']?.data}
</Typography>
<Typography variant="h4" color="inherit">
{t('Population and percentage by phase classification')}
</Typography>
<table className={classes.phasePopulationTable}>
<tr className={classes.phasePopulationTableRow}>
{Object.keys(phasePopulations).map((phaseName: string) => (
<th>{t(phaseName)}</th>
))}
</tr>
<tr className={classes.phasePopulationTableRow}>
{Object.values(phasePopulations).map((populationInPhase: number) => (
<th>{populationInPhase.toLocaleString()}</th>
))}
</tr>
<tr className={classes.phasePopulationTableRow}>
{Object.values(phasePopulations).map((populationInPhase: number) => (
<th>
{Math.round((populationInPhase / phasePopulations.Total) * 100)}%
</th>
))}
</tr>
</table>
</div>
);

return phasePopulationTable;
};

const MapTooltip = memo(({ classes }: TooltipProps) => {
const popup = useSelector(tooltipSelector);
const { t, i18n } = useSafeTranslation();

const popupData = popup.data;

const popupTitle = useMemo(() => {
if (isEnglishLanguageSelected(i18n)) {
return popup.locationName;
Expand All @@ -23,21 +94,64 @@ const MapTooltip = memo(({ classes }: TooltipProps) => {
}, [i18n, popup.locationLocalName, popup.locationName]);

const renderedPopupContent = useMemo(() => {
return Object.entries(popup.data)
const phasePopulationTable = generatePhasePopulationTable(
popupData,
t,
classes,
);
// filter out popupData where key value contains "Population in phase "
const popupDataWithoutPhasePopulations: PopupData = Object.entries(
popupData,
).reduce((acc: any, cur: any) => {
const [key, value] = cur;
if (
// keep "Population in phase 1" as a placeholder for the phase population table
key === 'Population in phase 1' ||
(!key.includes('Population in phase ') &&
!key.includes('Reference period '))
) {
return { ...acc, [key]: value };
}
return acc;
}, {});
return Object.entries(popupDataWithoutPhasePopulations)
.filter(([, value]) => {
return isEqual(value.coordinates, popup.coordinates);
})
.map(([key, value]) => {
return (
<Fragment key={key}>
<h4 key={key}>{`${t(key)}: ${value.data}`}</h4>
<h4>
{/* Allow users to show data without a key/title */}
{!key.includes('do_not_display') &&
key !== 'Population in phase 1' && (
<Typography
display="inline"
variant="h4"
color="inherit"
className={classes.text}
>
{`${t(key)}: `}
</Typography>
)}
{key !== 'Population in phase 1' && (
<Typography
display="inline"
variant="h4"
color="inherit"
className={classes.text}
>
{`${value.data}`}
</Typography>
)}
{/* Phase classification data */}
<Typography variant="h4" color="inherit">
{value.adminLevel && `${t('Admin Level')}: ${value.adminLevel}`}
</h4>
</Typography>
{key === 'Population in phase 1' && phasePopulationTable}
</Fragment>
);
});
}, [popup.coordinates, popup.data, t]);
}, [classes, popup.coordinates, popupData, t]);

const renderedPopupLoader = useMemo(() => {
if (!popup.wmsGetFeatureInfoLoading) {
Expand All @@ -51,18 +165,17 @@ const MapTooltip = memo(({ classes }: TooltipProps) => {
return null;
}
return (
<Popup
anchor="bottom"
coordinates={popup.coordinates}
className={classes.popup}
>
<h4>{popupTitle}</h4>
<Popup coordinates={popup.coordinates} className={classes.popup}>
<Typography variant="h4" color="inherit" className={classes.title}>
{popupTitle}
</Typography>
{renderedPopupContent}
{renderedPopupLoader}
</Popup>
);
}, [
classes.popup,
classes.title,
popup.coordinates,
popup.showing,
popupTitle,
Expand All @@ -73,17 +186,37 @@ const MapTooltip = memo(({ classes }: TooltipProps) => {

const styles = () =>
createStyles({
phasePopulationTable: {
tableLayout: 'fixed',
borderCollapse: 'collapse',
width: '100%',
borderWidth: '1px;',
borderColor: 'inherit',
borderStyle: 'solid',
border: '1px solid white',
},
phasePopulationTableRow: {
border: '1px solid white',
},
title: {
fontWeight: 600,
marginBottom: '8px',
},
text: {
marginBottom: '4px',
},
popup: {
'& div.mapboxgl-popup-content': {
background: 'black',
color: 'white',
padding: '10px 10px 10px',
padding: '5px 5px 5px 5px',
maxWidth: '30em',
maxHeight: '12em',
maxHeight: '20em',
overflow: 'auto',
},
'& div.mapboxgl-popup-tip': {
'border-top-color': 'black',
'border-bottom-color': 'black',
},
},
});
Expand Down
34 changes: 17 additions & 17 deletions frontend/src/config/nigeria/layers.json
Original file line number Diff line number Diff line change
Expand Up @@ -3650,8 +3650,8 @@
"2023-06-01"
],
"validityPeriod": {
"start_date_field" : "start_date",
"end_date_field" : "end_date"
"start_date_field": "start_date",
"end_date_field": "end_date"
},
"fallbackLayerKeys": ["ch_phase_admin1"],
"admin_level": 2,
Expand Down Expand Up @@ -3815,8 +3815,8 @@
"2023-06-01"
],
"validityPeriod": {
"start_date_field" : "start_date",
"end_date_field" : "end_date"
"start_date_field": "start_date",
"end_date_field": "end_date"
},
"data_field": "calc_pop_ph3_5_admin2",
"admin_level": 2,
Expand Down Expand Up @@ -3993,8 +3993,8 @@
"2023-06-01"
],
"validityPeriod": {
"start_date_field" : "start_date",
"end_date_field" : "end_date"
"start_date_field": "start_date",
"end_date_field": "end_date"
},
"admin_level": 2,
"data_field": "phase_class",
Expand Down Expand Up @@ -4102,8 +4102,8 @@
"2023-06-01"
],
"validityPeriod": {
"start_date_field" : "start_date",
"end_date_field" : "end_date"
"start_date_field": "start_date",
"end_date_field": "end_date"
},
"admin_level": 2,
"data_field": "calc_pop_ph3_5_admin2",
Expand Down Expand Up @@ -4234,8 +4234,8 @@
"2023-06-01"
],
"validityPeriod": {
"start_date_field" : "start_date",
"end_date_field" : "end_date"
"start_date_field": "start_date",
"end_date_field": "end_date"
},
"admin_level": 2,
"data_field": "phase_class",
Expand Down Expand Up @@ -4298,8 +4298,8 @@
"2023-06-01"
],
"validityPeriod": {
"start_date_field" : "start_date",
"end_date_field" : "end_date"
"start_date_field": "start_date",
"end_date_field": "end_date"
},
"admin_level": 2,
"data_field": "calc_pop_ph3_5_admin2",
Expand Down Expand Up @@ -4361,8 +4361,8 @@
"path": "data/nigeria/ch/urb_admin2_{YYYY_MM_DD}.json",
"dates": ["2021-02-01", "2021-06-01", "2022-02-01", "2022-06-01"],
"validityPeriod": {
"start_date_field" : "start_date",
"end_date_field" : "end_date"
"start_date_field": "start_date",
"end_date_field": "end_date"
},
"admin_level": 2,
"data_field": "phase_class",
Expand Down Expand Up @@ -4414,8 +4414,8 @@
"path": "data/nigeria/ch/urb_admin2_{YYYY_MM_DD}.json",
"dates": ["2022-02-01", "2022-06-01", "2023-06-01"],
"validityPeriod": {
"start_date_field" : "start_date",
"end_date_field" : "end_date"
"start_date_field": "start_date",
"end_date_field": "end_date"
},
"admin_level": 2,
"data_field": "calc_pop_ph3_5_admin2",
Expand Down Expand Up @@ -4470,4 +4470,4 @@
],
"legend_text": "Urban areas - population in phase 3 to 5"
}
}
}
Loading
Loading