From 2b3d772a9002dd2a5087280d970558558c85408f Mon Sep 17 00:00:00 2001 From: cgfhtw Date: Tue, 17 Jun 2025 10:50:29 +0200 Subject: [PATCH] Both searches usable --- .../controllers/api/frontend/v1/Searchbar.php | 2 +- .../js/components/Stv/Studentenverwaltung.js | 4 +- public/js/components/searchbar/searchbar.js | 84 +++++++++++++++---- 3 files changed, 72 insertions(+), 18 deletions(-) diff --git a/application/controllers/api/frontend/v1/Searchbar.php b/application/controllers/api/frontend/v1/Searchbar.php index 8273e0cd9..d1444cbad 100644 --- a/application/controllers/api/frontend/v1/Searchbar.php +++ b/application/controllers/api/frontend/v1/Searchbar.php @@ -62,7 +62,7 @@ class Searchbar extends FHCAPI_Controller $result = $this->searchbarlib->search($this->input->post(self::SEARCHSTR_PARAM), $this->input->post(self::TYPES_PARAM)); if (property_exists($result, 'error')) $this->terminateWithError(getError($result), self::ERROR_TYPE_GENERAL); - $this->terminateWithSuccess($result); + $this->terminateWithSuccess($result->data); } /** diff --git a/public/js/components/Stv/Studentenverwaltung.js b/public/js/components/Stv/Studentenverwaltung.js index 352df5e9e..9146bc144 100644 --- a/public/js/components/Stv/Studentenverwaltung.js +++ b/public/js/components/Stv/Studentenverwaltung.js @@ -137,8 +137,8 @@ export default { reloadList() { this.$refs.stvList.reload(); }, - searchfunction(params) { - return this.$api.call(ApiSearchbar.search(params)); + searchfunction(params, config) { + return this.$api.call(ApiSearchbar.search(params), config); } }, created() { diff --git a/public/js/components/searchbar/searchbar.js b/public/js/components/searchbar/searchbar.js index 7b28c8bea..8e1711e90 100644 --- a/public/js/components/searchbar/searchbar.js +++ b/public/js/components/searchbar/searchbar.js @@ -19,7 +19,8 @@ export default { showresult: false, searching: false, error: null, - settingsDropdown:null, + abortController: null, + settingsDropdown:null, }; }, components: { @@ -201,27 +202,80 @@ export default { this.searchresult.splice(0, this.searchresult.length); this.searching = true; this.showsearchresult(); - if(this.searchsettings.types.length === 0) { - this.error = 'Kein Ergebnistyp ausgewählt. Bitte mindestens einen Ergebnistyp auswählen.'; - this.searching = false; - return; - } - this.searchfunction(this.searchsettings) + if(this.searchsettings.types.length === 0) { + this.error = 'Kein Ergebnistyp ausgewählt. Bitte mindestens einen Ergebnistyp auswählen.'; + this.searching = false; + return; + } + + if (this.abortController) + this.abortController.abort(); + this.abortController = new AbortController(); + + this.searchfunction(this.searchsettings, { timeout: 50000, signal: this.abortController.signal }) .then(response=>{ - if( response.data?.error === 1 ) { - this.error = 'Bei der Suche ist ein Fehler aufgetreten.'; + if (!response.data) { + this.error = this.$p.t('search/error_general'); } else { - for(let element of response.data.data){ - this.searchresult.push(element); + let res = response.data.map(el => el.data ? {...el, ...JSON.parse(el.data)} : el); + this.lastQuery = response.meta.searchstring; + if (this.searchoptions.mergeResults) { + let counter = 0; + let mergeTypes = []; + let mergedType = 'merged'; + let mergeKey = ''; + + switch (this.searchoptions.mergeResults) { + case 'student': + mergeTypes = ['student', 'prestudent']; + mergedType += this.searchoptions.mergeResults; + mergeKey = 'uid'; + break; + case 'person': + mergeTypes = ['person', 'employee', 'unassigned_employee', 'mitarbeiter', 'mitarbeiter_ohne_zuordnung', 'student', 'prestudent']; + mergedType += this.searchoptions.mergeResults; + mergeKey = 'person_id'; + break; + } + + if (mergeTypes.length) { + res = Object.values(res.reduce((a, c) => { + if (!mergeTypes.includes(c.type)) { + a['nomerge' + counter++] = c; + } else if (c[mergeKey] === null) { + a['nomerge' + counter++] = c; + } else if (a[c[mergeKey]] === undefined) { + a[c[mergeKey]] = { + rank: c.rank, + type: mergedType, + list: [c] + }; + } else { + a[c[mergeKey]].list.push(c); + if (c.rank > a[c[mergeKey]].rank) + a[c[mergeKey]].rank = c.rank; + } + return a; + }, {})).sort((a, b) => b.rank - a.rank); + } } + this.searchresult = res; } + this.searching = false; + this.retry = 0; }) .catch(error=> { - this.error = 'Bei der Suche ist ein Fehler aufgetreten.' - + ' ' + error.message; - }) - .finally(()=> { + if (error.code == "ERR_CANCELED") { + return this.retry = 0; + } + if (error.code == "ECONNABORTED" && this.retry) { + this.retry--; + return this.callsearchapi(); + } + + this.error = this.$p.t('search/error_general', error); this.searching = false; + this.retry = 0; }); }, refreshsearch: function() {