瀏覽代碼

:recycle: cleaning up vuex store

tags/0.9.0
J 4 年之前
父節點
當前提交
3c97df848a
共有 3 個文件被更改,包括 28 次插入39 次删除
  1. 10
    16
      vue-theme/src/store/modules/event.js
  2. 12
    19
      vue-theme/src/store/modules/exhibition.js
  3. 6
    4
      vue-theme/src/store/modules/post.js

+ 10
- 16
vue-theme/src/store/modules/event.js 查看文件

6
     singleEvent: null,
6
     singleEvent: null,
7
 }
7
 }
8
 
8
 
9
+const now = new Date()
10
+
9
 const getters = {
11
 const getters = {
10
     allEvents: state => state.all,
12
     allEvents: state => state.all,
11
     allEventsLoaded: state => state.loaded,
13
     allEventsLoaded: state => state.loaded,
12
-    pastEvents: state => {
13
-        const now = new Date()
14
-        return state.all.filter(event => {
15
-            return parseInt(event.end) > now
16
-        })
17
-    },
18
-    upcomingAndCurrentEvents: state => {
19
-        const now = new Date()
20
-        return state.all.filter(event => {
21
-            return parseInt(event.end) <= now
22
-        })
23
-    },
14
+    pastEvents: state => state.all.filter(event => parseInt(event.end) > now),
15
+    upcomingAndCurrentEvents: state.all.filter(event => parseInt(event.end) <= now),
24
 }
16
 }
25
 
17
 
