fix: load more button
This commit is contained in:
+10
-90
@@ -1,97 +1,17 @@
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
// Gestion des clics sur les vidéos
|
||||
// Gestion des clics sur les vidéos initiales de la page catégorie
|
||||
// Note: Le bouton "Voir Plus" est géré par main.js via data-category-id
|
||||
const videoCards = document.querySelectorAll(".video-card");
|
||||
|
||||
for (const videoCard of videoCards) {
|
||||
videoCard.addEventListener("click", function () {
|
||||
const videoId = this.dataset.videoId;
|
||||
if (videoId) {
|
||||
window.location.href = `video.php?id=${videoId}`;
|
||||
}
|
||||
});
|
||||
}
|
||||
// Gestion du bouton "Voir plus"
|
||||
const viewMoreBtn = document.querySelector(".view-more");
|
||||
if (viewMoreBtn) {
|
||||
viewMoreBtn.addEventListener("click", function () {
|
||||
const page = Number.parseInt(this.dataset.page);
|
||||
const categoryId =
|
||||
document.querySelector(".video-section").dataset.categoryId;
|
||||
|
||||
// Changer le texte du bouton pendant le chargement
|
||||
this.textContent = "Chargement...";
|
||||
this.disabled = true;
|
||||
|
||||
// Préparer les données avec token CSRF
|
||||
const formData = new FormData();
|
||||
formData.append('csrf_token', document.querySelector('meta[name="csrf-token"]').getAttribute('content'));
|
||||
|
||||
// Faire la requête AJAX
|
||||
fetch(
|
||||
`ajax/load-more-videos.php?type=category&page=${page}&category=${categoryId}`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"X-Requested-With": "XMLHttpRequest",
|
||||
},
|
||||
body: formData
|
||||
if (!videoCard.hasAttribute("data-click-initialized")) {
|
||||
videoCard.setAttribute("data-click-initialized", "true");
|
||||
videoCard.addEventListener("click", function () {
|
||||
const videoId = this.dataset.videoId;
|
||||
if (videoId) {
|
||||
window.location.href = `video.php?id=${videoId}`;
|
||||
}
|
||||
)
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
if (data.success) {
|
||||
// Ajouter les nouvelles vidéos à la grille
|
||||
const videoGrid = document.querySelector(".video-grid");
|
||||
const tempDiv = document.createElement("div");
|
||||
tempDiv.innerHTML = data.html;
|
||||
|
||||
// Ajouter chaque vidéo à la grille
|
||||
while (tempDiv.firstChild) {
|
||||
videoGrid.appendChild(tempDiv.firstChild);
|
||||
}
|
||||
|
||||
// Mettre à jour le numéro de page
|
||||
this.dataset.page = data.page + 1;
|
||||
|
||||
// Réinitialiser le texte du bouton
|
||||
this.textContent = "Voir plus";
|
||||
this.disabled = false;
|
||||
|
||||
// Si plus de vidéos à charger, masquer le bouton
|
||||
if (!data.hasMore) {
|
||||
this.style.display = "none";
|
||||
}
|
||||
|
||||
// Initialiser les clics sur les nouvelles vidéos
|
||||
const cards = document.querySelectorAll(".video-card:not([data-click-initialized])")
|
||||
|
||||
for (const card of cards) {
|
||||
card.setAttribute("data-click-initialized", "true");
|
||||
card.addEventListener("click", function () {
|
||||
const videoId = this.dataset.videoId;
|
||||
if (videoId) {
|
||||
window.location.href = `video.php?id=${videoId}`;
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
} else {
|
||||
// Gérer l'erreur
|
||||
this.textContent = "Erreur lors du chargement";
|
||||
setTimeout(() => {
|
||||
this.textContent = "Voir plus";
|
||||
this.disabled = false;
|
||||
}, 2000);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Erreur:", error);
|
||||
this.textContent = "Erreur lors du chargement";
|
||||
setTimeout(() => {
|
||||
this.textContent = "Voir plus";
|
||||
this.disabled = false;
|
||||
}, 2000);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
+22
-33
@@ -320,51 +320,42 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
viewMoreButtons.forEach(button => {
|
||||
// Déterminer le type de vidéos à charger
|
||||
const section = button.closest('.video-section');
|
||||
const sectionTitle = section.querySelector('.section-title').textContent.trim().toLowerCase();
|
||||
const videoGrid = section.querySelector('.video-grid');
|
||||
let videoType = '';
|
||||
let categoryId = null;
|
||||
|
||||
if (sectionTitle.includes('dernières')) {
|
||||
videoType = 'recent';
|
||||
} else if (sectionTitle.includes('tendances')) {
|
||||
videoType = 'trending';
|
||||
} else if (sectionTitle.includes('indépendance')) {
|
||||
videoType = 'independence';
|
||||
} else {
|
||||
// Vérifier si c'est une section de catégorie
|
||||
const categorySection = section.querySelector('[data-category-id]');
|
||||
if (categorySection) {
|
||||
videoType = 'category';
|
||||
categoryId = categorySection.dataset.categoryId;
|
||||
} else if (section.hasAttribute('data-category-id')) {
|
||||
videoType = 'category';
|
||||
categoryId = section.dataset.categoryId;
|
||||
}
|
||||
|
||||
// Utiliser l'attribut data-video-type si présent (méthode prioritaire)
|
||||
if (section.hasAttribute('data-video-type')) {
|
||||
videoType = section.dataset.videoType;
|
||||
} else if (section.hasAttribute('data-category-id')) {
|
||||
// Section de catégorie
|
||||
videoType = 'category';
|
||||
categoryId = section.dataset.categoryId;
|
||||
}
|
||||
|
||||
|
||||
// Si aucun type reconnu, ne pas configurer l'événement
|
||||
if (!videoType) return;
|
||||
|
||||
// Stocker le numéro de page actuel
|
||||
button.dataset.page = '1';
|
||||
|
||||
|
||||
button.addEventListener('click', function() {
|
||||
const page = parseInt(this.dataset.page);
|
||||
|
||||
// Compter les vidéos déjà affichées pour calculer l'offset
|
||||
const currentCount = videoGrid.querySelectorAll('.video-card').length;
|
||||
|
||||
// Changer le texte du bouton pendant le chargement
|
||||
button.textContent = 'Chargement...';
|
||||
button.disabled = true;
|
||||
|
||||
// Préparer l'URL avec les paramètres
|
||||
let url = `ajax/load-more-videos.php?type=${videoType}&page=${page}`;
|
||||
|
||||
// Préparer l'URL avec les paramètres (utiliser offset au lieu de page)
|
||||
let url = `ajax/load-more-videos?type=${videoType}&offset=${currentCount}`;
|
||||
if (videoType === 'category' && categoryId) {
|
||||
url += `&category=${categoryId}`;
|
||||
}
|
||||
|
||||
// Préparer les données avec token CSRF
|
||||
const formData = new FormData();
|
||||
formData.append('csrf_token', document.querySelector('meta[name="csrf-token"]').getAttribute('content'));
|
||||
const csrfMeta = document.querySelector('meta[name="csrf-token"]');
|
||||
if (csrfMeta) {
|
||||
formData.append('csrf_token', csrfMeta.getAttribute('content'));
|
||||
}
|
||||
|
||||
// Faire la requête AJAX
|
||||
fetch(url, {
|
||||
@@ -372,7 +363,8 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
headers: {
|
||||
'X-Requested-With': 'XMLHttpRequest'
|
||||
},
|
||||
body: formData
|
||||
body: formData,
|
||||
credentials: 'same-origin'
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
@@ -386,9 +378,6 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
videoGrid.appendChild(tempDiv.firstChild);
|
||||
}
|
||||
|
||||
// Mettre à jour le numéro de page
|
||||
this.dataset.page = data.page + 1;
|
||||
|
||||
// Réinitialiser le texte du bouton
|
||||
button.textContent = 'Voir plus';
|
||||
button.disabled = false;
|
||||
|
||||
Reference in New Issue
Block a user