From 833329da5a0dbea329dd9023b640e7c7b3ca66ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Volds=C3=A6ter?= <ole.voldsater@uib.no> Date: Wed, 30 Sep 2020 11:11:52 +0200 Subject: [PATCH] =?UTF-8?q?refaktorering=20av=20API-sp=C3=B8rringer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.vue | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/App.vue b/src/App.vue index 2e7c1f0..6dfc470 100644 --- a/src/App.vue +++ b/src/App.vue @@ -68,8 +68,11 @@ axios.interceptors.request.use(function (config) { window.onpopstate = function (event) { if (event.state) { + // eslint-disable-next-line app.__vue__._data.article = event.state.article + // eslint-disable-next-line app.__vue__._data.search_results = event.state.search_results + // eslint-disable-next-line app.__vue__._data.lang = event.state.lang } } @@ -86,11 +89,14 @@ export default { } }, computed: { + api_pref: function() { + return api_endpoint + '/' + this.lang + '/article/' + }, search: function() { let self = this return function(q) { return new Promise(resolve => { - return axios.get(api_endpoint + '/' + self.lang + '/article/suggest?q=' + q).then( + return axios.get(self.api_pref + 'suggest?q=' + q).then( function(response) { let hits = q.length ? [{q: q}] : [] hits = hits.concat(response.data.suggest.suggest[0].options.sort((o1, o2) => o1.text.length - o2.text.length)) @@ -119,7 +125,7 @@ export default { var self = this self.waiting = true self.article = {lemmas: [], body:{pronunciation: [], definitions: [], etymology: []}} - axios.get(api_endpoint + '/' + self.lang + '/article/search?q=' + event.q + ' ' + event.q + '*&size=20') + axios.get(self.api_pref + 'search?q=' + event.q + ' ' + event.q + '*&size=20') .then(function(response){ self.$router.push('/' + `/search?q=${event.q}&lang=${self.lang}`) self.search_results = response.data.hits.hits @@ -136,7 +142,7 @@ export default { }else{ self.article = {lemmas: [], body:{pronunciation: [], definitions: [], etymology: []}} self.waiting = true - axios.get(api_endpoint + '/' + self.lang + '/article/' + item.article_id) + axios.get(self.api_pref + '' + item.article_id) .then(function(response){ self.article = response.data._source self.waiting = false @@ -154,7 +160,7 @@ export default { this.lang = this.$route.params.lang || this.$route.query.lang || 'bob,nob' var self = this; if(this.$route.query.q) { - axios.get(api_endpoint + '/' + self.lang + '/article/search?q=' + self.$route.query.q + ' ' + self.$route.query.q + '*&size=20') + axios.get(self.api_pref + 'search?q=' + self.$route.query.q + ' ' + self.$route.query.q + '*&size=20') .then(function(response){ self.search_results = response.data.hits.hits self.waiting = false @@ -162,7 +168,7 @@ export default { }) } else if(this.$route.params.id){ - axios.get(api_endpoint + '/' + self.lang + '/article/' + self.$route.params.id) + axios.get(self.api_pref + '' + self.$route.params.id) .then(function(response){ self.article = response.data._source self.waiting = false @@ -175,7 +181,7 @@ export default { } }, watch: { - $route(to, from) { + $route() { this.lang = this.$route.params.lang || this.$route.query.lang || 'bob,nob' } } -- GitLab