@@ -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-
8571export default store
0 commit comments