Skip to content

Commit

Permalink
WV-3147 update collections when projection changes (#5122)
Browse files Browse the repository at this point in the history
* update collections when proj changes

* save collection proj to state and check it

* lint
  • Loading branch information
PatchesMaps authored Apr 22, 2024
1 parent cd88c3c commit e8aa908
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion web/js/containers/sidebar/layer-row.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ const makeMapStateToProps = () => {
const dailyDate = formatDailyDate(activeDate);
const selectedDate = date.selected;
const subdailyDate = formatSubdailyDate(activeDate);
const collections = getCollections(layers, dailyDate, subdailyDate, layer);
const collections = getCollections(layers, dailyDate, subdailyDate, layer, proj.id);
const measurementDescriptionPath = getDescriptionPath(state, ownProps);
const { ddvZoomAlerts, ddvLocationAlerts } = state.alerts;

Expand Down
16 changes: 11 additions & 5 deletions web/js/mapUI/components/update-collections/updateCollections.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function UpdateCollections () {
const proj = useSelector((state) => state.proj);
const sources = useSelector((state) => state.config.sources);
const layerConfig = useSelector((state) => state.layers.layerConfig);
const projId = useSelector((state) => state.proj.id);

// Finds the correct subdomain to query headers from based on the layer source and GIBS/GITC env
const lookupLayerSource = (layerId) => {
Expand Down Expand Up @@ -49,7 +50,7 @@ function UpdateCollections () {
if (type !== 'NRT' && type !== 'STD') return undefined;

return {
id, date: formattedDate, type, version,
id, date: formattedDate, type, version, projection: proj.id,
};
} catch (error) {
// errors will clutter console, turn this on for debugging
Expand All @@ -59,7 +60,7 @@ function UpdateCollections () {
return undefined;
};

const findLayerCollections = (layers, dailyDate, subdailyDate) => {
const findLayerCollections = (layers, dailyDate, subdailyDate, forceUpdate) => {
const wmtsLayers = layers.filter((layer) => {
if (layer.type !== 'wmts' || !layer.visible) return false;

Expand All @@ -70,15 +71,15 @@ function UpdateCollections () {

const collectionDate = layerInCollections.dates.some((d) => d.date === date);

return !collectionDate; // If date exists in layer collection, don't query layer
return !collectionDate || forceUpdate; // If date exists in layer collection, don't query layer
});
return wmtsLayers;
};

const updateLayerCollections = async () => {
const updateLayerCollections = async (forceUpdate = false) => {
const formattedDailyDate = formatDailyDate(selectedDate);
const formattedSubdailyDate = formatSubdailyDate(selectedDate);
const layersToUpdate = findLayerCollections(layers, formattedDailyDate, formattedSubdailyDate);
const layersToUpdate = findLayerCollections(layers, formattedDailyDate, formattedSubdailyDate, forceUpdate);
const headerPromises = layersToUpdate.map((layer) => getHeaders(layer, selectedDate));

try {
Expand All @@ -96,6 +97,11 @@ function UpdateCollections () {
updateLayerCollections();
}, [selectedDate, layers]);

useEffect(() => {
if (!layers.length) return;
updateLayerCollections(true);
}, [projId]);

return null;
}

Expand Down
20 changes: 17 additions & 3 deletions web/js/modules/layers/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,28 @@ export function layerReducer(state = initialState, action) {
const updates = {};
action.payload.forEach((collection) => {
const {
id, date, type, version,
id, date, type, version, projection,
} = collection;
// If the layer doesn't exist, initialize it
if (!state.collections[id]) {
updates[id] = { $set: { dates: [{ version, type, date }] } };
updates[id] = {
$set: {
dates: [{
version,
type,
date,
projection,
}],
},
};
} else {
// If the layer exists, prepare to push to the dates array
const newEntry = { date, type, version };
const newEntry = {
date,
type,
version,
projection,
};
updates[id] = {
dates: { $push: [newEntry] },
};
Expand Down
4 changes: 2 additions & 2 deletions web/js/modules/layers/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ export const getStartingLayers = createSelector([getConfig], (config) => resetLa

export const isGroupingEnabled = ({ compare, layers }) => layers[compare.activeString].groupOverlays;

export const getCollections = (layers, dailyDate, subdailyDate, layer) => {
export const getCollections = (layers, dailyDate, subdailyDate, layer, projId) => {
if (!layers.collections[layer.id]) return;
const dateCollection = layers.collections[layer.id].dates;
for (let i = 0; i < dateCollection.length; i += 1) {
if (dateCollection[i].date === dailyDate || dateCollection[i].date === subdailyDate) {
if ((dateCollection[i].date === dailyDate || dateCollection[i].date === subdailyDate) && dateCollection[i].projection === projId) {
return dateCollection[i];
}
}
Expand Down

0 comments on commit e8aa908

Please sign in to comment.