Skip to content

Commit d695529

Browse files
Merge pull request #223 from JLG-WOCFR-DEV/codex/enrichir-handlesuccessfulresponse-pour-message-vide
Handle empty link list state and mark view counts dirty
2 parents 90bd081 + 849682b commit d695529

File tree

3 files changed

+121
-3
lines changed

3 files changed

+121
-3
lines changed

liens-morts-detector-jlg/assets/js/__tests__/blc-admin-scripts.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,13 @@ describe('blc-admin-scripts modal interactions', () => {
211211
ajax.triggerSuccess({ success: true });
212212

213213
expect(modal.hasClass('is-open')).toBe(false);
214-
expect($('#the-list tr').length).toBe(0);
214+
const rows = $('#the-list tr');
215+
expect(rows.length).toBe(1);
216+
217+
const noItemsRow = rows.filter('.no-items');
218+
expect(noItemsRow.length).toBe(1);
219+
expect(noItemsRow.find('td').attr('colspan')).toBe('1');
220+
expect(noItemsRow.text()).toBe('Aucun élément à afficher.');
215221
expect(document.body.classList.contains('blc-modal-open')).toBe(false);
216222
});
217223

liens-morts-detector-jlg/assets/js/blc-admin-scripts.js

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ jQuery(document).ready(function($) {
1717
invalidUrlMessage: 'Veuillez saisir une URL valide.',
1818
sameUrlMessage: "La nouvelle URL doit être différente de l'URL actuelle.",
1919
genericError: 'Une erreur est survenue. Veuillez réessayer.',
20-
successAnnouncement: 'La ligne a été mise à jour avec succès.'
20+
successAnnouncement: 'La ligne a été mise à jour avec succès.',
21+
noItemsMessage: 'Aucun élément à afficher.'
2122
};
2223

2324
var messages = $.extend({}, defaultMessages, window.blcAdminMessages || {});
@@ -396,6 +397,38 @@ jQuery(document).ready(function($) {
396397
return $candidate.length ? $candidate[0] : null;
397398
}
398399

400+
function determineColumnCount($tbody, $row) {
401+
var columnCount = 0;
402+
var $normalizedTbody = $tbody && $tbody.jquery ? $tbody : $();
403+
var $table = $normalizedTbody.length ? $normalizedTbody.closest('table') : $();
404+
405+
if ($table.length) {
406+
var $headerCells = $table.find('thead tr:first').children('th:visible, td:visible');
407+
columnCount = $headerCells.length;
408+
409+
if (!columnCount) {
410+
$headerCells = $table.find('thead tr:first').children('th, td');
411+
columnCount = $headerCells.length;
412+
}
413+
}
414+
415+
var $normalizedRow = $row && $row.jquery ? $row : $();
416+
417+
if (!columnCount && $normalizedRow.length) {
418+
columnCount = $normalizedRow.children('td, th').length;
419+
}
420+
421+
if (!columnCount && $table.length) {
422+
columnCount = $table.find('tr').first().children('td, th').length;
423+
}
424+
425+
if (!columnCount) {
426+
columnCount = 1;
427+
}
428+
429+
return columnCount;
430+
}
431+
399432
function handleSuccessfulResponse(response, row, helpers) {
400433
var $row = row && row.jquery ? row : $(row);
401434

@@ -409,7 +442,50 @@ jQuery(document).ready(function($) {
409442

410443
if ($row && $row.length) {
411444
$row.fadeOut(300, function() {
412-
$(this).remove();
445+
var $currentRow = $(this);
446+
var $tbody = $currentRow.closest('tbody');
447+
$currentRow.remove();
448+
449+
if (!$tbody.length) {
450+
$tbody = $('#the-list');
451+
}
452+
453+
var $remainingRows = $tbody.children('tr').filter(function() {
454+
var $candidate = $(this);
455+
return !$candidate.hasClass('no-items') && !$candidate.hasClass('inline-edit-row');
456+
});
457+
458+
var messageRow = null;
459+
460+
if (!$remainingRows.length) {
461+
var messageText = messages.noItemsMessage || '';
462+
463+
if (messageText) {
464+
var colspan = determineColumnCount($tbody, $currentRow);
465+
var $existingNoItems = $tbody.children('tr.no-items');
466+
if ($existingNoItems.length) {
467+
$existingNoItems.remove();
468+
}
469+
470+
messageRow = $('<tr>', { class: 'no-items' });
471+
$('<td>', { colspan: colspan }).text(messageText).appendTo(messageRow);
472+
$tbody.append(messageRow);
473+
}
474+
}
475+
476+
$(document).trigger('blcAdmin:listUpdated', {
477+
response: response,
478+
tbody: $tbody,
479+
table: $tbody.closest('table'),
480+
messageRow: messageRow
481+
});
482+
});
483+
} else {
484+
$(document).trigger('blcAdmin:listUpdated', {
485+
response: response,
486+
tbody: $('#the-list'),
487+
table: $('#the-list').closest('table'),
488+
messageRow: null
413489
});
414490
}
415491
}

liens-morts-detector-jlg/liens-morts-detector-jlg.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,37 @@ function blc_load_textdomain() {
5858
require_once BLC_PLUGIN_PATH . 'includes/class-blc-links-list-table.php';
5959
require_once BLC_PLUGIN_PATH . 'includes/class-blc-images-list-table.php';
6060

61+
/**
62+
* Mark the cached view counters for broken links as stale so they can be recalculated.
63+
*
64+
* @return void
65+
*/
66+
function blc_mark_link_view_counts_dirty() {
67+
if (!function_exists('update_option')) {
68+
return;
69+
}
70+
71+
static $already_marked = false;
72+
73+
if ($already_marked) {
74+
return;
75+
}
76+
77+
$already_marked = true;
78+
$timestamp = time();
79+
80+
update_option('blc_links_view_counts_dirty', $timestamp, false);
81+
82+
/**
83+
* Fires after the broken link view counters have been flagged for refresh.
84+
*
85+
* @since 1.0.0
86+
*
87+
* @param int $timestamp Unix timestamp saved alongside the dirty flag.
88+
*/
89+
do_action('blc_links_view_counts_marked_dirty', $timestamp);
90+
}
91+
6192
// --- Hameçons (Hooks) d'Activation et de Désactivation ---
6293
// Ces fonctions s'exécutent uniquement à l'activation ou la désactivation du plugin.
6394
register_activation_hook(__FILE__, 'blc_activation');
@@ -143,6 +174,7 @@ function blc_enqueue_admin_assets($hook) {
143174
'sameUrlMessage' => __('La nouvelle URL doit être différente de l\'URL actuelle.', 'liens-morts-detector-jlg'),
144175
'genericError' => __('Une erreur est survenue. Veuillez réessayer.', 'liens-morts-detector-jlg'),
145176
'successAnnouncement' => __('Action effectuée avec succès. La ligne a été retirée de la liste.', 'liens-morts-detector-jlg'),
177+
'noItemsMessage' => __('Aucun lien cassé à afficher.', 'liens-morts-detector-jlg'),
146178
)
147179
);
148180
}
@@ -377,6 +409,7 @@ function blc_ajax_edit_link_callback() {
377409
blc_adjust_dataset_storage_footprint('link', -$row_cache_footprint);
378410
}
379411

412+
blc_mark_link_view_counts_dirty();
380413
wp_send_json_success(['purged' => true]);
381414
return;
382415
}
@@ -441,6 +474,7 @@ function blc_ajax_edit_link_callback() {
441474
blc_adjust_dataset_storage_footprint('link', -$row_cache_footprint);
442475
}
443476

477+
blc_mark_link_view_counts_dirty();
444478
wp_send_json_success();
445479
}
446480

@@ -516,6 +550,7 @@ function blc_ajax_unlink_callback() {
516550
blc_adjust_dataset_storage_footprint('link', -$row_cache_footprint);
517551
}
518552

553+
blc_mark_link_view_counts_dirty();
519554
wp_send_json_success(['purged' => true]);
520555
return;
521556
}
@@ -569,5 +604,6 @@ function blc_ajax_unlink_callback() {
569604
blc_adjust_dataset_storage_footprint('link', -$row_cache_footprint);
570605
}
571606

607+
blc_mark_link_view_counts_dirty();
572608
wp_send_json_success();
573609
}

0 commit comments

Comments
 (0)