Przeglądaj źródła

Merge branch 'searchfix' of craft-in-america/vue-wp into dev

tags/1.0.0^2
maeda 3 lat temu
rodzic
commit
0bcd94518f
1 zmienionych plików z 114 dodań i 64 usunięć
  1. 114
    64
      vue-theme/src/pages/list.vue

+ 114
- 64
vue-theme/src/pages/list.vue Wyświetl plik

@@ -59,7 +59,12 @@ export default {
59 59
     },
60 60
     computed: {
61 61
         type() {
62
-            return postTypes.includes(this.$route.params.type) ? this.$route.params.type : 'post'
62
+            return postTypes.includes(this.$route.params.type)
63
+                ? this.$route.params.type
64
+                : 'post'
65
+        },
66
+        hasSearchTerm() {
67
+            return this.$route.query.s
63 68
         },
64 69
         hasSubtype() {
65 70
             return this.$route.query.type
@@ -80,67 +85,83 @@ export default {
80 85
             return !sansSidebarTypes.includes(this.type)
81 86
         },
82 87
         pType() {
83
-            if(!this.type) return
88
+            if (!this.type) return
84 89
             return `${convertTitleCase(this.type)}s`
85 90
         },
86 91
         loaded() {
87 92
             if (!this.pType) return
88
-            return this.isSearch ? this['allSearchResultsLoaded'] : this[`all${this.pType}Loaded`]
93
+            return this.isSearch
94
+                ? this['allSearchResultsLoaded']
95
+                : this[`all${this.pType}Loaded`]
89 96
         },
90 97
         posts() {
91 98
             if (!this.pType) return
92
-            return this.isSearch ? this['allSearchResults'] : this[`all${this.pType}`]
99
+            return this.isSearch
100
+                ? this['allSearchResults']
101
+                : this[`all${this.pType}`]
93 102
         },
94 103
         shouldLoadAllAtOnce() {
95
-            return this.type == 'episode' ||
104
+            return (
105
+                this.type == 'episode' ||
96 106
                 this.sortBy == sortTypes.subtype ||
97 107
                 this.sortBy == sortTypes.material ||
98 108
                 this.sortBy == sortTypes.episode
99
-        }
109
+            )
110
+        },
100 111
     },
