From: Anton Khorev Date: Fri, 11 Aug 2023 16:57:56 +0000 (+0300) Subject: Refactor message mark/delete listeners X-Git-Tag: live~1094^2~4 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/179c70f725848b0d0fc292da0ff2de7325551990 Refactor message mark/delete listeners --- diff --git a/app/assets/javascripts/messages.js b/app/assets/javascripts/messages.js index 0cae79a58..8c30f9b34 100644 --- a/app/assets/javascripts/messages.js +++ b/app/assets/javascripts/messages.js @@ -1,30 +1,32 @@ $(document).ready(function () { $(".inbox-mark-unread").on("ajax:success", function (event, data) { - $("#inboxanchor").remove(); - $(".user-button").before(data.inboxanchor); - - $("#inbox-count").replaceWith(data.inbox_count); - - $(this).parents(".inbox-row").removeClass("inbox-row").addClass("inbox-row-unread"); + updateHtml(data); + updateReadState(this, false); }); $(".inbox-mark-read").on("ajax:success", function (event, data) { - $("#inboxanchor").remove(); - $(".user-button").before(data.inboxanchor); + updateHtml(data); + updateReadState(this, true); + }); - $("#inbox-count").replaceWith(data.inbox_count); + $(".inbox-destroy").on("ajax:success", function (event, data) { + updateHtml(data); - $(this).parents(".inbox-row-unread").removeClass("inbox-row-unread").addClass("inbox-row"); + $(this).closest("tr").fadeOut(800, "linear", function () { + $(this).remove(); + }); }); - $(".inbox-destroy").on("ajax:success", function (event, data) { + function updateHtml(data) { $("#inboxanchor").remove(); $(".user-button").before(data.inboxanchor); $("#inbox-count").replaceWith(data.inbox_count); + } - $(this).parents(".inbox-row, .inbox-row-unread").fadeOut(800, "linear", function () { - $(this).remove(); - }); - }); + function updateReadState(target, isRead) { + $(target).closest("tr") + .toggleClass("inbox-row", isRead) + .toggleClass("inbox-row-unread", !isRead); + } });