Skip to content

Commit abe0093

Browse files
committed
simplify store actions
1 parent 7473669 commit abe0093

File tree

5 files changed

+23
-36
lines changed

5 files changed

+23
-36
lines changed

src/components/NewsList.vue

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
<script>
2828
import Spinner from './Spinner.vue'
2929
import NewsItem from './NewsItem.vue'
30-
import { fetchInitialData } from '../store'
3130
3231
export default {
3332
name: 'NewsList',
@@ -76,7 +75,9 @@ export default {
7675
methods: {
7776
loadItems (to, from) {
7877
this.loading = true
79-
fetchInitialData(this.type).then(() => {
78+
this.$store.dispatch('FETCH_DATA_FOR_TYPE', {
79+
type: this.type
80+
}).then(() => {
8081
if (this.page < 0 || this.page > this.maxPage) {
8182
this.$router.replace(`/${this.type}/1`)
8283
return

src/server-entry.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ export default context => {
88
// call prefetch hooks on components matched by the route
99
const s = isDev && Date.now()
1010
return Promise.all(router.getMatchedComponents().map(component => {
11-
if (component.prefetch) {
12-
return component.prefetch(store)
11+
if (component.preFetch) {
12+
return component.preFetch(store)
1313
}
1414
})).then(() => {
1515
isDev && console.log(`data pre-fetch: ${Date.now() - s}ms`)

src/store/api.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function createServerSideAPI () {
3636
// warm the cache every 15 min, since the front page changes quite often
3737
warmCache()
3838
function warmCache () {
39-
fetchItems((api.__topIds__ || []).slice(0, 30))
39+
fetchItems((api.__ids__.top || []).slice(0, 30))
4040
setTimeout(warmCache, 1000 * 60 * 15)
4141
}
4242

@@ -60,7 +60,7 @@ export function fetchIdsByType (type) {
6060
export function watchTopIds (cb) {
6161
api.child(`topstories`).on('value', snapshot => {
6262
const ids = snapshot.val()
63-
api.__topIds__ = ids
63+
api.__ids__ && (api.__ids__.top = ids)
6464
cb(ids)
6565
})
6666
}

src/store/index.js

+13-27
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,29 @@ const store = new Vuex.Store({
2222
},
2323

2424
actions: {
25-
FETCH_ACTIVE_IDS: ({ commit, state }) => {
26-
const type = state.activeType
27-
return fetchIdsByType(type).then(ids => {
28-
commit('SET_IDS', { type, ids })
29-
})
30-
},
31-
FETCH_ACTIVE_ITEMS: ({ commit, state, getters }) => {
32-
return fetchItems(getters.activeIds).then(items => {
33-
commit('SET_ITEMS', { items })
34-
})
25+
FETCH_DATA_FOR_TYPE: ({ commit, dispatch, state, getters }, { type }) => {
26+
commit('SET_ACTIVE_TYPE', { type })
27+
return fetchIdsByType(type)
28+
.then(ids => commit('SET_LIST', { type, ids }))
29+
.then(() => fetchItems(getters.activeIds.filter(id => !state.items[id])))
30+
.then(items => commit('SET_ITEMS', { items }))
3531
}
3632
},
3733

3834
mutations: {
3935
SET_ACTIVE_TYPE: (state, { type }) => {
4036
state.activeType = type
4137
},
42-
SET_IDS: (state, { type, ids }) => {
38+
39+
SET_LIST: (state, { type, ids }) => {
4340
state.lists[type] = ids
4441
},
42+
4543
SET_ITEMS: (state, { items }) => {
4644
items.forEach(item => {
47-
Vue.set(state.items, item.id, item)
45+
if (item) {
46+
Vue.set(state.items, item.id, item)
47+
}
4848
})
4949
}
5050
},
@@ -61,25 +61,11 @@ const store = new Vuex.Store({
6161
return []
6262
}
6363
},
64+
6465
activeItems (state, getters) {
6566
return getters.activeIds.map(id => state.items[id]).filter(_ => _)
6667
}
6768
}
6869
})
6970

70-
// watch for realtime top IDs updates on the client
71-
if (typeof window !== 'undefined') {
72-
watchTopIds(ids => {
73-
store.commit('SET_IDS', { type: 'top', ids })
74-
store.dispatch('FETCH_ACTIVE_ITEMS')
75-
})
76-
}
77-
78-
export function fetchInitialData (type) {
79-
store.commit('SET_ACTIVE_TYPE', { type })
80-
return store
81-
.dispatch('FETCH_ACTIVE_IDS')
82-
.then(() => store.dispatch('FETCH_ACTIVE_ITEMS'))
83-
}
84-
8571
export default store

src/views/CreateListView.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import NewsList from '../components/NewsList.vue'
2-
import { fetchInitialData } from '../store'
32

43
// factory function for creating root-level list views
54
// since they share most of the logic except for the type of items to display.
@@ -9,8 +8,9 @@ export function createListView (type) {
98
components: {
109
NewsList
1110
},
12-
prefetch () {
13-
return fetchInitialData(type)
11+
// this will be called during SSR to pre-fetch data into the store!
12+
preFetch (store) {
13+
return store.dispatch('FETCH_DATA_FOR_TYPE', { type })
1414
},
1515
created () {
1616
this.$store.commit('SET_ACTIVE_TYPE', { type })

0 commit comments

Comments
 (0)