export async function isSubscribed(channelId, addAlert) { const token = localStorage.getItem('token'); if (!token) { return; } const headers = { Authorization: `Bearer ${token}` }; const request = await fetch(`/api/users/${channelId}/channel/subscribed`, { headers }) const result = await request.json(); console.log("Subscription status for channel ID", channelId, ":", result); return result.subscribed; } export async function getChannel(userId, token, addAlert) { try { const response = await fetch(`/api/users/${userId}/channel`, { method: "GET", headers: { "Authorization": `Bearer ${token}`, } }); if (!response.ok) { throw new Error("Failed to fetch user data"); } const data = await response.json(); return data } catch (error) { console.error("Error fetching user channel:", error); addAlert('error', "Erreur lors de la récupération des données de l'utilisateur."); return null; } } export async function getUserHistory(userId, token, addAlert) { if (!userId || !token) { console.warn("User ID or token missing, skipping history fetch"); return; } try { const response = await fetch(`/api/users/${userId}/history`, { method: "GET", headers: { "Authorization": `Bearer ${token}`, } }); if (!response.ok) { throw new Error("Failed to fetch user history"); } const data = await response.json(); return data } catch (error) { console.error("Error fetching user history:", error); addAlert('error', "Erreur lors de la récupération de l'historique de l'utilisateur."); } } export async function getPlaylists(userId, token, addAlert) { if (!userId || !token) { console.warn("User ID or token missing, skipping playlists fetch"); return; } try { const response = await fetch(`/api/playlists/user/${userId}`, { method: "GET", headers: { "Authorization": `Bearer ${token}`, } }); if (!response.ok) { throw new Error("Failed to fetch user playlists"); } const data = await response.json(); return data; } catch (error) { console.error("Error fetching user playlists:", error); addAlert('error', "Erreur lors de la récupération des playlists de l'utilisateur."); } } export async function updateUser(userId, token, userData, addAlert) { try { const response = await fetch(`/api/users/${userId}`, { method: "PUT", headers: { "Authorization": `Bearer ${token}`, "Content-Type": "application/json" }, body: JSON.stringify(userData) }); if (!response.ok) { throw new Error("Failed to update user"); } const data = await response.json(); return data; } catch (error) { console.error("Error updating user:", error); addAlert('error', "Erreur lors de la mise à jour des données de l'utilisateur."); } } export async function verifyEmail(email, token, addAlert) { try { const response = await fetch('/api/users/verify-email', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, token }) }); if (!response.ok) { throw new Error("Failed to verify email"); } const data = await response.json(); return true; } catch (error) { console.error("Error verifying email:", error); addAlert('error', "Erreur lors de la vérification de l'email."); } } export async function searchByUsername(username, token, addAlert) { try { const response = await fetch(`/api/users/search?username=${encodeURIComponent(username)}`, { method: "GET", headers: { "Authorization": `Bearer ${token}`, } }); if (!response.ok) { throw new Error("Failed to search user"); } const data = await response.json(); return data; } catch (error) { console.error("Error searching user by username:", error); addAlert('error', "Erreur lors de la recherche de l'utilisateur."); } }