diff --git a/apps/Recipes/Templates/single.php b/apps/Recipes/Templates/single.php index 3b55acd..0cd22ae 100644 --- a/apps/Recipes/Templates/single.php +++ b/apps/Recipes/Templates/single.php @@ -11,6 +11,8 @@ the_header( ); ?> + +
@@ -72,8 +74,8 @@ the_header(
- { qrPopup.classList.add("hidden"); overlay.classList.add("hidden"); } +}); + +const favoriteBtn = document.getElementById('favorite-btn'); +const favoriteIcon = favoriteBtn.querySelector('i'); + + +favoriteBtn.addEventListener('click', async (e) => { + const isFavorite = favoriteBtn.classList.contains('active'); + const type = isFavorite ? 'remove' : 'add'; + + const formData = new FormData(); + formData.append('action', 'favorites'); + formData.append('recipe_id', recipeId); + formData.append('type', type); + + favoriteBtn.disabled = true; + const response = await fetch('/ajax', { + method: 'POST', + body: formData + }); + favoriteBtn.disabled = false; + + const json = await response.json(); + + if(!response.ok) { + const message = json.error; + showToastify(message, 'error'); + return; + } + favoriteBtn.classList.toggle('active'); + + if(type == 'add') { + favoriteIcon.classList.remove('fa-regular'); + favoriteIcon.classList.add('fa-solid'); + } else { + favoriteIcon.classList.add('fa-regular'); + favoriteIcon.classList.remove('fa-solid'); + } + + showToastify(json.success, 'success'); }); \ No newline at end of file