Skip to content

Commit b10e2e5

Browse files
authored
Fix bug with favoriting app with 0 favorites not registering
1 parent c15cfda commit b10e2e5

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

js/index.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//FAV WITH PILL AND ANIM
12
let appJSON = []; // List of apps and info from apps.json
23
let appSortInfo = {}; // list of data to sort by, from appdates.csv { created, modified }
34
let appCounts = {};
@@ -548,19 +549,19 @@ librarySearchInput.addEventListener('input', evt => {
548549

549550

550551
function getAppFavorites(app){
551-
let info = appSortInfo[app.id];
552-
let appFavourites = 0;
553-
if (info.favourites) {
554-
let favsThisSession = SETTINGS.appsFavoritedThisSession.find(obj => obj.id === app.id);
555-
appFavourites = info.favourites;
556-
if(favsThisSession){
557-
if(info.favourites!=favsThisSession.favs){
558-
//database has been updated, remove app from favsThisSession
559-
SETTINGS.appsFavoritedThisSession = SETTINGS.appsFavoritedThisSession.filter(obj => obj.id !== app.id);
560-
}
561-
else{
562-
appFavourites += 1; //add one to give the illusion of immediate database changes
563-
}
552+
let info = appSortInfo[app.id] || {};
553+
// start with whatever number we have in the database (may be undefined -> treat as 0)
554+
let appFavourites = (typeof info.favourites === 'number') ? info.favourites : 0;
555+
let favsThisSession = SETTINGS.appsFavoritedThisSession.find(obj => obj.id === app.id);
556+
if (favsThisSession) {
557+
// If the database count changed since we recorded the session-favourite, it means
558+
// the server/db has been updated and our optimistic session entry is stale.
559+
if (typeof info.favourites === 'number' && info.favourites !== favsThisSession.favs) {
560+
// remove stale session entry
561+
SETTINGS.appsFavoritedThisSession = SETTINGS.appsFavoritedThisSession.filter(obj => obj.id !== app.id);
562+
} else {
563+
// otherwise include our optimistic +1 so the UI updates immediately
564+
appFavourites += 1;
564565
}
565566
}
566567
return appFavourites;
@@ -862,13 +863,13 @@ function refreshLibrary(options) {
862863
changeAppFavourite(!favourite, app,false);
863864
if (icon) icon.classList.toggle("icon-favourite-active", !favourite);
864865
if (icon) icon.classList.add("favoriteAnim");
865-
// update visible count optimistically
866+
// update visible count optimistically (always update, even if 0)
866867
let cnt = getAppFavorites(app);
867-
if (!cnt) return "";
868-
let txt = (cnt > 999) ? Math.round(cnt/1000)+"k" : cnt;
868+
let txt = (cnt > 999) ? Math.round(cnt/1000)+"k" : cnt;
869869
let countEl = button.querySelector('.fav-count');
870-
countEl.textContent = String(txt);
870+
if (countEl) countEl.textContent = String(txt);
871871
const ANIM_MS = 500;
872+
// ensure animation class is removed after the duration so it can be re-triggered
872873
setTimeout(() => {
873874
try { if (icon) icon.classList.remove("favoriteAnim"); } catch (e) {}
874875
}, ANIM_MS);

0 commit comments

Comments
 (0)