Bug 32991: Improve our Dialog component
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Fri, 17 Feb 2023 10:26:38 +0000 (11:26 +0100)
committerTomas Cohen Arazi <tomascohen@theke.io>
Fri, 10 Mar 2023 14:15:25 +0000 (11:15 -0300)
This is picking some improvements made by Agustin on bug 32607.
This patch is only a POC and we should apply this change to the
different delete route.

We will then remove the *FormConfirmDelete components, and the /delete routes

Initially I wanted to have the same behaviour as in other areas of Koha,
and have a separate view for the deletion step. But it's too much
overhead for not much gain.

Additionally we will have to remove messages.js, the aim of this file
was to import the methods to add messages very easily (only 1 import
line). Now we will need 2 lines (it was more when I added messages.js,
because I didn't inject the store). Not a big deal.

Finally, there is something weird in Main.vue we need to fix. The
console is showing a warning
"[Vue warn]: setup() return property "_is_loading" should not start with "$" or "_" which are reserved prefixes for Vue internals."

I had a hard time to display this "loading" modal when the app is
loading all the things it needs. Pinia/store is not available as we are
accessing the methods/actions too earlier. It will be good to fix that
before we decide to move forward with this approach.

Signed-off-by: Matt Blenkinsop <matt.blenkinsop@ptfs-europe.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Pedro Amorim <pedro.amorim@ptfs-europe.com>
Signed-off-by: Agustín Moyano <agustinmoyano@theke.io>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
koha-tmpl/intranet-tmpl/prog/js/vue/components/Dialog.vue
koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsList.vue
koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/Main.vue
koha-tmpl/intranet-tmpl/prog/js/vue/stores/main.js

index f093261..995b9e6 100644 (file)
@@ -1,8 +1,16 @@
 <template>
-    <div class="dialog message" v-if="message">{{ message }}</div>
-    <div class="dialog alert" v-if="error">{{ error }}</div>
+    <div class="dialog message" v-if="message" v-html="message"></div>
+    <div class="dialog alert" v-if="error" v-html="error"></div>
     <div class="dialog alert modal" v-if="warning">
-        {{ warning }}
+        <span v-html="warning"></span>
+        <a
+            v-if="accept"
+            id="close_modal"
+            class="btn btn-primary btn-xs"
+            role="button"
+            @click="accept"
+            >{{ $__("Accept") }}</a
+        >
         <a
             id="close_modal"
             class="btn btn-default btn-xs"
@@ -27,13 +35,14 @@ import { storeToRefs } from "pinia"
 export default {
     setup() {
         const mainStore = inject("mainStore")
-        const { message, error, warning, is_submitting, is_loading } =
+        const { message, error, warning, accept, is_submitting, is_loading } =
             storeToRefs(mainStore)
         const { removeMessages } = mainStore
         return {
             message,
             error,
             warning,
+            accept,
             is_submitting,
             is_loading,
             removeMessages,
index b8d3840..ededc37 100644 (file)
@@ -57,6 +57,8 @@ export default {
         const AVStore = inject("AVStore")
         const { get_lib_from_av, map_av_dt_filter } = AVStore
 
+        const { setWarning, setMessage } = inject("mainStore")
+
         const table_id = "agreement_list"
         useDataTable(table_id)
 
@@ -66,6 +68,8 @@ export default {
             map_av_dt_filter,
             table_id,
             logged_in_user,
+            setWarning,
+            setMessage,
         }
     },
     data: function () {
@@ -121,14 +125,27 @@ export default {
             )
         },
         delete_agreement: function (agreement_id) {
-            this.$router.push(
-                "/cgi-bin/koha/erm/agreements/delete/" + agreement_id
-            )
+            this.setWarning(this.$__("Are you sure you want to remove this agreement?"), () => {
+                const client = APIClient.erm
+                client.agreements.delete(agreement_id).then(
+                    success => {
+                        this.setMessage(this.$__("Agreement deleted"))
+                        this.refresh_table()
+                    },
+                    error => {}
+                )
+            })
         },
         select_agreement: function (agreement_id) {
             this.$emit("select-agreement", agreement_id)
             this.$emit("close")
         },
+        refresh_table: function(){
+            $("#" + this.table_id)
+                .DataTable()
+                .ajax.url(this.datatable_url)
+                .draw()
+        },
         filter_table: async function () {
             if (this.before_route_entered) {
                 let new_route = build_url(
@@ -143,10 +160,7 @@ export default {
                         .toISOString()
                         .substring(0, 10)
             }
-            $("#" + this.table_id)
-                .DataTable()
-                .ajax.url(this.datatable_url)
-                .draw()
+            this.refresh_table()
         },
         table_url: function () {},
         build_datatable: function () {
index 8aa66d1..1f2ce3e 100644 (file)
@@ -1,5 +1,5 @@
 <template>
-    <div v-if="is_loading">
+    <div v-if="_is_loading">
         <Dialog />
     </div>
     <div v-else-if="ERMModule">
@@ -139,7 +139,7 @@ export default {
 
         // Note that we cannot use loading and loaded from messages
         // Pinia is not initiated yet there
-        const { is_loading } = storeToRefs(mainStore)
+        const { _is_loading } = storeToRefs(mainStore)
 
         return {
             vendorStore,
@@ -147,7 +147,7 @@ export default {
             mainStore,
             erm_providers,
             ERMModule,
-            is_loading,
+            _is_loading,
         }
     },
     data() {
@@ -156,7 +156,7 @@ export default {
         }
     },
     beforeCreate() {
-        this.mainStore.is_loading = true
+        this.mainStore._is_loading = true
 
         const acq_client = APIClient.acquisition
         acq_client.vendors.getAll().then(
@@ -200,7 +200,7 @@ export default {
                     }
                 )
             })
-            .then(() => (this.mainStore.is_loading = false))
+            .then(() => (this.mainStore._is_loading = false))
     },
     components: {
         Breadcrumb,
index b2b1b6c..3a79186 100644 (file)
@@ -2,51 +2,79 @@ import { defineStore } from "pinia";
 
 export const useMainStore = defineStore("main", {
     state: () => ({
-        message: null,
-        error: null,
-        warning: null,
+        _message: null,
+        _error: null,
+        _warning: null,
+        _accept: null,
         previousMessage: null,
         previousError: null,
         displayed_already: false,
-        is_submitting: false,
-        is_loading: false,
+        _is_submitting: false,
+        _is_loading: false,
     }),
     actions: {
-        setMessage(message) {
-            this.error = null;
-            this.warning = null;
-            this.message = message;
-            this.displayed_already = false; /* Will be displayed on the next view */
-        },
-        setError(error) {
-            this.error = error;
-            this.message = null;
-            this.displayed_already = true; /* Is displayed on the current view */
-        },
-        setWarning(warning) {
-            this.warning = warning;
-            this.message = null;
-            this.displayed_already = true; /* Is displayed on the current view */
+        setMessage(message, displayed = false) {
+            this._error = null;
+            this._warning = null;
+            this._message = message;
+            this.displayed_already = displayed; /* Will be displayed on the next view */
+        },
+        setError(error, displayed = true) {
+            this._error = error;
+            this._message = null;
+            this.displayed_already = displayed; /* Is displayed on the current view */
+        },
+        setWarning(warning, accept, displayed = true) {
+            if(accept) {
+                this._accept = async () => {
+                    await accept()
+                    this.removeMessages()
+                }
+            }
+            this._warning = warning;
+            this._message = null;
+            this.displayed_already = displayed; /* Is displayed on the current view */
         },
         removeMessages() {
             if (this.displayed_already) {
-                this.error = null;
-                this.warning = null;
-                this.message = null;
+                this._accept = null;
+                this._error = null;
+                this._warning = null;
+                this._message = null;
             }
             this.displayed_already = true;
         },
         submitting(){
-            this.is_submitting = true;
+            this._is_submitting = true;
         },
         submitted(){
-            this.is_submitting = false;
+            this._is_submitting = false;
         },
         loading(){
-            this.is_loading = true;
+            this._is_loading = true;
         },
         loaded(){
-            this.is_loading = false;
+            this._is_loading = false;
+        },
+    },
+    getters: {
+        error() {
+            return this._error
+        },
+        warning() {
+            return this._warning
+        },
+        message() {
+            return this._message
+        },
+        accept() {
+            return this._accept
+        },
+        is_submitting(){
+            return this._is_submitting
+        },
+        is_loading(){
+            return this._is_loading
         },
     },
 });