diff --git a/autopost/server.js b/autopost/server.js index a06d59b..3d05fed 100644 --- a/autopost/server.js +++ b/autopost/server.js @@ -505,28 +505,8 @@ autopostRouter.get('/', async (req, res) => { }); } - // Filtrage combiné avec recherche - var currentFilter = null; // null => pas de filtre actif - - function applyFilterAndSearch() { - var q = ($("#searchInput").val() || "").toLowerCase(); - $("table tbody tr").each(function() { - var $tr = $(this); - var name = $tr.find("td:first").text().toLowerCase(); - var status = parseInt($tr.data("status"), 10); - - var matchFilter = (currentFilter === null) || (status === currentFilter); - var matchSearch = !q || name.indexOf(q) !== -1; - - $tr.toggle(matchFilter && matchSearch); - }); - } - - $(document).ready(function(){ - - // Recherche AJAX - let searchTimer = null; - + $(document).ready(function(){ + let searchTimer = null; $("#searchInput").on("keyup", function() { clearTimeout(searchTimer); // annule le timer précédent @@ -540,10 +520,8 @@ autopostRouter.get('/', async (req, res) => { dataType: 'json', success: function(data) { updateTable(data); - if (typeof currentFilter === 'undefined') { - currentFilter = null; - } - applyFilterAndSearch(); + $(".filter-card").removeClass("ring-4 ring-white/40"); + toggleShowAllButton(); }, error: function(jqXHR, textStatus, errorThrown) { // On ignore l'erreur si la requête a été annulée volontairement @@ -558,29 +536,56 @@ autopostRouter.get('/', async (req, res) => { function toggleShowAllButton() { - if (currentFilter === null) { - $('#showAll').addClass('hidden'); - } else { - $('#showAll').removeClass('hidden'); - } + if ($(".filter-card.ring-4").length === 0) { + $('#showAll').addClass('hidden'); + } else { + $('#showAll').removeClass('hidden'); + } } + // Filtrage par clic sur une card - $(document).on("click", ".filter-card", function() { - currentFilter = parseInt($(this).data("status"), 10); + $(document).on("click", ".filter-card", function () { + const status = parseInt($(this).data("status"), 10); + $(".filter-card").removeClass("ring-4 ring-white/40"); $(this).addClass("ring-4 ring-white/40"); - applyFilterAndSearch(); - toggleShowAllButton(); + + $.ajax({ + url: '/autopost/filter', + type: 'GET', + data: { status }, + dataType: 'json', + success: function (data) { + updateTable(data); + toggleShowAllButton(); + }, + error: function () { + alert("Erreur lors du filtrage."); + } + }); }); - + + // Bouton tout afficher - $(document).on("click", "#showAll", function() { - currentFilter = null; + $(document).on("click", "#showAll", function () { $(".filter-card").removeClass("ring-4 ring-white/40"); - applyFilterAndSearch(); - toggleShowAllButton(); + + $.ajax({ + url: '/autopost/search', + type: 'GET', + data: { q: $("#searchInput").val() || "" }, + dataType: 'json', + success: function (data) { + updateTable(data); + toggleShowAllButton(); + }, + error: function () { + alert("Erreur lors du chargement des données."); + } + }); }); + // Edition $(document).on("click", ".edit-link", function(e) { @@ -985,6 +990,24 @@ autopostRouter.post('/delete/:id', async (req, res) => { } }); +// --------------------------- Fultrage ----------------------------- +autopostRouter.get('/filter', async (req, res) => { + const status = req.query.status; + if (status === undefined) { + return res.status(400).json({ error: "Status non fourni." }); + } + try { + const [rows] = await db.query( + `SELECT nom, status, id FROM \`${config.DB_TABLE}\` WHERE status = ? ORDER BY id DESC LIMIT 500`, + [status] + ); + res.json(rows); + } catch (err) { + console.error(err.message); + res.status(500).json({ error: "Erreur lors de la requête." }); + } +}); + // Redirection accès direct GET /edit/:id autopostRouter.get('/edit/:id', (req, res) => { res.redirect("/autopost/"); diff --git a/readme.md b/readme.md index 401f65e..f32192c 100644 --- a/readme.md +++ b/readme.md @@ -157,6 +157,16 @@ location /autopost/ { proxy_http_version 1.1; proxy_set_header X-Forwarded-Host $http_host; } +location /js/ { + proxy_pass http://127.0.0.1:XXXXX; + proxy_http_version 1.1; + proxy_set_header X-Forwarded-Host $http_host; +} +location /jquery/ { + proxy_pass http://127.0.0.1:XXXXX; + proxy_http_version 1.1; + proxy_set_header X-Forwarded-Host $http_host; +} ``` > Pensez à bien remplacer XXXXX par le port que vous avez mis dans votre config.js