From 56e8d646924eab5e75436b24c3d924861c040185 Mon Sep 17 00:00:00 2001 From: Simao Rodrigues Date: Thu, 22 Jun 2023 15:55:07 +0100 Subject: [PATCH] Rename NDCS Explore actions to solve conflicts conflicts --- .../ndcs-explore-table/ndcs-explore-table.js | 2 +- .../ndcs-explore-provider-actions.js | 146 +++++++++--------- .../ndcs-explore-provider-reducers.js | 6 +- .../ndcs-explore-provider.js | 6 +- 4 files changed, 82 insertions(+), 78 deletions(-) diff --git a/app/javascript/app/components/ndcs/ndcs-explore-table/ndcs-explore-table.js b/app/javascript/app/components/ndcs/ndcs-explore-table/ndcs-explore-table.js index e4bcf61953..3fabe7cb8b 100644 --- a/app/javascript/app/components/ndcs/ndcs-explore-table/ndcs-explore-table.js +++ b/app/javascript/app/components/ndcs/ndcs-explore-table/ndcs-explore-table.js @@ -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 = { diff --git a/app/javascript/app/providers/ndcs-explore-provider/ndcs-explore-provider-actions.js b/app/javascript/app/providers/ndcs-explore-provider/ndcs-explore-provider-actions.js index 386c577475..af9b98198d 100644 --- a/app/javascript/app/providers/ndcs-explore-provider/ndcs-explore-provider-actions.js +++ b/app/javascript/app/providers/ndcs-explore-provider/ndcs-explore-provider-actions.js @@ -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 }; diff --git a/app/javascript/app/providers/ndcs-explore-provider/ndcs-explore-provider-reducers.js b/app/javascript/app/providers/ndcs-explore-provider/ndcs-explore-provider-reducers.js index 5d080b0885..edb9186a38 100644 --- a/app/javascript/app/providers/ndcs-explore-provider/ndcs-explore-provider-reducers.js +++ b/app/javascript/app/providers/ndcs-explore-provider/ndcs-explore-provider-reducers.js @@ -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( @@ -29,5 +29,5 @@ export default { ), true ), - fetchNDCSFail: state => setError(state, true) + fetchNDCSExploreFail: state => setError(state, true) }; diff --git a/app/javascript/app/providers/ndcs-explore-provider/ndcs-explore-provider.js b/app/javascript/app/providers/ndcs-explore-provider/ndcs-explore-provider.js index e8c28aaa12..d09aec4952 100644 --- a/app/javascript/app/providers/ndcs-explore-provider/ndcs-explore-provider.js +++ b/app/javascript/app/providers/ndcs-explore-provider/ndcs-explore-provider.js @@ -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 }); @@ -18,7 +18,7 @@ const NDCSExploreProvider = props => { }; NDCSExploreProvider.propTypes = { - fetchNDCS: PropTypes.func.isRequired + fetchNDCSExplore: PropTypes.func.isRequired }; export { actions, reducers, initialState };