You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

124 lines
3.4 KiB

export function fetchChannelDetails(channelId, addAlert) {
return fetch(`/api/channels/${channelId}`)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.catch(error => {
addAlert('error', "Erreur lors de la récupération des détails de la chaîne");
throw error;
});
}
export async function fetchChannelStats(channelId, token, addAlert) {
try {
const request = await fetch(`/api/channels/${channelId}/stats`, {
method: "GET",
headers: {
"Authorization": `Bearer ${token}`
}
})
const result = await request.json();
return result;
} catch (error) {
console.error("Error fetching channel stats", error);
addAlert('error', "Erreur lors de la récupération des statistiques de la chaîne");
}
}
export async function updateChannel(channelId, data, token, addAlert) {
try {
const response = await fetch(`/api/channels/${channelId}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${token}`
},
body: JSON.stringify(data)
});
if (response.ok) {
addAlert('success', 'Chaîne mise à jour avec succès');
return response.json();
} else {
console.error("Failed to update channel");
const errorData = await response.json();
addAlert('error', errorData.message || 'Erreur lors de la mise à jour de la chaîne');
}
} catch (error) {
console.error("Error updating channel:", error);
addAlert('error', 'Erreur lors de la mise à jour de la chaîne');
}
}
export async function subscribe(channelId, addAlert) {
const token = localStorage.getItem('token');
const user = JSON.parse(localStorage.getItem('user'));
return fetch(`/api/channels/${channelId}/subscribe`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': "application/json"
},
body: JSON.stringify({
userId: user.id
})
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.catch(error => {
addAlert('error', "Erreur lors de l'abonnement à la chaîne");
throw error;
});
}
export async function createChannel(body, token, addAlert) {
const request = await fetch(`/api/channels/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
},
body: JSON.stringify(body)
})
if (!request.ok) {
console.error("Not able to create channel");
return;
}
addAlert('success', 'Chaîne créée avec succès');
const data = await request.json();
return data;
}
export async function deleteChannel(channelId, token, addAlert) {
try {
const response = await fetch(`/api/channels/${channelId}`, {
method: "DELETE",
headers: {
"Authorization": `Bearer ${token}`
}
});
if (response.ok) {
addAlert('success', 'Chaîne supprimée avec succès');
return true;
} else {
const errorData = await response.json();
addAlert('error', errorData.message || 'Erreur lors de la suppression de la chaîne');
return false;
}
} catch (error) {
console.error("Error deleting channel:", error);
addAlert('error', 'Erreur lors de la suppression de la chaîne');
return false;
}
}