Files
JellySport/ProjectSourceCode/src/resources/js/club-page/favorite-button.js

76 lines
2.6 KiB
JavaScript
Raw Normal View History

document.addEventListener("DOMContentLoaded", function() {
var favoriteButton = document.getElementById("club-favorite-button");
if (favoriteButton) {
favoriteButton.addEventListener("click", function() {
var userID = document.getElementById("userID").value;
var teamID = document.getElementById("teamID").value;
var teamName = document.getElementById("teamName").value;
var teamLogo = document.getElementById("teamLogo").value;
if (favoriteButton.src.includes("/favorited.png")) {
removeFavoriteTeam(userID, teamID)
.then((rmv_status) => {
console.log(rmv_status);
if (rmv_status === 200) {
favoriteButton.src = "/img/club-page/unfavorited.png";
}
})
.catch(error => {
console.error('Error removing favorite team:', error);
});
} else {
addFavoriteTeam(userID, teamID, teamName, teamLogo)
.then(() => {
favoriteButton.src = "/img/club-page/favorited.png";
})
.catch(error => {
console.error('Error adding favorite team:', error);
});
}
});
}
});
async function addFavoriteTeam(userID, teamID, teamName, teamLogo){
try {
const response = await fetch('/favteam/add', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
userID: userID,
teamID: teamID,
teamName: teamName,
teamLogo: teamLogo
})
});
if (!response.ok) {
throw new Error('Failed to add favorite team');
}
console.log('New favorite team added successfully.');
} catch (error) {
console.error('Error adding favorite team:', error);
}
}
async function removeFavoriteTeam(userID, teamID) {
try {
const response = await fetch('/favteam/remove', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
userID: userID,
teamID: teamID
})
});
console.log('Favorite team removed successfully.');
} catch (error) {
console.error('Error removing favorite team:', error);
}
}