Skip to content

Commit be3dd44

Browse files
Merge pull request #138 from CodeForPhilly/codex/fix-plant-tile-flickering-on-mobile
Fix mobile flicker
2 parents f848a9c + 557be5a commit be3dd44

File tree

1 file changed

+22
-45
lines changed

1 file changed

+22
-45
lines changed

src/components/Explorer.vue

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -994,26 +994,8 @@ export default {
994994
} else {
995995
// First update the counts
996996
await this.updateCounts();
997-
998-
// Reset the results array to force a re-render
999-
const oldResults = [...this.results];
1000-
this.results = [];
1001-
1002-
// Force an immediate submit to update the display
997+
// Submit without clearing current results to avoid flicker on mobile
1003998
this.submit();
1004-
1005-
// Use Vue's nextTick to ensure DOM updates after data changes
1006-
this.$nextTick(() => {
1007-
// If submit hasn't populated results yet, restore the old ones temporarily
1008-
if (this.results.length === 0 && oldResults.length > 0) {
1009-
this.results = [...oldResults];
1010-
1011-
// Then force another update after a short delay
1012-
setTimeout(() => {
1013-
this.forceUpdate();
1014-
}, 50);
1015-
}
1016-
});
1017999
}
10181000
},
10191001
deep: true,
@@ -1269,16 +1251,15 @@ export default {
12691251
this.submitTimeout = null;
12701252
}
12711253
this.submitTimeout = setTimeout(submit.bind(this), 50); // Reduced timeout for faster response
1272-
1254+
12731255
function submit() {
1274-
// Force a complete reset of the results to trigger reactivity
1256+
// Reset pagination values
12751257
this.page = 0;
12761258
this.loadedAll = false;
12771259
this.total = 0; // Reset total as well
1278-
this.results = [];
1279-
1280-
// Immediately force a fetch of new data
1281-
this.fetchPage();
1260+
1261+
// Fetch new data and replace results when it arrives
1262+
this.fetchPage(true);
12821263
12831264
// Restart infinite scroll monitoring
12841265
this.restartLoadMoreIfNeeded();
@@ -1287,17 +1268,8 @@ export default {
12871268
this.filtersOpen = false;
12881269
this.submitTimeout = null;
12891270
1290-
// Force DOM update by directly manipulating the DOM after a short delay
1291-
setTimeout(() => {
1292-
if (typeof document !== 'undefined') {
1293-
// This is a browser-only technique to force a repaint
1294-
const plants = document.querySelectorAll('.plant');
1295-
if (plants.length === 0) {
1296-
// If no plants are visible yet, force a fetchPage again
1297-
this.fetchPage();
1298-
}
1299-
}
1300-
}, 100);
1271+
// Allow any queued DOM updates to complete
1272+
this.$nextTick(() => {});
13011273
}
13021274
},
13031275
async updateCounts() {
@@ -1340,12 +1312,14 @@ export default {
13401312
doUpdate();
13411313
});
13421314
},
1343-
async fetchPage() {
1315+
async fetchPage(replace = false) {
13441316
this.loading = true;
13451317
if (this.favorites && ![...this.$store.state.favorites].length) {
13461318
// Avoid a query that would result in seeing all of the plants
13471319
// in the database as "favorites"
1348-
this.results = [];
1320+
if (replace) {
1321+
this.results = [];
1322+
}
13491323
this.loadedAll = true;
13501324
this.total = 0;
13511325
this.loading = false;
@@ -1378,13 +1352,16 @@ export default {
13781352
if (!data.results.length || this.favorites || this.questions) {
13791353
this.loadedAll = true;
13801354
}
1381-
// Prevent duplicate plants by checking if they already exist in the results array
1382-
data.results.forEach((datum) => {
1383-
// Only add the plant if it's not already in the results array
1384-
if (!this.results.some(existing => existing._id === datum._id)) {
1385-
this.results.push(datum);
1386-
}
1387-
});
1355+
if (replace) {
1356+
this.results = data.results;
1357+
} else {
1358+
// Prevent duplicate plants by checking if they already exist in the results array
1359+
data.results.forEach((datum) => {
1360+
if (!this.results.some((existing) => existing._id === datum._id)) {
1361+
this.results.push(datum);
1362+
}
1363+
});
1364+
}
13881365
this.total = data.total;
13891366
this.loading = false;
13901367
},

0 commit comments

Comments
 (0)