Просмотр исходного кода

feat: sorting by recent moved to the front-end | sorting by recent is default | created sort-by listings on front-end

tags/0.9.0
J 6 лет назад
Родитель
Сommit
d4d7260636

+ 14
- 4
plugins/cia-endpoints/includes/class.make-endpoint.php Просмотреть файл

@@ -20,18 +20,25 @@ class Make_Endpoint_For extends WP_REST_Controller {
20 20
             array(
21 21
                 'methods'  => WP_REST_Server::READABLE,
22 22
                 'callback' => array( $this, 'get_items' )
23
-            ),   
23
+            ),
24 24
         ]);
25 25
         register_rest_route( $namespace, '/' . $route . '/(?P<id>[\d]+)', [
26 26
             array(
27 27
                 'methods'  => WP_REST_Server::READABLE,
28 28
                 'callback' => array( $this, 'get_items' )
29
-            ),   
29
+            ),
30 30
         ]);
31 31
     }
32 32
 
33 33
     public function get_items( $request ) {
34
-        $args = array( 'numberposts' => -1, 'post_type' => $this->post_type );
34
+        $args = array(
35
+            'numberposts' => -1,
36
+            'post_type' => $this->post_type,
37
+            // Order DOES NOT WORK because you're
38
+            // returning the posts by id - DUH
39
+            // 'orderby' => 'date',
40
+            // 'order' => 'DESC'
41
+        );
35 42
         
36 43
         // Get parameters from request
37 44
         // /<id>?limit=<num>
@@ -44,8 +51,10 @@ class Make_Endpoint_For extends WP_REST_Controller {
44 51
         return new WP_REST_Response( $this->prepare_item_for_response($args), 200 );
45 52
     }
46 53
 
47
-    public function prepare_item_for_response( $args) {
54
+    public function prepare_item_for_response( $args ) {
48 55
         $collection = array();
56
+        
57
+        // https://developer.wordpress.org/reference/functions/get_posts/
49 58
         foreach( get_posts($args) as $item ) {
50 59
             $filtered = default_post_format($item);
51 60
             
@@ -59,6 +68,7 @@ class Make_Endpoint_For extends WP_REST_Controller {
59 68
             $collection[$item->ID] = $filtered;
60 69
         }
61 70
         wp_reset_postdata();
71
+
62 72
         return $collection;
63 73
     }
64 74
 }

+ 22
- 12
vue-theme/src/components/sidebars/sidebar.vue Просмотреть файл

@@ -2,10 +2,10 @@
2 2
 aside.sidebar
3 3
     section
4 4
         .shadow(v-if="type === 'artists'")
5
-            h1.t-up sort by
5
+            h1.t-up sort {{ type }} by
6 6
             ul
7 7
                 li(v-for="option in sortOptions")
8
-                    router-link(:to="`/${type}/${option.replace(' ', '-')}`").t-cap
8
+                    router-link(:to="`/${type}/${sortTypes[option]}`").t-cap
9 9
                         p {{ option }}
10 10
         .shadow(v-else)
11 11
             p {{ type }} sidebar
@@ -21,35 +21,45 @@ export default {
21 21
             type: String
22 22
         }
23 23
     },
24
+    data() {
25
+        return {
26
+            sortTypes: {
27
+                'most recent': '',
28
+                'alphabetized': 'by-alpha',
29
+                'by material': 'by-material',
30
+                'by artist': 'by-artist',
31
+                'by episode': 'by-episode',
32
+            }
33
+        }
34
+    },
24 35
     computed: {
25 36
         sortOptions() {
26 37
             let opts = []
27 38
             switch (this.type) {
28 39
                 case 'artists':
29 40
                     opts = [
30
-                        'most recent',
31
-                        'alphabetized',
32
-                        'by material',
41
+                        Object.keys(this.sortTypes)[0],
42
+                        Object.keys(this.sortTypes)[1],
43
+                        Object.keys(this.sortTypes)[2],
33 44
                     ]
34 45
                     break
35 46
                 case 'shorts':
36 47
                     opts = [
37
-                        'by artist',
38
-                        'by material',
48
+                        Object.keys(this.sortTypes)[3],
49
+                        Object.keys(this.sortTypes)[2],
39 50
                     ]
40 51
                     break
41 52
                 case 'guides':
42 53
                     opts = [
43
-                        'most recent',
44
-                        'by episode',
45
-                        'by material',
54
+                        Object.keys(this.sortTypes)[0],
55
+                        Object.keys(this.sortTypes)[4],
56
+                        Object.keys(this.sortTypes)[2],
46 57
                     ]
47 58
                     break
48 59
             }
49 60
             return opts
50 61
         }
51
-    },
52
-    mounted() {}
62
+    }
53 63
 }
