Jake Vanderwerf
2 days ago 235ce5716edc2f7cbe80fdccf26eac7269587839
assets/js/concise/FrontendFavourites.js
@@ -1,11 +1,15 @@
class FrontendFavourites {
class Favourites {
   constructor() {
      // Initialize DataStore for queue persistence
      this.store = window.jvbStore.register(
      let store = window.jvbStore.register(
         'favourites',
         {
            storeName: 'favourites',
            keyPath: 'id',
            endpoint: 'favourites',
            headers: {
               'X-Action-Nonce': window.auth.getNonce('favourites')
            },
            indexes: [
               {name: 'content', keyPath: 'content'},
               {name: 'listId', keyPath: 'listId'},
@@ -22,14 +26,16 @@
            }
         });
      this.listStore = window.jvbStore.register(
         'favourites_lists',
         {
            storeName: 'lists',
            keyPath: 'listId',
            endpoint: 'favourites/lists',
            TTL: 6 * 60 * 1000,
         });
      this.store = store.favourites;
      // this.listStore = window.jvbStore.register(
      //    'favourites_lists',
      //    {
      //       storeName: 'lists',
      //       keyPath: 'listId',
      //       endpoint: 'favourites/lists',
      //       TTL: 6 * 60 * 1000,
      //    });
      this.store.subscribe((event, data) => {
         switch (event) {
@@ -46,8 +52,6 @@
         }
      });
      this.store.fetch();
   }
   toggleFavourite(button) {
@@ -67,12 +71,40 @@
      // Update button icon
      button.innerHTML = jvbSettings.icons[button.classList.contains('favourited') ? 'heart-filled' : 'heart'];
      this.store.setItem(button.dataset.id, {
         target_id: button.dataset.id,
         action: action,
         type: button.dataset.type,
         artist: button.dataset.artist,
      });
      window.debouncer.schedule(
         `favourite-${button.dataset.id}`,
         this.postFavourite({
            user: window.auth.getUser(),
            target_id: button.dataset.id,
            action: action,
            type: button.dataset.type
         }),
         100
      );
   }
   async postFavourite(args) {
      await window.auth.fetch(
         `${jvbSettings.api}favourites`,
         {
            method: 'POST',
            headers: {
               'X-Action-Nonce': window.auth.getNonce('favourites')
            },
            body: args
         }
      );
      if (args.action === 'add') {
         await this.store.setItem(button.dataset.id, {
            target_id: button.dataset.id,
            action: action,
            type: button.dataset.type,
         }).then(()=>{});
      } else {
         await this.store.delete(button.dataset.id).then(()=>{});
      }
   }
   // async toggleFavourite(itemType, itemId) {
@@ -181,11 +213,14 @@
      return this.store.get(favId) !== undefined;
   }
}
document.addEventListener('DOMContentLoaded', function() {
   window.jvbFavourites = false;
   if (window.auth.getUser() !== '') {
      window.jvbFavourites = new FrontendFavourites();
   }
   window.auth.subscribe((event) => {
      if (event === 'auth-loaded') {
         window.jvbFavourites = new Favourites();
      }
   });
});