Skip to content
This repository has been archived by the owner on Aug 12, 2021. It is now read-only.

Commit

Permalink
facet changes dynamically update choice counts (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
thatbudakguy committed Aug 2, 2018
1 parent e329f1a commit cec6722
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
1 change: 0 additions & 1 deletion sitemedia/js/components/RangeFacet.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export default Vue.component('RangeFacet', {
<sui-input :placeholder="maxVal" />
</div>
<div class="histogram">
{{ choices }}
</div>
</div>
`,
Expand Down
4 changes: 3 additions & 1 deletion sitemedia/js/components/books/BooksSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,15 @@ export default Vue.component('BooksSearch', {
]),
},
created() {
this.loadSearchData('/static/js/components/books/data.json')
this.setEndpoint('/books/facets/')
this.loadSearchData()
},
methods: {
...mapActions([
'loadSearchData',
'clearFacetChoices',
'toggleFacetChoice',
'setEndpoint',
]),
/**
* Generate a string label for search widget tabs.
Expand Down
31 changes: 28 additions & 3 deletions sitemedia/js/store/actions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import router from '../router'

export default {
loadSearchData ({ commit }, query) {
return fetch(query)
loadSearchData ({ commit, getters }) {
return fetch(getters.dataPath)
.then(res => res.json())
.then(data => {
commit('setTotalResults', data.total)
Expand Down Expand Up @@ -33,13 +33,38 @@ export default {
})
},

toggleFacetChoice ({ commit, getters }, choice) {
updateFacetCounts ({ commit, getters }) {
return fetch(getters.dataPath)
.then(res => res.json())
.then(data => {
commit('setTotalResults', data.total)
commit('updateFacetChoiceCounts', Object.entries(data.facets))
})
},

toggleFacetChoice ({ commit, getters, dispatch }, choice) {
let activeBeforeToggle = Object.keys(getters.activeFacets)
commit('editFacetChoice', { choice, active: !choice.active })
router.replace({
query: {
...getters.activeFacets
}
})
if (Object.keys(getters.activeFacets) != activeBeforeToggle) { // a facet was added or removed
return dispatch('updateFacetCounts')
}
},

setRangeFacetMin ({ commit }, facet) {
commit('editRangeFacet', facet)
},

setRangeFacetMax ({ commit }, facet) {
commit('editRangeFacet', facet)
},

setEndpoint ({ commit }, endpoint) {
commit('setEndpoint', endpoint)
},

clearFacetChoices ({ commit }) {
Expand Down
5 changes: 5 additions & 0 deletions sitemedia/js/store/getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@ export default {
else acc[cur.facet] = cur.value
return acc
}, {})
},

dataPath (state) {
let querystring = state.route.fullPath.split('?')[1]
return querystring ? `${state.endpoint}?${querystring}` : state.endpoint
}
}
1 change: 1 addition & 0 deletions sitemedia/js/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const state = {
totalResults: 0,
facetChoices: [],
rangeFacets: [],
endpoint: ''
}

export default new Vuex.Store({
Expand Down
12 changes: 12 additions & 0 deletions sitemedia/js/store/mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export default {
})
},

updateFacetChoiceCounts (state, facets) {
facets.forEach(facet => {
Object.keys(facet[1]).forEach(value => {
state.facetChoices.find(c => c.facet === facet[0] && c.value === value).count = facet[1][value]
})
})
},

addRangeFacet (state, facet) {
state.rangeFacets.push(facet)
},
Expand All @@ -44,6 +52,10 @@ export default {
state.rangeFacets[facet.facet].maxVal = maxVal
},

setEndpoint (state, endpoint) {
state.endpoint = endpoint
},

setTotalResults (state, total) {
state.totalResults = total
},
Expand Down

0 comments on commit cec6722

Please sign in to comment.