1
1
import Vue from 'vue'
2
2
import Vuex from 'vuex'
3
- import { fetchIdsByType , fetchItems } from './api'
3
+ import { fetchIdsByType , fetchItem , fetchItems } from './api'
4
4
5
5
Vue . use ( Vuex )
6
6
@@ -23,17 +23,23 @@ const store = new Vuex.Store({
23
23
24
24
actions : {
25
25
// ensure data for rendering given list type
26
- FETCH_DATA_FOR_TYPE : ( { commit, dispatch, state, getters } , { type } ) => {
26
+ FETCH_LIST_DATA : ( { commit, dispatch, state, getters } , { type } ) => {
27
27
commit ( 'SET_ACTIVE_TYPE' , { type } )
28
28
return fetchIdsByType ( type )
29
29
. then ( ids => commit ( 'SET_LIST' , { type, ids } ) )
30
- . then ( ( ) => dispatch ( 'FETCH_ACTIVE_ITEMS ' ) )
30
+ . then ( ( ) => dispatch ( 'ENSURE_ACTIVE_ITEMS ' ) )
31
31
} ,
32
32
33
33
// ensure all active items are fetched
34
- FETCH_ACTIVE_ITEMS : ( { commit, state, getters } ) => {
34
+ ENSURE_ACTIVE_ITEMS : ( { dispatch, getters } ) => {
35
+ return dispatch ( 'FETCH_ITEMS' , {
36
+ ids : getters . activeIds
37
+ } )
38
+ } ,
39
+
40
+ FETCH_ITEMS : ( { commit, state } , { ids } ) => {
35
41
// only fetch items that we don't already have.
36
- const ids = getters . activeIds . filter ( id => ! state . items [ id ] )
42
+ ids = ids . filter ( id => ! state . items [ id ] )
37
43
if ( ids . length ) {
38
44
return fetchItems ( ids ) . then ( items => commit ( 'SET_ITEMS' , { items } ) )
39
45
} else {
0 commit comments