Bladeren bron

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 jaren geleden
bovenliggende
commit
d4d7260636

+ 14
- 4
plugins/cia-endpoints/includes/class.make-endpoint.php Bestand weergeven

20
             array(
20
             array(
21
                 'methods'  => WP_REST_Server::READABLE,
21
                 'methods'  => WP_REST_Server::READABLE,
22
                 'callback' => array( $this, 'get_items' )
22
                 'callback' => array( $this, 'get_items' )
23
-            ),   
23
+            ),
24
         ]);
24
         ]);
25
         register_rest_route( $namespace, '/' . $route . '/(?P<id>[\d]+)', [
25
         register_rest_route( $namespace, '/' . $route . '/(?P<id>[\d]+)', [
26
             array(
26
             array(
27
                 'methods'  => WP_REST_Server::READABLE,
27
                 'methods'  => WP_REST_Server::READABLE,
28
                 'callback' => array( $this, 'get_items' )
28
                 'callback' => array( $this, 'get_items' )
29
-            ),   
29
+            ),
30
         ]);
30
         ]);
31
     }
31
     }
32
 
32
 
33
     public function get_items( $request ) {
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
         // Get parameters from request
43
         // Get parameters from request
37
         // /<id>?limit=<num>
44
         // /<id>?limit=<num>
44
         return new WP_REST_Response( $this->prepare_item_for_response($args), 200 );
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
         $collection = array();
55
         $collection = array();
56
+        
57
+        // https://developer.wordpress.org/reference/functions/get_posts/
49
         foreach( get_posts($args) as $item ) {
58
         foreach( get_posts($args) as $item ) {
50
             $filtered = default_post_format($item);
59
             $filtered = default_post_format($item);
51
             
60
             
59
             $collection[$item->ID] = $filtered;
68
             $collection[$item->ID] = $filtered;
60
         }
69
         }
61
         wp_reset_postdata();
70
         wp_reset_postdata();
71
+
62
         return $collection;
72
         return $collection;
63
     }
73
     }
64
 }
74
 }

+ 22
- 12
vue-theme/src/components/sidebars/sidebar.vue Bestand weergeven

2
 aside.sidebar
2
 aside.sidebar
3
     section
3
     section
4
         .shadow(v-if="type === 'artists'")
4
         .shadow(v-if="type === 'artists'")
5
-            h1.t-up sort by
5
+            h1.t-up sort {{ type }} by
6
             ul
6
             ul
7
                 li(v-for="option in sortOptions")
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
                         p {{ option }}
9
                         p {{ option }}
10
         .shadow(v-else)
10
         .shadow(v-else)
11
             p {{ type }} sidebar
11
             p {{ type }} sidebar
21
             type: String
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
     computed: {
35
     computed: {
25
         sortOptions() {
36
         sortOptions() {
26
             let opts = []
37
             let opts = []
27
             switch (this.type) {
38
             switch (this.type) {
28
                 case 'artists':
39
                 case 'artists':
29
                     opts = [
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
                     break
45
                     break
35
                 case 'shorts':
46
                 case 'shorts':
36
                     opts = [
47
                     opts = [
37
-                        'by artist',
38
-                        'by material',
48
+                        Object.keys(this.sortTypes)[3],
49
+                        Object.keys(this.sortTypes)[2],
39
                     ]
50
                     ]
40
                     break
51
                     break
41
                 case 'guides':
52
                 case 'guides':
42
                     opts = [
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
                     break
58
                     break
48
             }
59
             }
49
             return opts
60
             return opts
50
         }
61
         }
51
-    },
52
-    mounted() {}
62
+    }
53
 }
63
 }
54
 </script>
64
 </script>
55
 
65
 

+ 2
- 2
vue-theme/src/index.js Bestand weergeven

18
         { path: '/episodes', component: () => import(/* webpackChunkName: "pages-list" */ './pages/list.vue'), props: { sidebar: false } },
18
         { path: '/episodes', component: () => import(/* webpackChunkName: "pages-list" */ './pages/list.vue'), props: { sidebar: false } },
