Skip to content

Commit

Permalink
Rename NDCS Explore actions to solve conflicts conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
SARodrigues committed Jun 30, 2023
1 parent 6391f83 commit 56e8d64
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 78 deletions.
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 @@ -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

0 comments on commit 56e8d64

Please sign in to comment.