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 NDC Enhancement page crashing/not loading #1667

Merged
merged 6 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const getIndicatorsParsed = createSelector(
'value'
),
'label'
).filter(ind => ind.categoryIds.indexOf(parseInt(categoryId, 10)) > -1);
).filter(ind => ind.categoryIds?.indexOf(parseInt(categoryId, 10)) > -1);
}
);

Expand Down Expand Up @@ -247,14 +247,14 @@ export const getPathsWithStyles = createSelector(
...COUNTRY_STYLES,
default: {
...COUNTRY_STYLES.default,
'stroke-width': strokeWidth,
strokeWidth,
fill: color,
fillOpacity: 1
},
hover: {
...COUNTRY_STYLES.hover,
cursor: 'pointer',
'stroke-width': strokeWidth,
strokeWidth,
fill: color,
fillOpacity: 1
}
Expand Down Expand Up @@ -296,7 +296,7 @@ export const getPreviousComparisonCountryValues = createSelector(
[getPreviousComparisonIndicators, getISOCountries],
(previousComparisonIndicators, isos) => {
if (!previousComparisonIndicators) return null;
const previousComparisonCountryValues = {};
const previousComparisonCountryValues = [];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like an object. We are adding attributes to it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for catching that.

isos.forEach(iso => {
previousComparisonCountryValues[
iso
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ export const getIndicatorsParsed = createSelector(
),
'label'
);

const filteredIndicators = sortAndParseIndicators(indicators).filter(
ind => ind.categoryIds.indexOf(parseInt(categoryId, 10)) > -1
ind => ind.categoryIds?.indexOf(parseInt(categoryId, 10)) > -1
);

return previousComparisonIndicators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from './ndcs-explore-table-selectors';

const mapStateToProps = (state, { location }) => {
const { data, loading } = state.ndcs;
const { data, loading } = state.ndcsExplore;
const { countries } = state;
const search = qs.parse(location.search);
const ndcsNDCSWithSelection = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const NDCSEnhancements = ({ route }) => (
{route.sections &&
route.sections.length > 0 &&
route.sections.map(section => (
<div className={styles.section}>
<div key={section.hash} className={styles.section}>
<div id={section.hash} className={styles.sectionHash} />
<section.component />
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,87 +4,91 @@ import { apiWithCache } from 'services/api';
import uniqBy from 'lodash/uniqBy';
import { getFirstDocumentValue } from 'utils/indctransform';

const fetchNDCSInit = createAction('fetchNDCSInit');
const fetchNDCSReady = createAction('fetchNDCSReady');
const fetchNDCSFail = createAction('fetchNDCSFail');
const fetchNDCSExploreInit = createAction('fetchNDCSExploreInit');
const fetchNDCSExploreReady = createAction('fetchNDCSExploreReady');
const fetchNDCSExploreFail = createAction('fetchNDCSExploreFail');

const fetchNDCS = createThunkAction('fetchNDCS', props => (dispatch, state) => {
const { document } = props || {};
const { ndcsExplore } = state();
const params = [];
const isDocumentSelected = document && document !== 'all';
const fetchNDCSExplore = createThunkAction(
'fetchNDCSExplore',
props => (dispatch, state) => {
const { document } = props || {};
const { ndcsExplore } = state();
const params = [];
const isDocumentSelected = document && document !== 'all';

if (isDocumentSelected) {
params.push(`document=${document}`);
}
if (isDocumentSelected) {
params.push(`document=${document}`);
}

const promises = [];
if (ndcsExplore && !ndcsExplore.loading) {
promises.push(
apiWithCache.get(
`/api/v1/ndcs?filter=map&source[]=Climate%20Watch&source[]=WB&source[]=NDC%20Explorer&source[]=UNICEF${
params.length ? `&${params.join('&')}` : ''
}`
)
);
}

const promises = [];
if (ndcsExplore && !ndcsExplore.loading) {
// Used for indicators like ndce_ghg (emissions) that are needed but not included on category filtered calls
// and as it is not filtered by category also serves the whole list of categories
promises.push(
apiWithCache.get(
`/api/v1/ndcs?filter=map&source[]=Climate%20Watch&source[]=WB&source[]=NDC%20Explorer&source[]=UNICEF${
params.length ? `&${params.join('&')}` : ''
}`
'/api/v1/ndcs?indicators=ndce_ghg,submission,submission_date'
)
);
}

// Used for indicators like ndce_ghg (emissions) that are needed but not included on category filtered calls
// and as it is not filtered by category also serves the whole list of categories
promises.push(
apiWithCache.get(
'/api/v1/ndcs?indicators=ndce_ghg,submission,submission_date'
)
);

// Used for vulnerability indicator that is needed but not included on ndcs indicators
promises.push(
apiWithCache.get(
'/api/v1/country_profile/indicators?indicator=vulnerability'
)
);
// Used for vulnerability indicator that is needed but not included on ndcs indicators
promises.push(
apiWithCache.get(
'/api/v1/country_profile/indicators?indicator=vulnerability'
)
);

dispatch(fetchNDCSInit());
Promise.all(promises)
.then(async response => {
if (response.length && response[0].data) {
if (!response[1] || !response[1].data) {
return response[0].data;
}
return {
categories: {
...response[0].data.categories,
...response[1].data.categories
},
indicators: uniqBy(
response[0].data.indicators
.concat(response[1].data.indicators)
.concat(
(response[2] && response[2].data && response[2].data.data) || []
),
'id'
),
sectors: {
...response[0].data.sectors,
...response[1].data.sectors
dispatch(fetchNDCSExploreInit());
Promise.all(promises)
.then(async response => {
if (response.length && response[0].data) {
if (!response[1] || !response[1].data) {
return response[0].data;
}
};
}
throw Error(response.statusText);
})
.then(data => getFirstDocumentValue(data))
.then(data => {
dispatch(fetchNDCSReady(data));
})
.catch(error => {
console.warn(error);
dispatch(fetchNDCSFail());
});
});
return {
categories: {
...response[0].data.categories,
...response[1].data.categories
},
indicators: uniqBy(
response[0].data.indicators
.concat(response[1].data.indicators)
.concat(
(response[2] && response[2].data && response[2].data.data) ||
[]
),
'id'
),
sectors: {
...response[0].data.sectors,
...response[1].data.sectors
}
};
}
throw Error(response.statusText);
})
.then(data => getFirstDocumentValue(data))
.then(data => {
dispatch(fetchNDCSExploreReady(data));
})
.catch(error => {
console.warn(error);
dispatch(fetchNDCSExploreFail());
});
}
);

export default {
fetchNDCS,
fetchNDCSInit,
fetchNDCSReady,
fetchNDCSFail
fetchNDCSExplore,
fetchNDCSExploreInit,
fetchNDCSExploreReady,
fetchNDCSExploreFail
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const setError = (state, error) => ({ ...state, error });
const setLoaded = (state, loaded) => ({ ...state, loaded });

export default {
fetchNDCSInit: state => setLoading(state, true),
fetchNDCSExploreInit: state => setLoading(state, true),
// eslint-disable-next-line no-confusing-arrow
fetchNDCSReady: (state, { payload }) =>
fetchNDCSExploreReady: (state, { payload }) =>
!state.data || !payload
? null
: setLoaded(
Expand All @@ -29,5 +29,5 @@ export default {
),
true
),
fetchNDCSFail: state => setError(state, true)
fetchNDCSExploreFail: state => setError(state, true)
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import reducers, { initialState } from './ndcs-explore-provider-reducers';
import actions from './ndcs-explore-provider-actions';

const NDCSExploreProvider = props => {
const { fetchNDCS, subcategory, document } = props;
const { fetchNDCSExplore, subcategory, document } = props;

useEffect(() => {
fetchNDCS({
fetchNDCSExplore({
subcategory,
document
});
Expand All @@ -18,7 +18,7 @@ const NDCSExploreProvider = props => {
};

NDCSExploreProvider.propTypes = {
fetchNDCS: PropTypes.func.isRequired
fetchNDCSExplore: PropTypes.func.isRequired
};
export { actions, reducers, initialState };

Expand Down
6 changes: 3 additions & 3 deletions app/javascript/app/utils/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function getColorByIndex(data, index, colors = buckets) {

export function createLegendBuckets(
locations,
labels,
labels = {},
isos,
notApplicableLabel = 'Not Applicable'
) {
Expand All @@ -50,11 +50,11 @@ export function createLegendBuckets(
// If we have a zoom level we show the points until we reach the MIN_ZOOM_SHOW_ISLANDS level
// If we dont we always show the points instead of the islands
export const shouldShowPath = (path, zoom) =>
(zoom
zoom
? (zoom >= MIN_ZOOM_SHOW_ISLANDS &&
(path.properties.layer === PATH_LAYERS.COUNTRIES ||
path.properties.layer === PATH_LAYERS.ISLANDS)) ||
(zoom < MIN_ZOOM_SHOW_ISLANDS &&
(path.properties.layer === PATH_LAYERS.COUNTRIES ||
path.properties.layer === PATH_LAYERS.POINTS))
: path.properties.layer !== PATH_LAYERS.ISLANDS);
: path.properties.layer !== PATH_LAYERS.ISLANDS;
Loading