Skip to content

Commit

Permalink
chore: Refactor granuleLayerBuilder to use array of params for CMR re…
Browse files Browse the repository at this point in the history
…quest
  • Loading branch information
PatchesMaps committed Aug 12, 2024
1 parent fcb5886 commit f81598d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
18 changes: 9 additions & 9 deletions web/js/map/granule/granule-layer-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,23 @@ export default function granuleLayerBuilder(cache, store, createLayerWMTS) {
const state = store.getState();
const { proj: { selected: { crs } } } = state;
const getGranulesUrl = getGranulesUrlSelector(state);
const params = getParamsForGranuleRequest(def, selectedDate, crs);
const nrtParams = getParamsForGranuleRequest(def, selectedDate, crs, true);
const paramsArray = getParamsForGranuleRequest(def, selectedDate, crs);
let data = [];
let nrtData = [];
try {
showLoading();
const requestUrl = getGranulesUrl(params);
const nrtRequestUrl = getGranulesUrl(nrtParams);
const requests = [fetch(requestUrl, CMR_AJAX_OPTIONS), fetch(nrtRequestUrl, CMR_AJAX_OPTIONS)];
const responses = await Promise.allSettled(requests);
const promises = paramsArray.map((params) => {
const requestUrl = getGranulesUrl(params);
return fetch(requestUrl, CMR_AJAX_OPTIONS);
});
const responses = await Promise.allSettled(promises);
const fulfilledResponses = responses.filter(({ status }) => status === 'fulfilled').map(({ value }) => value);
const [response, nrtResponse] = fulfilledResponses;
const jsonRequests = [response.json(), nrtResponse.json()];
const jsonRequests = [response?.json(), nrtResponse?.json()];
const jsonResponses = await Promise.allSettled(jsonRequests);
const [responseJson, nrtResponseJson] = jsonResponses.filter(({ status }) => status === 'fulfilled').map(({ value }) => value);
data = responseJson.feed.entry;
nrtData = nrtResponseJson.feed.entry;
data = responseJson?.feed?.entry || [];
nrtData = nrtResponseJson?.feed?.entry || [];
} catch (e) {
console.error(e);
throttleDispathCMRErrorDialog(title);
Expand Down
31 changes: 26 additions & 5 deletions web/js/map/granule/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ export const getCMRQueryDates = (crs, selectedDate) => {
* @param {*} def - layer definition
* @param {*} date - "current" date from which to base the query
* @param {*} crs
* @returns
* @returns {Array} - array of objects with parameters for CMR request
*/
export const getParamsForGranuleRequest = (def, date, crs, nrt) => {
export const getParamsForGranuleRequest = (def, date, crs) => {
const dayNightFilter = def.daynight[0];
const bboxForProj = {
[CRS.WEB_MERCATOR]: [-180, -65, 180, 65],
Expand All @@ -173,7 +173,7 @@ export const getParamsForGranuleRequest = (def, date, crs, nrt) => {
};
const { startQueryDate, endQueryDate } = getCMRQueryDates(crs, date);

const getShortName = () => {
const getShortName = (nrt) => {
try {
const { shortName } = def.conceptIds[0];
if (nrt) return shortName;
Expand All @@ -184,14 +184,35 @@ export const getParamsForGranuleRequest = (def, date, crs, nrt) => {
}
};

return {
if (def.conceptIds[0].type === 'NRT') {
return [
{
shortName: getShortName(false),
startDate: startQueryDate.toISOString(),
endDate: endQueryDate.toISOString(),
dayNight: dayNightFilter,
bbox: bboxForProj[crs],
pageSize: 500,
},
{
shortName: getShortName(true),
startDate: startQueryDate.toISOString(),
endDate: endQueryDate.toISOString(),
dayNight: dayNightFilter,
bbox: bboxForProj[crs],
pageSize: 500,
},
];
}

return [{
shortName: getShortName(),
startDate: startQueryDate.toISOString(),
endDate: endQueryDate.toISOString(),
dayNight: dayNightFilter,
bbox: bboxForProj[crs],
pageSize: 500,
};
}];
};

/**
Expand Down

0 comments on commit f81598d

Please sign in to comment.