19
         { path: '/:type', component: () => import(/* webpackChunkName: "pages-list" */ './pages/list.vue'), props: { sidebar: true } },
19
         { path: '/:type', component: () => import(/* webpackChunkName: "pages-list" */ './pages/list.vue'), props: { sidebar: true } },
20
         // Sorted List Pages 
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
         { path: '/:type/by-material', component: () => import(/* webpackChunkName: "pages-list" */ './pages/list.vue'), props: { sidebar: true, sortBy: 'by-material' } },
23
         { path: '/:type/by-material', component: () => import(/* webpackChunkName: "pages-list" */ './pages/list.vue'), props: { sidebar: true, sortBy: 'by-material' } },
24
         { path: '/:type/by-episode', component: () => import(/* webpackChunkName: "pages-list" */ './pages/list.vue'), props: { sidebar: true, sortBy: 'by-episode' } },
24
         { path: '/:type/by-episode', component: () => import(/* webpackChunkName: "pages-list" */ './pages/list.vue'), props: { sidebar: true, sortBy: 'by-episode' } },
25
         { path: '/:type/by-artist', component: () => import(/* webpackChunkName: "pages-list" */ './pages/list.vue'), props: { sidebar: true, sortBy: 'by-artist' } },
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 Bestand weergeven

13
 
13
 
14
         section(v-for="post in posts").shadow
14
         section(v-for="post in posts").shadow
15
             router-link(:to="`/${type}/${post.slug}`")
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
     sidebar(v-if="sidebar" :type="`${type}`")
19
     sidebar(v-if="sidebar" :type="`${type}`")
19
         .shadow
20
         .shadow
58
         },
59
         },
59
         posts() {
60
         posts() {
60
             let type = this.convertTitleCase(this.type)
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
     methods: {
74
     methods: {
69
     mounted() {
79
     mounted() {
70
         let type = this.convertTitleCase(this.type)
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
         this.$store.commit('SET_HERO', type)
83
         this.$store.commit('SET_HERO', type)
79
     },
84
     },
80
     watch: {
85
     watch: {
81
         $route(to, from){
86
         $route(to, from){
82
             let type = this.convertTitleCase(to.path.slice(1))
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
             if(this.$store.state.hero.url !== type) {
93
             if(this.$store.state.hero.url !== type) {
84
                 this.$store.commit('SET_HERO', type)
94
                 this.$store.commit('SET_HERO', type)
85
             }
95
             }

+ 5
- 2
vue-theme/src/store/modules/artist.js Bestand weergeven

29
 }
29
 }
30
 
30
 
31
 const actions = {
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
             commit('STORE_FETCHED_ARTISTS', { artists })
36
             commit('STORE_FETCHED_ARTISTS', { artists })
35
             commit('ARTISTS_LOADED', true)
37
             commit('ARTISTS_LOADED', true)
36
         })
38
         })
39
 
41
 
40
 const mutations = {
42
 const mutations = {
41
     STORE_FETCHED_ARTISTS(state, { artists }) { state.all = artists },
43
     STORE_FETCHED_ARTISTS(state, { artists }) { state.all = artists },
44
+    CLEAR_ARTISTS(state) { state.all = [] },
42
     ARTISTS_LOADED(state, val) { state.loaded = val },
45
     ARTISTS_LOADED(state, val) { state.loaded = val },
43
 }
46
 }
44
 
47
 

+ 2
- 2
vue-theme/src/store/modules/episode.js Bestand weergeven

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

+ 2
- 2
vue-theme/src/store/modules/page.js Bestand weergeven

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

+ 2
- 2
vue-theme/src/store/modules/post.js Bestand weergeven

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

+ 11
- 8
vue-theme/src/utils/api.js Bestand weergeven

6
 }
6
 }
7
 
7
 
8
 export default {
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
     getPosts(limit = 5, cb) {
21
     getPosts(limit = 5, cb) {
19
         axios
22
         axios

Laden…
Annuleren
Opslaan