1
1
import Vue from 'vue'
2
2
import Vuex from 'vuex'
3
- import { watchTopIds , fetchTopIds , fetchItems } from './api'
3
+ import { watchTopIds , fetchIdsByType , fetchItems } from './api'
4
4
5
5
Vue . use ( Vuex )
6
6
7
7
const store = new Vuex . Store ( {
8
8
state : {
9
+ activeType : null ,
9
10
itemsPerPage : 20 ,
10
- activeItemIds : [ ] ,
11
- items : { }
11
+ // the current items being displayed
12
+ activeItemIds : [ /* number */ ] ,
13
+ // fetched items by id. This also serves as a cache to some extent
14
+ items : { /* [id: number]: Item */ } ,
15
+ // the id lists for each type of stories
16
+ // will be periodically updated in realtime
17
+ itemIdsByType : {
18
+ top : [ ] ,
19
+ new : [ ] ,
20
+ show : [ ] ,
21
+ ask : [ ] ,
22
+ job : [ ]
23
+ }
12
24
} ,
13
25
14
26
actions : {
15
- FETCH_IDS : ( { commit } ) => {
16
- return fetchTopIds ( ) . then ( ids => {
17
- commit ( 'SET_ACTIVE_IDS' , { ids } )
27
+ FETCH_ACTIVE_IDS : ( { commit, state } ) => {
28
+ const type = state . activeType
29
+ return fetchIdsByType ( type ) . then ( ids => {
30
+ commit ( 'SET_IDS' , { type, ids } )
18
31
} )
19
32
} ,
20
- FETCH_DISPLAYED_ITEMS : ( { commit, state } ) => {
21
- const ids = getDisplayedIds ( state )
22
- return fetchItems ( ids ) . then ( items => {
33
+ FETCH_ACTIVE_ITEMS : ( { commit, state, getters } ) => {
34
+ return fetchItems ( getters . activeIds ) . then ( items => {
23
35
commit ( 'SET_ITEMS' , { items } )
24
36
} )
25
37
}
26
38
} ,
27
39
28
40
mutations : {
29
- SET_ACTIVE_IDS : ( state , { ids } ) => {
30
- state . activeItemIds = ids
41
+ SET_ACTIVE_TYPE : ( state , { type } ) => {
42
+ state . activeType = type
43
+ } ,
44
+ SET_IDS : ( state , { type, ids } ) => {
45
+ state . itemIdsByType [ type ] = ids
31
46
} ,
32
47
SET_ITEMS : ( state , { items } ) => {
33
48
items . forEach ( item => {
@@ -37,27 +52,36 @@ const store = new Vuex.Store({
37
52
} ,
38
53
39
54
getters : {
40
- displayedItems : state => {
41
- const ids = getDisplayedIds ( state )
42
- return ids . map ( id => state . items [ id ] ) . filter ( _ => _ )
55
+ activeIds ( state ) {
56
+ const { activeType, itemsPerPage, itemIdsByType } = state
57
+ const page = Number ( state . route . params . page ) || 1
58
+ if ( activeType ) {
59
+ const start = ( page - 1 ) * itemsPerPage
60
+ const end = page * itemsPerPage
61
+ return itemIdsByType [ activeType ] . slice ( start , end )
62
+ } else {
63
+ return [ ]
64
+ }
65
+ } ,
66
+ activeItems ( state , getters ) {
67
+ return getters . activeIds . map ( id => state . items [ id ] ) . filter ( _ => _ )
43
68
}
44
69
}
45
70
} )
46
71
47
72
// watch for realtime top IDs updates on the client
48
73
if ( typeof window !== 'undefined' ) {
49
74
watchTopIds ( ids => {
50
- store . commit ( 'SET_ACTIVE_IDS ' , { ids } )
51
- store . dispatch ( 'FETCH_DISPLAYED_ITEMS ' )
75
+ store . commit ( 'SET_IDS ' , { type : 'top' , ids } )
76
+ store . dispatch ( 'FETCH_ACTIVE_ITEMS ' )
52
77
} )
53
78
}
54
79
55
- function getDisplayedIds ( state ) {
56
- const page = Number ( state . route . params . page ) || 1
57
- const { itemsPerPage, activeItemIds } = state
58
- const start = ( page - 1 ) * itemsPerPage
59
- const end = page * itemsPerPage
60
- return activeItemIds . slice ( start , end )
80
+ export function fetchInitialData ( type ) {
81
+ store . commit ( 'SET_ACTIVE_TYPE' , { type } )
82
+ return store
83
+ . dispatch ( 'FETCH_ACTIVE_IDS' )
84
+ . then ( ( ) => store . dispatch ( 'FETCH_ACTIVE_ITEMS' ) )
61
85
}
62
86
63
87
export default store
0 commit comments