From 66a91a662134671ce4f2613364fcb575fd900890 Mon Sep 17 00:00:00 2001 From: Lucas Patenaude Date: Tue, 23 Apr 2024 21:25:53 -0600 Subject: [PATCH] Fix to favorites button code so page is refreshed each time it's clicked --- .../resources/js/club-page/favorite-button.js | 31 +++++++++++-------- .../js/club-page/refresh-for-favorite.js | 0 .../src/views/partials/footer.hbs | 2 +- 3 files changed, 19 insertions(+), 14 deletions(-) create mode 100644 ProjectSourceCode/src/resources/js/club-page/refresh-for-favorite.js diff --git a/ProjectSourceCode/src/resources/js/club-page/favorite-button.js b/ProjectSourceCode/src/resources/js/club-page/favorite-button.js index 5697a60..3d0651c 100644 --- a/ProjectSourceCode/src/resources/js/club-page/favorite-button.js +++ b/ProjectSourceCode/src/resources/js/club-page/favorite-button.js @@ -8,10 +8,9 @@ document.addEventListener("DOMContentLoaded", function() { var teamLogo = document.getElementById("teamLogo").value; if (favoriteButton.src.includes("/favorited.png")) { - removeFavoriteTeam(userID, teamID) - } - else { - addFavoriteTeam(userID, teamID, teamName, teamLogo) + removeFavoriteTeam(userID, teamID); + } else { + addFavoriteTeam(userID, teamID, teamName, teamLogo); } }); } @@ -35,16 +34,19 @@ async function addFavoriteTeam(userID, teamID, teamName, teamLogo){ if (!response.ok) { throw new Error('Failed to add favorite team'); } - //Changes button if favorite team is added// - console.log('New favorite team added successfully.'); - var favoriteButton = document.getElementById("club-favorite-button"); - favoriteButton.src = "/img/club-page/favorited.png"; + + if (response.status === 200) { + console.log('New favorite team added successfully.'); + var favoriteButton = document.getElementById("club-favorite-button"); + favoriteButton.src = "/img/club-page/favorited.png"; + location.reload(); // Refresh the page + } } catch (error) { console.error('Error adding favorite team:', error); - } } + async function removeFavoriteTeam(userID, teamID) { try { const response = await fetch('/favteam/remove', { @@ -57,10 +59,13 @@ async function removeFavoriteTeam(userID, teamID) { teamID: teamID }) }); - console.log('Favorite team removed successfully.'); - //Change button source// - var favoriteButton = document.getElementById("club-favorite-button"); - favoriteButton.src = "/img/club-page/unfavorited.png"; + + if (response.status === 200) { + console.log('Favorite team removed successfully.'); + var favoriteButton = document.getElementById("club-favorite-button"); + favoriteButton.src = "/img/club-page/unfavorited.png"; + location.reload(); // Refresh the page + } } catch (error) { console.error('Error removing favorite team:', error); diff --git a/ProjectSourceCode/src/resources/js/club-page/refresh-for-favorite.js b/ProjectSourceCode/src/resources/js/club-page/refresh-for-favorite.js new file mode 100644 index 0000000..e69de29 diff --git a/ProjectSourceCode/src/views/partials/footer.hbs b/ProjectSourceCode/src/views/partials/footer.hbs index dcdbd58..d652968 100644 --- a/ProjectSourceCode/src/views/partials/footer.hbs +++ b/ProjectSourceCode/src/views/partials/footer.hbs @@ -19,6 +19,6 @@ - + \ No newline at end of file