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

:recycle: update sort-by return paginated results

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

+ 18
- 1
plugins/cia-endpoints/includes/class.make-sortby.php Просмотреть файл

@@ -112,7 +112,24 @@ class Make_Sort_By extends WP_REST_Controller {
112 112
             $start, $time, $time, $end, $time, $this->post_type
113 113
         ));
114 114
         wp_reset_postdata();
115
-        return new WP_REST_Response( $this->prepare_items_for_reponse($res), 200 );
115
+
116
+        // Get parameters from request,
117
+        // /?limit=<num>&?orderby=<str>&?order=<str>
118
+        $args = array(
119
+            'posts_per_page' => -1,
120
+            'page' => 1
121
+        );
122
+        $params = $request->get_params();
123
+        if(intval($params['limit']) > 0) {
124
+            $args['posts_per_page'] = intval($params['limit']);
125
+        }
126
+        if(intval($params['p']) > 1) {
127
+            $args['page'] = intval($params['p']);
128
+        }
129
+        $index_from_page = $args['page'] - 1;
130
+        $offset = array_chunk($res, $args['posts_per_page']);
131
+        $page = $offset[$index_from_page];
132
+        return new WP_REST_Response( $this->prepare_items_for_reponse($page), 200 );
116 133
     }
117 134
     
118 135
     public function by_upcoming( $request ) {

+ 1
- 1
vue-theme/src/components/card.vue Просмотреть файл

@@ -10,7 +10,7 @@
10 10
         .f-col.w-max
11 11
             router-link(:to="`/${type}/${content.slug}`")
12 12
                 h1.t-up.t-cntr.t-b {{ content.title }}
13
-            p(v-if="content.end") {{ content.start }} &ndash; {{ content.end }}
13
+            p(v-if="content.end") {{ new Date(content.start * 1000).toISOString().split('T')[0] }} &ndash; {{ new Date(content.end * 1000).toISOString().split('T')[0] }}
14 14
             p {{ content.excerpt }}
15 15
 </template>
16 16
 

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

@@ -53,8 +53,8 @@ export default {
53 53
             return typeFromRoute(this.$route)
54 54
         },
55 55
         pType() {
56
-            if(!this.type) return
57
-            return this.sortBy ? `${convertTitleCase(this.type.split('/')[0])}s` : `${convertTitleCase(this.type)}s`
56
+            if(!typeFromRoute(this.$route)) return
57
+            return this.sortBy ? `${convertTitleCase(typeFromRoute(this.$route).split('/')[0])}s` : `${convertTitleCase(this.type)}s`
58 58
         },
59 59
         loaded() {
60 60
             if (!this.pType) return
@@ -67,8 +67,11 @@ export default {
67 67
     },
68 68
     methods: {
69 69
         loadMorePosts() {
70
-            if(!this.type) return console.warn(`type: ${this.type} not found...`)
71
-            console.warn(`loading more ${this.type} posts...`)
70
+            console.log(this.$route)
71
+            const type = typeFromRoute(this.$route)
72
+            if(!type) return console.warn(`type: ${type} not found...`)
73
+
74
+            console.warn(`loading more ${type} posts...`)
72 75
             this.page++
73 76
             this.getPosts({ clear: false })
74 77
         },
@@ -151,34 +154,31 @@ export default {
151 154
         }
152 155
     },
153 156
     watch: {
154
-        $route: {
155
-            handler: (to, from) => {
156
-                // Filter the path to remove blank strings and
157
-                // only grab the hero if moving to another list page
158
-                // eg. list page -> list page
159
-                const path = to.fullPath.split('/').filter(p => p)
160
-                console.log('to:', path)
161
-                console.log('?:', path.pop())
162
-                console.log('?:', path.pop())
163
-    
164
-                // Always reset the page count
165
-                this.page = 0
166
-                
167
-                // this.checkAndSetHero(this.type)
168
-                
169
-                // Load first page of results
170
-                // this.getPosts({ clear: false })
171
-                console.log("route", to, from)
172
-                // this.setIntersectionLoader()
173
-            },
174
-            deep: true
157
+        $route(to, from) {
158
+            // Filter the path to remove blank strings and
159
+            // only grab the hero if moving to another list page
160
+            // eg. list page -> list page
161
+            // const path = to.fullPath.split('/').filter(p => p)
162
+            // console.log('to:', path)
163
+            // console.log('?:', path.pop())
164
+            // console.log('?:', path.pop())
165
+
166
+            // Always reset the page count
167
+            this.page = 0
168
+            
169
+            // this.checkAndSetHero(this.type)
170
+            
171
+            // Load first page of results
172
+            // this.getPosts({ clear: false })
173
+            console.log("route", to, from)
174
+            // this.setIntersectionLoader()
175 175
         },
176 176
     },
177 177
     mounted() {
178
-        this.setIntersectionLoader()
179 178
     },
180 179
     created() {
181 180
         console.log('--- liiiist ---')
181
+        this.setIntersectionLoader()
182 182
         // this.checkAndSetHero(this.type)
183 183
     },
184 184
 }

+ 4
- 3
vue-theme/src/utils/helpers.js Просмотреть файл

@@ -41,18 +41,19 @@ const postTypes = [
41 41
  */
42 42
 const typeFromRoute = route => {
43 43
     let type = route.params.type ? route.params.type : route.fullPath.split('/')
44
-
44
+    
45 45
     if (!route.params.type) {
46 46
         // Remove blank path sections and match to postTypes array
47 47
         type = type
48 48
             .filter(pathSection => pathSection != '')
49 49
             .filter(pathSection => postTypes.includes(pathSection))
50 50
 
51
-        // Only take the first match
51
+            // Only take the first match
52 52
         type = type[0]
53 53
         // console.log(`type derived from route.path: ${type}`)
54 54
     }
55
-
55
+    
56
+    console.log('from route->',type)
56 57
     return type
57 58
 }
58 59
 

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