Skip to content

Commit

Permalink
remove card moves on filter changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Hadrien Froger committed Oct 18, 2024
1 parent 1b2cf84 commit 09af96f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
47 changes: 29 additions & 18 deletions app/packs/src/decidim/geo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ async function fetchData() {
addProcess();
// Fetch all the data
await Promise.all([fetchAll(defaultFilters), pointsForFilters(activeFilters)]);

removeProcess();
}

async function displayMap() {
try {
const { addProcess, removeProcess } = pointStore.getState();
Expand All @@ -60,11 +62,13 @@ async function displayMap() {

map.addControl(new FilterControl());
map.addControl(new PageNameControl());
map.setMinZoom((configStore.getState().map_config.zoom || 15) - 2)
// Save the first loaded position.
await new Promise((resolve) => setTimeout(resolve, 120));
configStore.getState().setReady();
removeProcess();
setTimeout(setSavedPosition, 320);

});

pointStore.subscribe(
Expand All @@ -75,9 +79,11 @@ async function displayMap() {
state._lastResponse
],
([isLoading, getFilteredPoints, fetchesRunning]) => {
if (isLoading > 0) {
const {isInitialized} = pointStore.getState();
if (isLoading > 0 || !isInitialized) {
return;
}

const { map, pointsLayer } = configStore.getState();
const { selectedPoint, selectedScope } = geoStore.getState();
const { savedCenter } = memoryStore.getState();
Expand All @@ -93,6 +99,7 @@ async function displayMap() {
});
}


const pointInMap = getFilteredPoints().filter(
(node) => typeof node.isGeoLocated !== "undefined" && node.isGeoLocated()
);
Expand All @@ -102,23 +109,23 @@ async function displayMap() {
pointsLayer.addLayer(marker);
});
}
if (
pointInMap.length > 0 &&
!fetchesRunning &&
!savedCenter &&
!selectedScope &&
!selectedPoint
) {
const idealBoundingBox = pointInMap.map(({ marker }) => marker);
const boundingBox = L.featureGroup(
_.isEmpty(idealBoundingBox)
? pointInMap.map(({ marker }) => marker)
: idealBoundingBox,
{ updateWhenZooming: true }
);

map.fitBounds(boundingBox.getBounds(), { padding: [64, 64] });
}
// if (
// pointInMap.length > 0 &&
// !fetchesRunning &&
// !selectedScope &&
// !selectedPoint
// ) {
// const boundingBox = L.featureGroup(pointInMap.map(({ marker }) => marker)).getBounds();
// if(map.getBounds().contains(boundingBox)){
// return;
// }
// map.fitBounds(
// boundingBox,
// {
// padding: configStore.getState().isFullScreen ? [8, 8] : [16, 16]
// }
// );
// }
}
);

Expand Down Expand Up @@ -188,10 +195,14 @@ async function main() {
await fetchData();

await displayMap();


}

window.addEventListener("load", function () {
main().then(() => {
console.log("decidim geo ready");
setTimeout(() => pointStore.setState(() => ({isInitialized: true})), 640)

});
});
4 changes: 3 additions & 1 deletion app/packs/src/decidim/geo/models/GeoDatasourceNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export default class GeoDatasourceNode {
return true;
}
isGeoLocated() {
return !!this.marker;
if(!this.marker) return false;
const latlng = this.marker.getLatLng()
return latlng.lat && latlng.lng;
}

repaint() {
Expand Down
1 change: 0 additions & 1 deletion app/packs/src/decidim/geo/stores/memoryStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const store = createStore(
popState: () => {
const { savedCenter, savedZoom } = get();
const { map, mapReady } = configStore.getState();
console.log("popState", { savedCenter, savedZoom, mapReady });
if (!savedCenter || !savedZoom || !mapReady) return null;
map.setView(savedCenter, savedZoom, { animate: false });
}
Expand Down
9 changes: 5 additions & 4 deletions app/packs/src/decidim/geo/stores/pointStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const store = createStore(
scopes: [],
fetchedGeoScopes: [],
selectedGeoScopeIds: [],
isInitialized: false,
isLoading: 0,
_lastFilter: "",
_lastResponse: [],
Expand Down Expand Up @@ -98,7 +99,7 @@ const store = createStore(
(f) => typeof f.timeFilter === "undefined"
);
set(({ fetchesRunning }) => ({ fetchesRunning: fetchesRunning + 1 }));
getGeoDataSource(
await getGeoDataSource(
{ filters: filterWithoutTime, locale, isIndex },
true,
(data, hasMore, meta) => {
Expand Down Expand Up @@ -147,12 +148,12 @@ const store = createStore(
}));
filteredIdsStore.setState(() => ({ activeFilterIds: [] }));

getGeoDataSource({ filters, locale: locale, isIndex }, false, (data, hasMore) => {
await getGeoDataSource({ filters, locale: locale, isIndex }, false, (data, hasMore) => {
filteredIdsStore.setState(({ activeFilterIds }) => ({
activeFilterIds: activeFilterIds.concat(data || [])
}));
set(({ fetchesRunning }) => ({
fetchesRunning: fetchesRunning - (hasMore ? 0 : 1)
set(({ fetchesRunning}) => ({
fetchesRunning: fetchesRunning - (hasMore ? 0 : 1),
}));
get()._updateCache();
});
Expand Down

0 comments on commit 09af96f

Please sign in to comment.