From c130e9a1903c08a6dfa8d1bd1a343e85663ed170 Mon Sep 17 00:00:00 2001 From: Paul Derscheid Date: Fri, 25 Oct 2024 09:03:43 +0000 Subject: [PATCH] Bug 38222: (QA follow-up) Show cancellation reason where applicable, appropriately handle statuses MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Sponsored-by: Büchereizentrale Schleswig-Holstein Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- .../prog/en/modules/bookings/list.tt | 63 ++++++++++++---- .../intranet-tmpl/prog/js/tables/bookings.js | 72 ++++++++++++++----- 2 files changed, 102 insertions(+), 33 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/bookings/list.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/bookings/list.tt index 31155c6436..979ed7ac43 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/bookings/list.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/bookings/list.tt @@ -269,23 +269,56 @@ orderable: false, visible: false, render: function (data, type, row, meta) { - const is_expired = dayjs(row.end_date).isBefore(new Date()); - if (is_expired) { - return ` - ${_("Expired")} - `; - } + const isExpired = date => dayjs(date).isBefore(new Date()); + const isActive = (startDate, endDate) => { + const now = dayjs(); + return ( + now.isAfter(dayjs(startDate)) && + now.isBefore(dayjs(endDate).add(1, "day")) + ); + }; - const is_cancelled = row.status === "cancelled"; - if (is_cancelled) { - return ` - ${_("Cancelled")} - `; - } + const statusMap = { + new: () => { + if (isExpired(row.end_date)) { + return __("Expired"); + } + + if (isActive(row.start_date, row.end_date)) { + return __("Active"); + } + + if (dayjs(row.start_date).isAfter(new Date())) { + return __("Pending"); + } + + return __("New"); + }, + cancelled: () => + [__("Cancelled"), row.cancellation_reason] + .filter(Boolean) + .join(": "), + completed: () => __("Completed"), + }; + + const statusText = statusMap[row.status] + ? statusMap[row.status]() + : __("Unknown"); + + const classMap = [ + { status: __("Expired"), class: "bg-secondary" }, + { status: __("Cancelled"), class: "bg-secondary" }, + { status: __("Pending"), class: "bg-warning" }, + { status: __("Active"), class: "bg-primary" }, + { status: __("Completed"), class: "bg-info" }, + { status: __("New"), class: "bg-success" }, + ]; + + const badgeClass = + classMap.find(mapping => statusText.startsWith(mapping.status)) + ?.class || "bg-secondary"; - return ` - ${_("New")} - `; + return `${statusText}`; } }, { diff --git a/koha-tmpl/intranet-tmpl/prog/js/tables/bookings.js b/koha-tmpl/intranet-tmpl/prog/js/tables/bookings.js index a99695dc07..018ffdffb8 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/tables/bookings.js +++ b/koha-tmpl/intranet-tmpl/prog/js/tables/bookings.js @@ -45,24 +45,7 @@ $(document).ready(function () { name: "status", searchable: false, orderable: false, - render: function (data, type, row, meta) { - let is_expired = dayjs(row.end_date).isBefore( - new Date() - ); - if (is_expired) { - return ( - '' + - __("Expired") + - "" - ); - } - - return ( - '' + - __("Active") + - "" - ); - }, + render: renderStatus, }, { data: "biblio.title", @@ -140,6 +123,59 @@ $(document).ready(function () { } } + function renderStatus(data, type, row, meta) { + const isExpired = date => dayjs(date).isBefore(new Date()); + const isActive = (startDate, endDate) => { + const now = dayjs(); + return ( + now.isAfter(dayjs(startDate)) && + now.isBefore(dayjs(endDate).add(1, "day")) + ); + }; + + const statusMap = { + new: () => { + if (isExpired(row.end_date)) { + return __("Expired"); + } + + if (isActive(row.start_date, row.end_date)) { + return __("Active"); + } + + if (dayjs(row.start_date).isAfter(new Date())) { + return __("Pending"); + } + + return __("New"); + }, + cancelled: () => + [__("Cancelled"), row.cancellation_reason] + .filter(Boolean) + .join(": "), + completed: () => __("Completed"), + }; + + const statusText = statusMap[row.status] + ? statusMap[row.status]() + : __("Unknown"); + + const classMap = [ + { status: _("Expired"), class: "bg-secondary" }, + { status: _("Cancelled"), class: "bg-secondary" }, + { status: _("Pending"), class: "bg-warning" }, + { status: _("Active"), class: "bg-primary" }, + { status: _("Completed"), class: "bg-info" }, + { status: _("New"), class: "bg-success" }, + ]; + + const badgeClass = + classMap.find(mapping => statusText.startsWith(mapping.status)) + ?.class || "bg-secondary"; + + return `${statusText}`; + } + var txtActivefilter = __("Show expired"); var txtInactivefilter = __("Hide expired"); $("#expired_filter").on("click", function () { -- 2.39.5