From 61a00feb60409b8f9b28cc6bb721dc1d0ae68bb2 Mon Sep 17 00:00:00 2001 From: Grzegorz Leoniec Date: Tue, 19 Feb 2019 12:30:02 +0100 Subject: [PATCH] Use VueX store for searching and better testabillity --- components/SearchInput.vue | 89 ++++++++++++++++++++------------------ layouts/default.vue | 38 ++++++---------- store/search.js | 65 ++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 67 deletions(-) create mode 100644 store/search.js diff --git a/components/SearchInput.vue b/components/SearchInput.vue index 6fa761b1e..1b59d9941 100644 --- a/components/SearchInput.vue +++ b/components/SearchInput.vue @@ -88,7 +88,7 @@ diff --git a/store/search.js b/store/search.js new file mode 100644 index 000000000..a14f2c878 --- /dev/null +++ b/store/search.js @@ -0,0 +1,65 @@ +import gql from 'graphql-tag' + +export const state = () => { + return { + quickResults: [], + quickPending: false + } +} + +export const mutations = { + SET_QUICK_RESULTS(state, results) { + state.quickResults = results || [] + }, + SET_QUICK_PENDING(state, pending) { + state.quickPending = pending + } +} + +export const getters = { + quickResults(state) { + return state.quickResults + }, + quickPending(state) { + return state.quickPending + } +} + +export const actions = { + async quickSearch({ commit, getters }, { value }) { + commit('SET_QUICK_PENDING', true) + await this.app.apolloProvider.defaultClient + .query({ + query: gql(` + query findPosts($filter: String!) { + findPosts(filter: $filter, limit: 10) { + id + slug + label: title + value: title, + shoutedCount + commentsCount + createdAt + author { + id + name + slug + } + } + } + `), + variables: { + filter: value + } + }) + .then(res => { + commit('SET_QUICK_RESULTS', res.data.findPosts || []) + commit('SET_QUICK_PENDING', false) + }) + return getters.quickResults + }, + async quickClear({ commit }) { + commit('SET_QUICK_PENDING', false) + commit('SET_QUICK_RESULTS', []) + } +}