Fix to favorites button code so page is refreshed each time it's clicked

This commit is contained in:
Lucas Patenaude
2024-04-23 21:25:53 -06:00
parent 4d8a87ed57
commit 66a91a6621
3 changed files with 19 additions and 14 deletions

View File

@@ -8,10 +8,9 @@ document.addEventListener("DOMContentLoaded", function() {
var teamLogo = document.getElementById("teamLogo").value; var teamLogo = document.getElementById("teamLogo").value;
if (favoriteButton.src.includes("/favorited.png")) { if (favoriteButton.src.includes("/favorited.png")) {
removeFavoriteTeam(userID, teamID) removeFavoriteTeam(userID, teamID);
} } else {
else { addFavoriteTeam(userID, teamID, teamName, teamLogo);
addFavoriteTeam(userID, teamID, teamName, teamLogo)
} }
}); });
} }
@@ -35,16 +34,19 @@ async function addFavoriteTeam(userID, teamID, teamName, teamLogo){
if (!response.ok) { if (!response.ok) {
throw new Error('Failed to add favorite team'); throw new Error('Failed to add favorite team');
} }
//Changes button if favorite team is added//
console.log('New favorite team added successfully.'); if (response.status === 200) {
var favoriteButton = document.getElementById("club-favorite-button"); console.log('New favorite team added successfully.');
favoriteButton.src = "/img/club-page/favorited.png"; var favoriteButton = document.getElementById("club-favorite-button");
favoriteButton.src = "/img/club-page/favorited.png";
location.reload(); // Refresh the page
}
} catch (error) { } catch (error) {
console.error('Error adding favorite team:', error); console.error('Error adding favorite team:', error);
} }
} }
async function removeFavoriteTeam(userID, teamID) { async function removeFavoriteTeam(userID, teamID) {
try { try {
const response = await fetch('/favteam/remove', { const response = await fetch('/favteam/remove', {
@@ -57,10 +59,13 @@ async function removeFavoriteTeam(userID, teamID) {
teamID: teamID teamID: teamID
}) })
}); });
console.log('Favorite team removed successfully.');
//Change button source// if (response.status === 200) {
var favoriteButton = document.getElementById("club-favorite-button"); console.log('Favorite team removed successfully.');
favoriteButton.src = "/img/club-page/unfavorited.png"; var favoriteButton = document.getElementById("club-favorite-button");
favoriteButton.src = "/img/club-page/unfavorited.png";
location.reload(); // Refresh the page
}
} catch (error) { } catch (error) {
console.error('Error removing favorite team:', error); console.error('Error removing favorite team:', error);

View File

@@ -19,6 +19,6 @@
<!-- Club Pages Scripts --> <!-- Club Pages Scripts -->
<script src="/js/club-page/favorite-button.js"></script> <script src="/js/club-page/favorite-button.js"></script>
<script src="/js/club-page/player-card-hover.js"></script> <script src="/js/club-page/refresh-for-favorite.js"></script>
</footer> </footer>