54 64
 </script>
55 65
 

+ 2
- 2
vue-theme/src/index.js Просмотреть файл

@@ -18,8 +18,8 @@ const router = new VueRouter({
18 18
         { path: '/episodes', component: () => import(/* webpackChunkName: "pages-list" */ './pages/list.vue'), props: { sidebar: false } },
19 19
         { path: '/:type', component: () => import(/* webpackChunkName: "pages-list" */ './pages/list.vue'), props: { sidebar: true } },
20 20
         // Sorted List Pages 
21
-        { path: '/:type/alphabetized', component: () => import(/* webpackChunkName: "pages-list" */ './pages/list.vue'), props: { sidebar: true, sortBy: 'alphabetized' } },
22
-        { path: '/:type/most-recent', component: () => import(/* webpackChunkName: "pages-list" */ './pages/list.vue'), props: { sidebar: true, sortBy: 'most-recent' } },
21
+        { path: '/:type/by-alpha', component: () => import(/* webpackChunkName: "pages-list" */ './pages/list.vue'), props: { sidebar: true, sortBy: 'by-alpha' } },
22
+        { path: '/:type/recent', component: () => import(/* webpackChunkName: "pages-list" */ './pages/list.vue'), props: { sidebar: true, sortBy: 'recent' } },
23 23
         { path: '/:type/by-material', component: () => import(/* webpackChunkName: "pages-list" */ './pages/list.vue'), props: { sidebar: true, sortBy: 'by-material' } },
24 24
         { path: '/:type/by-episode', component: () => import(/* webpackChunkName: "pages-list" */ './pages/list.vue'), props: { sidebar: true, sortBy: 'by-episode' } },
25 25
         { path: '/:type/by-artist', component: () => import(/* webpackChunkName: "pages-list" */ './pages/list.vue'), props: { sidebar: true, sortBy: 'by-artist' } },

+ 18
- 8
vue-theme/src/pages/list.vue Просмотреть файл

@@ -13,7 +13,8 @@
13 13
 
14 14
         section(v-for="post in posts").shadow
15 15
             router-link(:to="`/${type}/${post.slug}`")
16
-                h4 {{ post.title }}
16
+                h4 {{ post.title }} 
17
+                p(style="font-size: 9px;") {{ post.date }}
17 18
 
18 19
     sidebar(v-if="sidebar" :type="`${type}`")
19 20
         .shadow
@@ -58,7 +59,16 @@ export default {
58 59
         },
59 60
         posts() {
60 61
             let type = this.convertTitleCase(this.type)
61
-            return this[`all${type}`]
62
+
63
+            // We're override the API to sort by date
64
+            // because items are returned by ID
65
+            let unsortedOfType = Object.values(this[`all${type}`])
66
+            let sortedByRecent = unsortedOfType.sort((postA, postB) => {
67
+                return new Date(postB.date) - new Date(postA.date)
68
+            })
69
+            
70
+            // !: Sorting of this[`all${type}`] is controlled by API
71
+            return this.sortBy ? this[`all${type}`] : sortedByRecent
62 72
         },
63 73
     },
64 74
     methods: {
@@ -69,17 +79,17 @@ export default {
69 79
     mounted() {
70 80
         let type = this.convertTitleCase(this.type)
71 81
         
72
-        if(this.sortBy) {
73
-            // TODO: Make a sorted call instead
74
-            this.$store.dispatch(`getAll${type}`)
75
-        } else {
76
-            this.$store.dispatch(`getAll${type}`)
77
-        }
82
+        this.$store.dispatch(`getAll${type}`, this.sortBy)
78 83
         this.$store.commit('SET_HERO', type)
79 84
     },
80 85
     watch: {
81 86
         $route(to, from){
82 87
             let type = this.convertTitleCase(to.path.slice(1))
88
+
89
+            // Accounts for the sort-by in URL case 
90
+            let dispatchName = this.sortBy ? `getAll${type.split('/')[0]}` : `getAll${type}`
91
+            this.$store.dispatch(dispatchName, this.sortBy)
92
+            
83 93
             if(this.$store.state.hero.url !== type) {
84 94
                 this.$store.commit('SET_HERO', type)
85 95
             }

+ 5
- 2
vue-theme/src/store/modules/artist.js Просмотреть файл

@@ -29,8 +29,10 @@ const getters = {
29 29
 }
30 30
 
31 31
 const actions = {
32
-    getAllArtists({ commit }) {
33
-        api.getByType('artists', artists => {
32
+    getAllArtists({ commit }, sortType) {
33
+        commit('CLEAR_ARTISTS')
34
+        
35
+        api.getByType('artists', sortType, artists => {
34 36
             commit('STORE_FETCHED_ARTISTS', { artists })
35 37
             commit('ARTISTS_LOADED', true)
36 38
         })
@@ -39,6 +41,7 @@ const actions = {
39 41
 
40 42
 const mutations = {
41 43
     STORE_FETCHED_ARTISTS(state, { artists }) { state.all = artists },
44
+    CLEAR_ARTISTS(state) { state.all = [] },
42 45
     ARTISTS_LOADED(state, val) { state.loaded = val },
43 46
 }
44 47
 

+ 2
- 2
vue-theme/src/store/modules/episode.js Просмотреть файл

@@ -30,8 +30,8 @@ const getters = {
30 30
 }
31 31
 
32 32
 const actions = {
33
-    getAllEpisodes({ commit }) {
34
-        api.getByType('episodes', episodes => {
33
+    getAllEpisodes({ commit }, sortType) {
34
+        api.getByType('episodes', sortType, episodes => {
35 35
             commit('STORE_FETCHED_EPISODES', { episodes })
36 36
             commit('EPISODES_LOADED', true)
37 37
         })

+ 2
- 2
vue-theme/src/store/modules/page.js Просмотреть файл

@@ -33,8 +33,8 @@ const getters = {
33 33
 
34 34
 // actions
35 35
 const actions = {
36
-    getAllPages({ commit }) {
37
-        api.getByType('pages', pages => {
36
+    getAllPages({ commit }, sortType) {
37
+        api.getByType('pages', sortType, pages => {
38 38
             commit('STORE_FETCHED_PAGES', { pages })
39 39
             commit('PAGES_LOADED', true)
40 40
         })

+ 2
- 2
vue-theme/src/store/modules/post.js Просмотреть файл

@@ -30,8 +30,8 @@ const getters = {
30 30
 }
31 31
 
32 32
 const actions = {
33
-    getAllPosts({ commit }) {
34
-        api.getByType('posts', posts => {
33
+    getAllPosts({ commit }, sortType) {
34
+        api.getByType('posts', sortType, posts => {
35 35
             commit('STORE_FETCHED_POSTS', { posts })
36 36
             commit('POSTS_LOADED', true)
37 37
         })

+ 11
- 8
vue-theme/src/utils/api.js Просмотреть файл

@@ -6,14 +6,17 @@ const SETTINGS = {
6 6
 }
7 7
 
8 8
 export default {
9
-    getByType(type, cb) {
10
-        axios.get(SETTINGS.API_BASE_PATH + `${type}`)
11
-        .then(response => {
12
-            cb(response.data)
13
-        })
14
-        .catch(e => {
15
-            cb(e)
16
-        })
9
+    getByType(type, sortType, cb) {
10
+        console.log('sortType:', sortType)
11
+        if (sortType) {
12
+            axios.get(SETTINGS.API_BASE_PATH + `sort/${type}/${sortType}`).then(response => {
13
+                cb(response.data)
14
+            }).catch(e => { cb(e) })
15
+        } else {
16
+            axios.get(SETTINGS.API_BASE_PATH + `${type}`).then(response => {
17
+                cb(response.data)
18
+            }).catch(e => { cb(e) })
19
+        }
17 20
     },
18 21
     getPosts(limit = 5, cb) {
19 22
         axios

Загрузка…
Отмена
Сохранить