101 112
     methods: {
102 113
         async loadMoreSearchResults() {
103
-            if(!this.keepFetching) return console.warn('Nothing left to fetch...')
114
+            if (!this.keepFetching)
115
+                return console.warn('Nothing left to fetch...')
104 116
             const getPosts = async params => {
105
-                return await this.$store.dispatch(`getMoreSearchResults`, { params })
117
+                return await this.$store.dispatch(`getMoreSearchResults`, {
118
+                    params,
119
+                })
106 120
             }
107 121
             try {
108 122
                 this.loadingFetched = true
109 123
                 this.page++
110
-                const res = await getPosts(
111
-                    {
112
-                        limit: this.perPage,
113
-                        page: this.page,
114
-                        s: this.searchTerm
115
-                    }
116
-                )
124
+                const res = await getPosts({
125
+                    limit: this.perPage,
126
+                    page: this.page,
127
+                    s: this.searchTerm,
128
+                })
117 129
                 this.loadingFetched = false
118 130
                 // Stop trying to load more posts
119
-                if(res && !res.length) {
131
+                if (res && !res.length) {
120 132
                     this.keepFetching = false
121
-                    if(!res.length) console.warn(`Empty response for search results:`, res.length)
133
+                    if (!res.length)
134
+                        console.warn(
135
+                            `Empty response for search results:`,
136
+                            res.length,
137
+                        )
122 138
                 }
123
-            } catch (err) { console.error(err) }
139
+            } catch (err) {
140
+                console.error(err)
141
+            }
124 142
         },
125 143
         async loadMorePosts(shouldClear) {
126
-            if(!this.keepFetching) return console.warn('Nothing left to fetch...')
144
+            if (!this.keepFetching)
145
+                return console.warn('Nothing left to fetch...')
127 146
 
128
-            const getPosts = async  (params, dispatchType) => {
129
-                if(!this.type) throw `post type: ${this.type} not found...`
147
+            const getPosts = async (params, dispatchType) => {
148
+                if (!this.type) throw `post type: ${this.type} not found...`
130 149
                 console.log(`Getting more ${this.type} posts`)
131 150
                 // We always grab all pages on hero init so no need to do it here
132
-                return this.pType && this.type != 'page' ? await this.$store.dispatch(
133
-                    `get${dispatchType}${this.pType}`,
134
-                    { sortType: this.sortBy, params }
135
-                ) : []
151
+                return this.pType && this.type != 'page'
152
+                    ? await this.$store.dispatch(
153
+                          `get${dispatchType}${this.pType}`,
154
+                          { sortType: this.sortBy, params },
155
+                      )
156
+                    : []
136 157
             }
137 158
 
138
-            if(shouldClear) {
159
+            if (shouldClear) {
139 160
                 this.$store.commit(`CLEAR_${this.pType.toUpperCase()}`)
140
-                
161
+
141 162
                 // Clear any state needed to track title inbetweens
142 163
                 const hasInbetweens = ['artist']
143
-                if(hasInbetweens.includes(this.type)) {
164
+                if (hasInbetweens.includes(this.type)) {
144 165
                     this.$store.commit(`CLEAR_${this.pType.toUpperCase()}_SEEN`)
145 166
                 }
146 167
             }
@@ -151,30 +172,40 @@ export default {
151 172
                 // console.log('shouldLoadAll :', this.shouldLoadAllAtOnce, this.type)
152 173
                 const params = {
153 174
                     limit: this.shouldLoadAllAtOnce ? -1 : this.perPage,
154
-                    page: this.page
175
+                    page: this.page,
155 176
                 }
156
-                if(this.hasSubtype) {
177
+                if (this.hasSubtype) {
157 178
                     params.type = this.$route.query.type
158 179
                 }
159 180
                 const res = await getPosts(
160 181
                     params,
161
-                    this.shouldLoadAllAtOnce ? 'All' : 'More'
182
+                    this.shouldLoadAllAtOnce ? 'All' : 'More',
162 183
                 )
163 184
                 this.loadingFetched = false
164 185
 
165 186
                 // Stop trying to load more posts
166
-                if(res && !res.length || this.shouldLoadAllAtOnce) {
187
+                if ((res && !res.length) || this.shouldLoadAllAtOnce) {
167 188
                     this.keepFetching = false
168
-                    if(!res.length) console.warn(`Empty response for ${this.type}:`, res.length)
169
-                    if(this.shouldLoadAllAtOnce) console.warn(`Fetched all responses for ${this.type}:`, res.length)
189
+                    if (!res.length)
190
+                        console.warn(
191
+                            `Empty response for ${this.type}:`,
192
+                            res.length,
193
+                        )
194
+                    if (this.shouldLoadAllAtOnce)
195
+                        console.warn(
196
+                            `Fetched all responses for ${this.type}:`,
197
+                            res.length,
198
+                        )
170 199
                 }
171
-            } catch (err) { console.error(err) }
200
+            } catch (err) {
201
+                console.error(err)
202
+            }
172 203
         },
173 204
         async getPage(type) {
174 205
             await this._getAllIfNotLoaded('page', this.$store)
175
-            if(!this.allPages) throw 'no pages in state'
206
+            if (!this.allPages) throw 'no pages in state'
176 207
             const page = this.allPages.filter(page => page.slug == `${type}`)[0]
177
-            if(!page) throw `No page: ${type} found. Cannot set hero.`
208
+            if (!page) throw `No page: ${type} found. Cannot set hero.`
178 209
             return page
179 210
         },
180 211
         // _setHeroInfo(post) {} from mixin
@@ -191,38 +222,49 @@ export default {
191 222
                 heroJson.subtype = this.$route.query.type
192 223
                 console.log(heroJson)
193 224
                 this.$store.commit('SET_HERO', heroJson)
194
-            } catch (err) { console.error(err) }
225
+            } catch (err) {
226
+                console.error(err)
227
+            }
195 228
         },
196 229
         setIntersectionLoader(cb) {
197 230
             // KeepFetching is UNSET for certain post types and sort types in `loadMorePosts()`
198
-            if(!this.keepFetching) return console.warn('Cannot setup intersection handler keepFetching is set false')
199
-            
231
+            if (!this.keepFetching)
232
+                return console.warn(
233
+                    'Cannot setup intersection handler keepFetching is set false',
234
+                )
235
+
200 236
             // Always unset before setting the intersection loader
201 237
             this.unsetIntersectionLoader()
202 238
 
203 239
             // console.warn('Setting interesection loader...')
204
-            this.observer = new IntersectionObserver(entries => {
205
-                entries.forEach(entry => {
206
-                    if (!entry.isIntersecting || this.loadingFetched) return
207
-                    setTimeout(cb, TIMEOUT)
208
-                })
209
-            }, { threshold: 0.80 })
240
+            this.observer = new IntersectionObserver(
241
+                entries => {
242
+                    entries.forEach(entry => {
243
+                        if (!entry.isIntersecting || this.loadingFetched) return
244
+                        setTimeout(cb, TIMEOUT)
245
+                    })
246
+                },
247
+                { threshold: 0.8 },
248
+            )
210 249
             this.observer.observe(document.querySelector(INTERSECT_SELECTOR))
211 250
         },
212 251
         unsetIntersectionLoader() {
213 252
             const footerEl = document.querySelector(INTERSECT_SELECTOR)
214 253
             try {
215
-                if(!footerEl) throw `Cannot unset intersection handler missing el: ${footerEl}`
216
-                if(!this.observer) return 
254
+                if (!footerEl)
255
+                    throw `Cannot unset intersection handler missing el: ${footerEl}`
256
+                if (!this.observer) return
217 257
                 this.observer.unobserve(footerEl)
218 258
                 this.observer.disconnect()
219
-            } catch (error) { console.error(error) }
259
+            } catch (error) {
260
+                console.error(error)
261
+            }
220 262
         },
221 263
         initPostList() {
222 264
             this.page = 0
223 265
             this.keepFetching = true
224 266
 
225
-            if(this.isSearch) {
267
+            if (this.isSearch) {
226 268
                 this.checkAndSetHero(`search`)
227 269
                 this.$store.commit(`CLEAR_SEARCH_RESULTS`)
228 270
                 this.loadMoreSearchResults()
@@ -234,35 +276,43 @@ export default {
234 276
                 this.loadMorePosts(true)
235 277
                 this.setIntersectionLoader(this.loadMorePosts)
236 278
             }
237
-        }
238
-    },
239
-    beforeRouteUpdate(to, from) {
240
-        // !: Only fires between two searches
241
-        if(!to.query || to.query.s == from.query.s) return
242
-        console.log('between route search')
243
-        // Set the search term for impending callback fire
244
-        this.searchTerm = to.query.s
245
-        this.initPostList()
279
+        },
246 280
     },
247 281
     watch: {
282
+        // Fires no matter what
283
+        hasSearchTerm(newTerm, oldTerm) {
284
+            this.searchTerm = newTerm
285
+            if (newTerm && newTerm != oldTerm) {
286
+                this.initPostList()
287
+            }
288
+        },
289
+        hasSubtype(newSubtype, oldSubtype) {
290
+            if (newSubtype && newSubtype != oldSubtype) {
291
+                this.initPostList()
292
+            }
293
+            if (!newSubtype && oldSubtype) {
294
+                this.initPostList()
295
+            }
296
+        },
248 297
         // This only fires navigating from a list page, to another list page
249 298
         type(newType) {
250
-            if(!postTypes.includes(newType)) return console.warn('Type not valid...')
299
+            if (!postTypes.includes(newType))
300
+                return console.warn('Type not valid...')
251 301
 
252 302
             // Ignore some types with presorts so the sortBy watcher can handle them
253 303
             const ignore = ['artist']
254
-            if(ignore.includes(newType)) {
304
+            if (ignore.includes(newType)) {
255 305
                 return
256 306
             }
257 307
             // Reset the search term betweem loads
258
-            // so we initPostList() works correctly when 
308
+            // so we initPostList() works correctly when
259 309
             // navigating from search result to a type list page
260 310
             this.searchTerm = this.$route.query.s
261 311
             this.initPostList()
262 312
         },
263 313
         // Fires when navigating between pages with different sorts
264 314
         sortBy(newSort) {
265
-            if(!Object.values(sortTypes).includes(newSort)) return
315
+            if (!Object.values(sortTypes).includes(newSort)) return
266 316
             this.initPostList()
267 317
         },
268 318
     },
@@ -272,7 +322,7 @@ export default {
272 322
     },
273 323
     beforeUnmount() {
274 324
         this.unsetIntersectionLoader()
275
-    }
325
+    },
276 326
 }
277 327
 </script>
278 328
 

Ładowanie…
Anuluj
Zapisz