From f976fb6cb0c36a737418f6c6c939f9b6f68066f7 Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Thu, 16 Jan 2025 15:42:56 +0000 Subject: [PATCH] Bug 37930: Move AV code to a dedicated file --- .../vue/components/ERM/AgreementsFormAdd.vue | 4 +- .../js/vue/components/ERM/AgreementsList.vue | 9 +-- .../prog/js/vue/components/ERM/Main.vue | 40 +++++------ .../js/vue/composables/authorisedValues.js | 50 ++++++++++++++ .../prog/js/vue/stores/authorised-values.js | 67 ------------------- .../intranet-tmpl/prog/js/vue/stores/erm.js | 14 ++++ .../intranet-tmpl/prog/js/vue/stores/main.js | 55 --------------- 7 files changed, 87 insertions(+), 152 deletions(-) create mode 100644 koha-tmpl/intranet-tmpl/prog/js/vue/composables/authorisedValues.js delete mode 100644 koha-tmpl/intranet-tmpl/prog/js/vue/stores/authorised-values.js diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsFormAdd.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsFormAdd.vue index 39876a5deab..690893f79b1 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsFormAdd.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsFormAdd.vue @@ -200,8 +200,8 @@ import { storeToRefs } from "pinia"; export default { setup() { - const mainStore = inject("mainStore"); - const { authorisedValues } = storeToRefs(mainStore); + const ERMStore = inject("ERMStore"); + const { authorisedValues } = storeToRefs(ERMStore); return { authorisedValues, 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 73cf9f350c8..5b150a94f3c 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 @@ -72,12 +72,9 @@ export default { const vendorStore = inject("vendorStore"); const { vendors } = storeToRefs(vendorStore); - const { - setConfirmationDialog, - setMessage, - get_lib_from_av, - map_av_dt_filter, - } = inject("mainStore"); + const { setConfirmationDialog, setMessage } = inject("mainStore"); + + const { get_lib_from_av, map_av_dt_filter } = inject("ERMStore"); const table = ref(); diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/Main.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/Main.vue index 80a8c4d9f61..f2a7d0b75c7 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/Main.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/Main.vue @@ -43,11 +43,12 @@ export default { const mainStore = inject("mainStore"); - const { loading, loaded, setError, loadAuthorisedValues } = mainStore; + const { loading, loaded, setError } = mainStore; const ERMStore = inject("ERMStore"); const { config, authorisedValues } = storeToRefs(ERMStore); + const { loadAuthorisedValues } = ERMStore; return { vendorStore, @@ -70,8 +71,6 @@ export default { this.loading(); const fetch_config = () => { - let promises = []; - const acq_client = APIClient.acquisition; acq_client.vendors.getAll().then( vendors => { @@ -79,32 +78,29 @@ export default { }, error => {} ); - this.loadAuthorisedValues(this.authorisedValues).then(() => { + this.loadAuthorisedValues( + this.authorisedValues, + this.ERMStore + ).then(() => { this.loaded(); this.initialized = true; }); }; const client = APIClient.erm; - client.config - .get() - .then(config => { - this.config = config; - if (this.config.settings.ERMModule != 1) { - this.loaded(); - return this.setError( - this.$__( - "The e-resource management module is disabled, turn on ERMModule to use it" - ), - false - ); - } - return fetch_config(); - }) - .then(() => { + client.config.get().then(config => { + this.config = config; + if (this.config.settings.ERMModule != 1) { this.loaded(); - this.initialized = true; - }); + return this.setError( + this.$__( + 'The e-resource management module is disabled, turn on ERMModule to use it' + ), + false + ); + } + return fetch_config(); + }); }, methods: { async filterProviders(navigationTree) { diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/composables/authorisedValues.js b/koha-tmpl/intranet-tmpl/prog/js/vue/composables/authorisedValues.js new file mode 100644 index 00000000000..3bfdb9ac305 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/composables/authorisedValues.js @@ -0,0 +1,50 @@ +import { APIClient } from "../fetch/api-client.js"; + +export const get_lib_from_av_handler = (arr_name, av, store) => { + if (store.authorisedValues[arr_name] === undefined) { + console.warn( + "The authorised value category for '%s' is not defined.".format( + arr_name + ) + ); + return; + } + let o = store.authorisedValues[arr_name].find(e => e.value == av); + return o ? o.description : av; +}; +export const map_av_dt_filter_handler = (arr_name, store) => { + return store.authorisedValues[arr_name].map(e => { + e["_id"] = e["value"]; + e["_str"] = e["description"]; + return e; + }); +}; +export const loadAuthorisedValues = async (authorisedValues, targetStore) => { + const AVsToFetch = Object.keys(authorisedValues).reduce((acc, avKey) => { + if (Array.isArray(authorisedValues[avKey])) return acc; + acc[avKey] = authorisedValues[avKey]; + return acc; + }, {}); + + const AVCatArray = Object.keys(AVsToFetch).map(avCat => { + return '"' + AVsToFetch[avCat] + '"'; + }); + + const promises = []; + const AVClient = APIClient.authorised_values; + promises.push( + AVClient.values + .getCategoriesWithValues(AVCatArray) + .then(AVCategories => { + Object.entries(AVsToFetch).forEach(([AVName, AVCat]) => { + const AVMatch = AVCategories.find( + element => element.category_name == AVCat + ); + targetStore.authorisedValues[AVName] = + AVMatch.authorised_values; + }); + }) + ); + + return Promise.all(promises); +}; diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/stores/authorised-values.js b/koha-tmpl/intranet-tmpl/prog/js/vue/stores/authorised-values.js deleted file mode 100644 index ef77373d689..00000000000 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/stores/authorised-values.js +++ /dev/null @@ -1,67 +0,0 @@ -import { defineStore } from "pinia"; - -export const useAVStore = defineStore("authorised_values", { - state: () => ({ - av_agreement_statuses: [], - av_agreement_closure_reasons: [], - av_agreement_renewal_priorities: [], - av_user_roles: [], - av_license_types: [], - av_license_statuses: [], - av_agreement_license_statuses: [], - av_agreement_license_location: [], - av_agreement_relationships: [ - { value: "supersedes", description: __("supersedes") }, - { value: "is-superseded-by", description: __("is superseded by") }, - { - value: "provides_post-cancellation_access_for", - description: __("provides post-cancellation access for"), - }, - { - value: "has-post-cancellation-access-in", - description: __("has post-cancellation access in"), - }, - { - value: "tracks_demand-driven_acquisitions_for", - description: __("tracks demand-driven acquisitions for"), - }, - { - value: "has-demand-driven-acquisitions-in", - description: __("has demand-driven acquisitions in"), - }, - { value: "has_backfile_in", description: __("has backfile in") }, - { value: "has_frontfile_in", description: __("has frontfile in") }, - { value: "related_to", description: __("related to") }, - ], - av_package_types: [], - av_package_content_types: [], - av_title_publication_types: [], - av_notforloan: [], - av_report_types: [], - av_platform_reports_metrics: [], - av_database_reports_metrics: [], - av_title_reports_metrics: [], - av_item_reports_metrics: [], - }), - actions: { - get_lib_from_av(arr_name, av) { - if (this[arr_name] === undefined) { - console.warn( - "The authorised value category for '%s' is not defined.".format( - arr_name - ) - ); - return; - } - let o = this[arr_name].find(e => e.value == av); - return o ? o.description : av; - }, - map_av_dt_filter(arr_name) { - return this[arr_name].map(e => { - e["_id"] = e["value"]; - e["_str"] = e["description"]; - return e; - }); - }, - }, -}); diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/stores/erm.js b/koha-tmpl/intranet-tmpl/prog/js/vue/stores/erm.js index 6e8b13074d0..244501ea158 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/stores/erm.js +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/stores/erm.js @@ -1,4 +1,9 @@ import { defineStore } from "pinia"; +import { + loadAuthorisedValues, + get_lib_from_av_handler, + map_av_dt_filter_handler, +} from "../composables/authorisedValues"; export const useERMStore = defineStore("erm", { state: () => ({ @@ -59,4 +64,13 @@ export const useERMStore = defineStore("erm", { ], }, }), + actions: { + loadAuthorisedValues, + get_lib_from_av(arr_name, av) { + return get_lib_from_av_handler(arr_name, av, this); + }, + map_av_dt_filter(arr_name) { + return map_av_dt_filter_handler(arr_name, this); + }, + }, }); diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/stores/main.js b/koha-tmpl/intranet-tmpl/prog/js/vue/stores/main.js index 97daf68301f..7a0c9e65166 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/stores/main.js +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/stores/main.js @@ -1,5 +1,4 @@ import { defineStore } from "pinia"; -import { APIClient } from "../fetch/api-client.js"; export const useMainStore = defineStore("main", { state: () => ({ @@ -13,7 +12,6 @@ export const useMainStore = defineStore("main", { displayed_already: false, _is_submitting: false, _is_loading: false, - authorisedValues: {}, }), actions: { setMessage(message, displayed = false) { @@ -92,59 +90,6 @@ export const useMainStore = defineStore("main", { loaded() { this._is_loading = false; }, - get_lib_from_av(arr_name, av) { - if (this.authorisedValues[arr_name] === undefined) { - console.warn( - "The authorised value category for '%s' is not defined.".format( - arr_name - ) - ); - return; - } - let o = this.authorisedValues[arr_name].find(e => e.value == av); - return o ? o.description : av; - }, - map_av_dt_filter(arr_name) { - return this.authorisedValues[arr_name].map(e => { - e["_id"] = e["value"]; - e["_str"] = e["description"]; - return e; - }); - }, - async loadAuthorisedValues(authorisedValues) { - const AVsToFetch = Object.keys(authorisedValues).reduce( - (acc, avKey) => { - if (Array.isArray(authorisedValues[avKey])) return acc; - acc[avKey] = authorisedValues[avKey]; - return acc; - }, - {} - ); - - const AVCatArray = Object.keys(AVsToFetch).map(avCat => { - return '"' + AVsToFetch[avCat] + '"'; - }); - - const promises = []; - const AVClient = APIClient.authorised_values; - promises.push( - AVClient.values - .getCategoriesWithValues(AVCatArray) - .then(AVCategories => { - Object.entries(AVsToFetch).forEach( - ([AVName, AVCat]) => { - const AVMatch = AVCategories.find( - element => element.category_name == AVCat - ); - this.authorisedValues[AVName] = - AVMatch.authorised_values; - } - ); - }) - ); - - return Promise.all(promises); - }, }, getters: { error() { -- 2.48.1