From 473d30f39e9de0a4614746e3b063d18d8d7fc84a Mon Sep 17 00:00:00 2001 From: Henrik Askjer <henrik.askjer@uib.no> Date: Mon, 27 Sep 2021 15:08:54 +0200 Subject: [PATCH] Refactor select_result and replace navigate_to_word --- src/components/DictionaryView.vue | 131 ++++++++++++++---------------- 1 file changed, 59 insertions(+), 72 deletions(-) diff --git a/src/components/DictionaryView.vue b/src/components/DictionaryView.vue index a1bf103e..77e49ad1 100644 --- a/src/components/DictionaryView.vue +++ b/src/components/DictionaryView.vue @@ -162,79 +162,25 @@ function navigate_to_search(self, query) { }) } -function navigate_to_word(self, word) { - axios.get(self.api_pref + 'suggest?q=' + word, { headers: {"x-api-key":"ZkYiyRVXxH86ijsvhx3cH4SY5Iik2ijI3BKVJGMm"}}) // API-key to be removed or reimplemented with a ci-variable with the new API - .then(function(response){ - self.search_results = response.data.filter(result => result.match == word).sort(compare_by_hgno(word)) - if (! self.search_results.length) { - self.error = "Ordet «" + decodeURIComponent(word) + "» finnes ikke i ordbøkene" - } else { - self.error = null - } - }) - .catch(function(error){ - if (error.response) { - self.error = "Noe gikk galt på serversiden" - } else { - self.error = "Nettverksproblemer, prøv igjen" - } - }) - .then(function(_){ - self.waiting_for_articles = false - history.replaceState({article: self.article, search_results: self.search_results, lang: self.lang, error: self.error}, '') - }) -} -export default { - name: 'DictionaryView', - data: function() { - return { - article_key: 0, - search_results: [], - lang: 'bob,nob', - waiting_for_articles: true, - waiting_for_metadata: true, - article: null, - error: null, - monthly_bm: null, - monthly_nn: null, - event: null - } - }, - computed: { - waiting: function() { - return (this.waiting_for_articles || this.waiting_for_metadata) && this.$route.name != 'root' - }, - api_pref: function() { - return api_endpoint + '/' + this.lang + '/article/' - }, - get_oda_api: function() { - return oda_api - }, - }, - components: { - Article, - Autocomplete, - SearchResults - }, - methods: { - select_result: function (event) { - this.event = event - if (event.match) { - oda_api.get('articles?', {params: {lord: event.match, dict: this.lang}}).then((response) => { +function navigate_to_word(self, word) { + let event = self.event ? self.event : {q: word, + match: word, + source: self.$route.path} + console.log(event) + + // Get article IDs + oda_api.get('articles?', {params: {lord: event.match, dict: self.lang}}).then((response) => { event.article_ids = response.data - let source = '/' + this.lang + '/w/' + event.match - this.$router.push(source) let unwrapped = [] for (const d in event.article_ids) { event.article_ids[d].forEach(i => unwrapped.push({ dictionary: d, id: i })) - } - - let self = this + + // Get individual articles Promise.all(unwrapped.map((article) => { return axios.get(`${oda_dev}/${article.dictionary}/article/${article.id}.json`) @@ -242,7 +188,7 @@ export default { .then((response) => { self.search_results = response.map((element, index) => { return Object.assign(element.data, { - source: source, + source: event.source, match: event.match, dictionary: unwrapped[index].dictionary }) @@ -275,7 +221,50 @@ export default { error: self.error }, '') }) - })} else { + }) +} + +export default { + name: 'DictionaryView', + data: function() { + return { + article_key: 0, + search_results: [], + lang: 'bob,nob', + waiting_for_articles: true, + waiting_for_metadata: true, + article: null, + error: null, + monthly_bm: null, + monthly_nn: null, + event: null + } + }, + computed: { + waiting: function() { + return (this.waiting_for_articles || this.waiting_for_metadata) && this.$route.name != 'root' + }, + api_pref: function() { + return api_endpoint + '/' + this.lang + '/article/' + }, + get_oda_api: function() { + return oda_api + }, + }, + components: { + Article, + Autocomplete, + SearchResults + }, + methods: { + select_result: function (event) { + this.event = event + console.log(this.lang) + if (event.match) { + event.source = '/' + this.lang + '/w/' + event.match + this.$router.push(event.source) + navigate_to_word(this) + } else { this.waiting_for_articles = true this.article = null this.$router.push(`/${this.lang}/search/${event.q}`) @@ -290,12 +279,10 @@ export default { } }) }, - update_lang_form: function (event) { - this.lang = event - if (this.event && !this.article) { - this.event.articles = null - this.event.update_lang = true - this.select_result(this.event) + update_lang_form: function (lang) { + this.lang = lang + if (! this.article) { + navigate_to_word(this, this.$route.params.word) } }, article_link_click: function(item) { -- GitLab