2024-04-16 20:30:30 -06:00
|
|
|
document.addEventListener("DOMContentLoaded", function() {
|
|
|
|
|
var favoriteButton = document.getElementById("club-favorite-button");
|
|
|
|
|
if (favoriteButton) {
|
|
|
|
|
favoriteButton.addEventListener("click", function() {
|
2024-04-21 14:11:35 -06:00
|
|
|
var userID = document.getElementById("userID").value;
|
|
|
|
|
var teamID = document.getElementById("teamID").value;
|
|
|
|
|
var teamName = document.getElementById("teamName").value;
|
|
|
|
|
var teamLogo = document.getElementById("teamLogo").value;
|
|
|
|
|
|
2024-04-17 18:06:45 -06:00
|
|
|
if (favoriteButton.src.includes("/favorited.png")) {
|
2024-04-23 21:25:53 -06:00
|
|
|
removeFavoriteTeam(userID, teamID);
|
|
|
|
|
} else {
|
|
|
|
|
addFavoriteTeam(userID, teamID, teamName, teamLogo);
|
2024-04-17 18:06:45 -06:00
|
|
|
}
|
2024-04-16 20:30:30 -06:00
|
|
|
});
|
|
|
|
|
}
|
2024-04-21 23:03:51 -06:00
|
|
|
});
|
|
|
|
|
|
2024-04-21 14:11:35 -06:00
|
|
|
async function addFavoriteTeam(userID, teamID, teamName, teamLogo){
|
2024-04-17 18:06:45 -06:00
|
|
|
try {
|
2024-04-19 19:19:42 -06:00
|
|
|
const response = await fetch('/favteam/add', {
|
2024-04-17 18:06:45 -06:00
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
userID: userID,
|
|
|
|
|
teamID: teamID,
|
|
|
|
|
teamName: teamName,
|
|
|
|
|
teamLogo: teamLogo
|
|
|
|
|
})
|
|
|
|
|
});
|
2024-04-21 14:11:35 -06:00
|
|
|
|
2024-04-17 18:06:45 -06:00
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error('Failed to add favorite team');
|
|
|
|
|
}
|
2024-04-23 21:25:53 -06:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
2024-04-22 11:39:37 -06:00
|
|
|
|
2024-04-17 18:06:45 -06:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error adding favorite team:', error);
|
|
|
|
|
}
|
2024-04-19 19:19:42 -06:00
|
|
|
}
|
2024-04-23 21:25:53 -06:00
|
|
|
|
2024-04-19 19:19:42 -06:00
|
|
|
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
|
|
|
|
|
})
|
|
|
|
|
});
|
2024-04-23 21:25:53 -06:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
2024-04-22 11:39:37 -06:00
|
|
|
|
2024-04-19 19:19:42 -06:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error removing favorite team:', error);
|
|
|
|
|
}
|
|
|
|
|
}
|