Refactor message mark/delete listeners

This commit is contained in:
Anton Khorev 2023-08-11 19:57:56 +03:00
parent d0cfde2996
commit 179c70f725

View file

@ -1,30 +1,32 @@
$(document).ready(function () { $(document).ready(function () {
$(".inbox-mark-unread").on("ajax:success", function (event, data) { $(".inbox-mark-unread").on("ajax:success", function (event, data) {
$("#inboxanchor").remove(); updateHtml(data);
$(".user-button").before(data.inboxanchor); updateReadState(this, false);
$("#inbox-count").replaceWith(data.inbox_count);
$(this).parents(".inbox-row").removeClass("inbox-row").addClass("inbox-row-unread");
}); });
$(".inbox-mark-read").on("ajax:success", function (event, data) { $(".inbox-mark-read").on("ajax:success", function (event, data) {
$("#inboxanchor").remove(); updateHtml(data);
$(".user-button").before(data.inboxanchor); updateReadState(this, true);
$("#inbox-count").replaceWith(data.inbox_count);
$(this).parents(".inbox-row-unread").removeClass("inbox-row-unread").addClass("inbox-row");
}); });
$(".inbox-destroy").on("ajax:success", function (event, data) { $(".inbox-destroy").on("ajax:success", function (event, data) {
updateHtml(data);
$(this).closest("tr").fadeOut(800, "linear", function () {
$(this).remove();
});
});
function updateHtml(data) {
$("#inboxanchor").remove(); $("#inboxanchor").remove();
$(".user-button").before(data.inboxanchor); $(".user-button").before(data.inboxanchor);
$("#inbox-count").replaceWith(data.inbox_count); $("#inbox-count").replaceWith(data.inbox_count);
}
$(this).parents(".inbox-row, .inbox-row-unread").fadeOut(800, "linear", function () { function updateReadState(target, isRead) {
$(this).remove(); $(target).closest("tr")
}); .toggleClass("inbox-row", isRead)
}); .toggleClass("inbox-row-unread", !isRead);
}
}); });