From 3277f84a8d603025eaad9b7d29b793c2abd20d0a Mon Sep 17 00:00:00 2001 From: Kylart Date: Sat, 28 Oct 2017 03:24:25 +0200 Subject: [PATCH] Fixed problem on click search where the selected item was not the one given in the end --- components/infoModal.vue | 17 +++++++++-------- package.json | 2 +- server/index.js | 4 +++- server/search.js | 15 +++++++++++++-- store/modules/search/actions.js | 8 ++++---- test/server.test.js | 13 ++++++++++++- 6 files changed, 42 insertions(+), 17 deletions(-) diff --git a/components/infoModal.vue b/components/infoModal.vue index c13b97c..47d3b48 100644 --- a/components/infoModal.vue +++ b/components/infoModal.vue @@ -18,12 +18,11 @@ v-flex(xs12) v-layout(row, wrap, justify-center) template(v-if='results.length', v-for='item in results') - v-flex.elem(xs3, @click='search(item.name)') + v-flex.elem(xs3, @click='search(item)') v-layout.elem-content.elevation-3( wrap, justify-center, - v-ripple='true', - @click.all='search(item.name)' + v-ripple='true' ) v-flex(xs8) img.elem-picture(:src='item.image_url', height='140') @@ -50,16 +49,16 @@ this.searchTerm = '' this.$refs.input.focus() }, - async search (name) { - this.searchTerm = name + async search (item) { + this.searchTerm = item.name - if (this.$store.state.search.info.info.title === name) { + if (this.$store.state.search.info.info.title === item.name) { this.$store.commit('search/showInfo', true) this.searchShow = false } else { this.searchShow = false - this.$store.dispatch('search/fromName', name) + this.$store.dispatch('search/fromUrl', item) } }, quickSearch: _.debounce( @@ -68,7 +67,9 @@ if (term && term.length > 2) { try { - const {data, status} = await axios.get(`searchTermOnMal?term=${term}`) + const {data, status} = await axios.get(`searchTermOnMal`, { + params: {term} + }) if (status === 200) { this.results = data.categories[0].items diff --git a/package.json b/package.json index 0a9f1d0..849b08b 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "horrible-api": "^1.0.5", "lodash": "^4.17.4", "lru-cache": "^4.0.2", - "mal-scraper": "^1.4.1", + "mal-scraper": "^1.5.0", "moment": "^2.19.1", "nyaapi": "^1.2.2", "progress-bar-webpack-plugin": "^1.10.0", diff --git a/server/index.js b/server/index.js index 143c2f8..5fabb7b 100644 --- a/server/index.js +++ b/server/index.js @@ -223,7 +223,9 @@ let routes = [ }, (app) => { app.get('/getInfoFromMal', ({query}, res) => { - search.searchOnMal(query, res) + query.url + ? search.fromUrl(query, res) + : search.fromName(query, res) }) }, /* istanbul ignore next */ (app) => { diff --git a/server/search.js b/server/search.js index 727edae..d798a74 100644 --- a/server/search.js +++ b/server/search.js @@ -22,7 +22,7 @@ const searchTerm = (query, res) => { }) } -const searchOnMal = (query, res) => { +const fromName = (query, res) => { malScraper.getInfoFromName(query.term).then((data) => { res.type('application/json') res.status(200).send(JSON.stringify(data)) @@ -32,7 +32,18 @@ const searchOnMal = (query, res) => { }) } +const fromUrl = (query, res) => { + malScraper.getInfoFromUrl(query.url).then((data) => { + res.type('application/json') + res.status(200).send(JSON.stringify(data)) + }).catch(/* istanbul ignore next */(err) => { + console.log(err.message) + res.status(204).send() + }) +} + module.exports = { searchTerm, - searchOnMal + fromName, + fromUrl } diff --git a/store/modules/search/actions.js b/store/modules/search/actions.js index 6005551..789b97c 100644 --- a/store/modules/search/actions.js +++ b/store/modules/search/actions.js @@ -1,7 +1,9 @@ import {axios} from '../../utils' export default { - async fromName ({commit, state}, name) { + async fromUrl ({commit, state}, item) { + const {name, url} = item + if (name === state.info.term) { commit('showInfo', true) } else { @@ -9,9 +11,7 @@ export default { commit('setInfoLoading', true) commit('showInfo', true) const {data, status} = await axios.get(`getInfoFromMal`, { - params: { - term: name - } + params: {url} }) commit('setInfoLoading', false) diff --git a/test/server.test.js b/test/server.test.js index b08105e..79b1403 100644 --- a/test/server.test.js +++ b/test/server.test.js @@ -404,13 +404,24 @@ test('/searchTermOnMal route exits and return 10 elements', async t => { t.is(data.categories[0].items.length, 10) }) -test('/getInfoFromMal route exits and return an object with name', async t => { +test('/getInfoFromMal route exits if given name and return an object with name', async t => { const { data, status } = await axios.get(`${uri}/getInfoFromMal?term=sakura trick`) t.is(status, 200) t.is(data.title, 'Sakura Trick') }) +test('/getInfoFromMal route exits if given url and return an object with name', async t => { + const { data, status } = await axios.get(`${uri}/getInfoFromMal`, { + params: { + url: 'https://myanimelist.net/anime/20047/Sakura_Trick' + } + }) + + t.is(status, 200) + t.is(data.title, 'Sakura Trick') +}) + test('/_env route exits and return string containing platform\'s name', async t => { const { data, status } = await axios.get(`${uri}/_env`)