From 773db711e4672467dd1a721636724b82cdb66144 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Fri, 14 Apr 2023 11:09:33 +0200
Subject: [PATCH] Bug 33490: Improve max date handling

It fixes:
1. /agreements?by_expired=true do no longer explode
2. Fill date with today's date when we setup the component and when we
hit "filter"

However there are a couple of things totally wrong:
1. We should not need is_fp_disabled, we could be able to use
  :disabled="!filters.by_expired"
in the template, but it does not work
2. We should not need to deal with filters.max_expiration_date the "old
JS way", but should use a Vue computed value instead. I've failed at
that

I think those 2 problems have the same root, the way we deal with
filters in data() is wrong, and we may be loosing the ref at some point.
---
 .../js/vue/components/ERM/AgreementsList.vue  | 29 ++++++++++++++-----
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsList.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsList.vue
index d7c9f82a1ee..da59bb5743c 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsList.vue
+++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsList.vue
@@ -9,12 +9,14 @@
                 id="expired_filter"
                 v-model="filters.by_expired"
                 @keyup.enter="filter_table"
+                @change="updateMaxExpirationDate($event)"
             />
             {{ $__("on") }}
             <flat-pickr
                 id="max_expiration_date_filter"
-                v-model="filters.max_expiration_date"
+                v-model="this.filters.max_expiration_date"
                 :config="fp_config"
+                :disabled="is_fp_disabled"
             />
 
             <label for="by_mine_filter">{{ $__("Show mine only") }}:</label>
@@ -90,15 +92,17 @@ export default {
     },
     data: function () {
         this.filters = {
-            by_expired: this.$route.query.by_expired || false,
+            by_expired: this.$route.query.by_expired === "true" || false,
             max_expiration_date: this.$route.query.max_expiration_date || "",
             by_mine: this.$route.query.by_mine || false,
         }
         let filters = this.filters
+        this.updateMaxExpirationDate() // Set date to today if empty
 
         let logged_in_user = this.logged_in_user
         return {
             fp_config: flatpickr_defaults,
+            is_fp_disabled: !filters.by_expired,
             agreement_count: 0,
             initialized: false,
             tableOptions: {
@@ -214,12 +218,7 @@ export default {
             return url
         },
         filter_table: async function () {
-            if (this.filters.by_expired) {
-                if (!this.filters.max_expiration_date)
-                    this.filters.max_expiration_date = new Date()
-                        .toISOString()
-                        .substring(0, 10)
-            }
+            this.updateMaxExpirationDate()
             if (!this.embedded) {
                 let new_route = build_url(
                     "/cgi-bin/koha/erm/agreements",
@@ -229,6 +228,20 @@ export default {
             }
             this.$refs.table.redraw(this.table_url())
         },
+        updateMaxExpirationDate: function (event) {
+            if (event) {
+                this.is_fp_disabled = !event.target.checked
+            }
+            if (
+                this.filters.by_expired &&
+                (!this.filters.max_expiration_date ||
+                    this.filters.max_expiration_date === "")
+            ) {
+                this.filters.max_expiration_date = new Date()
+                    .toISOString()
+                    .substring(0, 10)
+            }
+        },
         getTableColumns: function () {
             let get_lib_from_av = this.get_lib_from_av
             let escape_str = this.escape_str
-- 
2.25.1