26
 const actions = {
18
 const actions = {
27
     getAllEvents({ commit }, { sortType, params }) {
19
     getAllEvents({ commit }, { sortType, params }) {
28
         commit('CLEAR_EVENTS')
20
         commit('CLEAR_EVENTS')
29
         commit('EVENTS_LOADED', false)
21
         commit('EVENTS_LOADED', false)
30
-        return api.getByType({ type: 'event', sort: sortType, cb: events => {
22
+        const storeFetch = (events => {
31
             commit('STORE_FETCHED_EVENTS', { events })
23
             commit('STORE_FETCHED_EVENTS', { events })
32
             commit('EVENTS_LOADED', true)
24
             commit('EVENTS_LOADED', true)
33
-        }, params })
25
+        })
26
+        return api.getByType({ type: 'event', sort: sortType, params, cb: storeFetch })
34
     },
27
     },
35
     getMoreEvents({ commit }, { sortType, params }) {
28
     getMoreEvents({ commit }, { sortType, params }) {
36
-        return api.getByType({ type: 'event', sort: sortType, cb: events => {
29
+        const storeFetch = (events => {
37
             commit('ADD_TO_FETCHED_EVENTS', { events })
30
             commit('ADD_TO_FETCHED_EVENTS', { events })
38
             commit('EVENTS_LOADED', true)
31
             commit('EVENTS_LOADED', true)
39
-        }, params })
32
+        })
33
+        return api.getByType({ type: 'event', sort: sortType, params, cb: storeFetch })
40
     },
34
     },
41
     getSingleEvent({ commit }, id) {
35
     getSingleEvent({ commit }, id) {
42
         commit('CLEAR_SINGLE_EVENTS')
36
         commit('CLEAR_SINGLE_EVENTS')

+ 12
- 19
vue-theme/src/store/modules/exhibition.js 查看文件

6
     singleExhibition: null,
6
     singleExhibition: null,
7
 }
7
 }
8
 
8
 
9
+const now = new Date()
10
+
9
 const getters = {
11
 const getters = {
10
     allExhibitions: state => state.all,
12
     allExhibitions: state => state.all,
11
     allExhibitionsLoaded: state => state.loaded,
13
     allExhibitionsLoaded: state => state.loaded,
12
-    pastExhibitions: state => {
13
-        return state.all.filter(exhibition => {
14
-            const now = new Date()
15
-            return parseInt(exhibition.end) > now
16
-        })
17
-    },
18
-    upcomingAndCurrentExhibitions: state => {
19
-        return state.all
20
-        // return state.all.filter(exhibition => {
21
-        //     const now = new Date()
22
-        //     // return parseInt(exhibition.end) <= now
23
-        //     return parseInt(exhibition.end) <= now
24
-        // })
25
-    },
14
+    pastExhibitions: state => state.all.filter(exhibition => parseInt(exhibition.end) > now),
15
+    upcomingAndCurrentExhibitions: state => state.all,
16
+    // upcomingAndCurrentExhibitions: state => state.all.filter(exhibition => parseInt(exhibition.end) <= now)
26
 }
17
 }
27
 
18
 
28
 const actions = {
19
 const actions = {
29
     getAllExhibitions({ commit },{ sortType, params }) {
20
     getAllExhibitions({ commit },{ sortType, params }) {
30
         commit('CLEAR_EXHIBITIONS')
21
         commit('CLEAR_EXHIBITIONS')
31
         commit('EXHIBITIONS_LOADED', false)
22
         commit('EXHIBITIONS_LOADED', false)
32
-        return api.getByType({ type: 'exhibition', sort: sortType, cb: exhibitions => {
23
+        const storeFetch = (exhibitions => {
33
             commit('STORE_FETCHED_EXHIBITIONS', { exhibitions })
24
             commit('STORE_FETCHED_EXHIBITIONS', { exhibitions })
34
             commit('EXHIBITIONS_LOADED', true)
25
             commit('EXHIBITIONS_LOADED', true)
35
-        }, params })
26
+        })
27
+        return api.getByType({ type: 'exhibition', sort: sortType, params, cb: storeFetch })
36
     },
28
     },
37
-    getMoreExhibitions({ commit },{ sortType, params }) {
38
-        return api.getByType({ type: 'exhibition', sort: sortType, cb: exhibitions => {
29
+    getMoreExhibitions({ commit }, { sortType, params }) {
30
+        const storeFetch = (exhibitions => {
39
             commit('ADD_TO_FETCHED_EXHIBITIONS', { exhibitions })
31
             commit('ADD_TO_FETCHED_EXHIBITIONS', { exhibitions })
40
             commit('EXHIBITIONS_LOADED', true)
32
             commit('EXHIBITIONS_LOADED', true)
41
-        }, params })
33
+        })
34
+        return api.getByType({ type: 'exhibition', sort: sortType, params, cb: storeFetch })
42
     },
35
     },
43
     getSingleExhibition({ commit }, id) {
36
     getSingleExhibition({ commit }, id) {
44
         commit('CLEAR_SINGLE_EXHIBITIONS')
37
         commit('CLEAR_SINGLE_EXHIBITIONS')

+ 6
- 4
vue-theme/src/store/modules/post.js 查看文件

15
     getAllPosts({ commit }, { sortType, params }) {
15
     getAllPosts({ commit }, { sortType, params }) {
16
         commit('CLEAR_POSTS')
16
         commit('CLEAR_POSTS')
17
         commit('POSTS_LOADED', false)
17
         commit('POSTS_LOADED', false)
18
-        return api.getByType({ type: 'post', sort: sortType, cb: posts => {
18
+        const storeFetch = (posts => {
19
             commit('STORE_FETCHED_POSTS', { posts })
19
             commit('STORE_FETCHED_POSTS', { posts })
20
             commit('POSTS_LOADED', true)
20
             commit('POSTS_LOADED', true)
21
-        }, params })
21
+        }) 
22
+        return api.getByType({ type: 'post', sort: sortType, params, cb: storeFetch)
22
     },
23
     },
23
     getMorePosts({ commit }, { sortType, params }) {
24
     getMorePosts({ commit }, { sortType, params }) {
24
-        return api.getByType({ type: 'post', sort: sortType, cb: posts => {
25
+        const storeFetch = (posts => {
25
             commit('ADD_TO_FETCHED_POSTS', { posts })
26
             commit('ADD_TO_FETCHED_POSTS', { posts })
26
             commit('POSTS_LOADED', true)
27
             commit('POSTS_LOADED', true)
27
-        }, params })
28
+        })
29
+        return api.getByType({ type: 'post', sort: sortType, params, cb: storeFetch })
28
     },
30
     },
29
     getSinglePost({ commit }, id) {
31
     getSinglePost({ commit }, id) {
30
         commit('CLEAR_SINGLE_POSTS')
32
         commit('CLEAR_SINGLE_POSTS')

Loading…
取消
儲存