Matt Rider 6de602fd58 Revert @roschaefer's hacky fix to Search.feature
- now that we are checking to see if there are any badIds, it was breaking the search functionality
2019-08-26 21:37:36 +02:00

86 lines
1.9 KiB
JavaScript

import gql from 'graphql-tag'
import isString from 'lodash/isString'
export const state = () => {
return {
quickResults: [],
quickPending: false,
quickValue: '',
}
}
export const mutations = {
SET_QUICK_RESULTS(state, results) {
state.quickResults = results || []
state.quickPending = false
},
SET_QUICK_PENDING(state, pending) {
state.quickPending = pending
},
SET_QUICK_VALUE(state, value) {
state.quickValue = value
},
}
export const getters = {
quickResults(state) {
return state.quickResults
},
quickPending(state) {
return state.quickPending
},
quickValue(state) {
return state.quickValue
},
}
export const actions = {
async quickSearch({ commit, getters }, { value }) {
value = isString(value) ? value.trim() : ''
const lastVal = getters.quickValue
if (value.length < 3 || lastVal.toLowerCase() === value.toLowerCase()) {
return
}
commit('SET_QUICK_VALUE', 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
createdAt
author {
id
name
slug
}
}
}
`,
variables: {
filter: value.replace(/\s/g, '~ ') + '~',
},
})
.then(res => {
commit('SET_QUICK_RESULTS', res.data.findPosts || [])
})
.catch(() => {
commit('SET_QUICK_RESULTS', [])
})
.finally(() => {
commit('SET_QUICK_PENDING', false)
})
return getters.quickResults
},
async quickClear({ commit }) {
commit('SET_QUICK_PENDING', false)
commit('SET_QUICK_RESULTS', [])
commit('SET_QUICK_VALUE', '')
},
}