From 235ce5716edc2f7cbe80fdccf26eac7269587839 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Mon, 08 Jun 2026 04:38:18 +0000
Subject: [PATCH] =FavouritesManager.php and FavouritesRoutes.php fixes. Moving all logic to FavouritesManager.php. Still some left to do

---
 assets/js/concise/FrontendFavourites.js |   77 ++++++++++++++++++++++++++++----------
 1 files changed, 56 insertions(+), 21 deletions(-)

diff --git a/assets/js/concise/FrontendFavourites.js b/assets/js/concise/FrontendFavourites.js
index 94e2920..47befbe 100644
--- a/assets/js/concise/FrontendFavourites.js
+++ b/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();
+		}
+	});
 });
 
 

--
Gitblit v1.10.0