From 23d97f00b168986b10c4372e69ee7cd5b87ff165 Mon Sep 17 00:00:00 2001 From: Kevin van der Werff Date: Wed, 6 Nov 2019 21:00:53 +0100 Subject: [PATCH] :sparkles: Add search page and show search results --- components/Search.vue | 35 ++++++++++------- pages/search.vue | 81 +++++++++++++++++++++++++++++++++++++++ resources/javascript.json | 6 --- 3 files changed, 102 insertions(+), 20 deletions(-) create mode 100644 pages/search.vue diff --git a/components/Search.vue b/components/Search.vue index 0ac6429..f2d8f49 100644 --- a/components/Search.vue +++ b/components/Search.vue @@ -10,25 +10,32 @@ export default { data() { return { searchInput: '', + searchPath: '/search', } }, + methods: { + // isTag :: String -> Bool + isTag: R.startsWith('#'), + + // removeFirstChar :: String -> String + removeFirstChar: R.compose( + R.join(''), + R.adjust(0, () => '') + ), + }, watch: { searchInput(input) { - const isTag = R.startsWith('#') - const removeFirstChar = R.compose( - R.join(''), - R.adjust(0, () => '') - ) - const words = R.filter(isNotEmpty, R.split(' ', input)) - const tags = R.filter(isTag, words) - const titles = R.filter(R.compose(R.not, isTag), words) - console.group() - console.log('words:', titles) - console.log('tags:', tags) - console.log('returned by words search:', this.$store.getters['data/findByName'](titles)) - console.log('returned by tags search:', this.$store.getters['data/findByTags'](R.map(removeFirstChar, tags))) - console.groupEnd() + const tags = R.filter(this.isTag, words) + const titles = R.filter(R.compose(R.not, this.isTag), words) + + const searchParams = new URLSearchParams() + if (isNotEmpty(titles)) + searchParams.append('keywords', titles) + if (isNotEmpty(tags)) + searchParams.append('tags', R.map(this.removeFirstChar, tags)) + + this.$router.push(this.searchPath + '?' + searchParams.toString()) }, }, } diff --git a/pages/search.vue b/pages/search.vue new file mode 100644 index 0000000..97d4a8e --- /dev/null +++ b/pages/search.vue @@ -0,0 +1,81 @@ + + + + + diff --git a/resources/javascript.json b/resources/javascript.json index f8eb981..048a72a 100644 --- a/resources/javascript.json +++ b/resources/javascript.json @@ -32,12 +32,6 @@ "url": "https://javascript.info/", "tags": ["tutorial", "explanations", "basics", "advanced"] }, - { - "title": "30 Seconds of Code", - "desc": "A curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.", - "url": "https://30secondsofcode.org", - "tags": ["resources", "educational", "short", "beginner"] - }, { "title": "JS Tips", "desc": "JS Tips is a collection of useful daily JavaScript tips that will allow you to improve your code writing.",