Kaynağa Gözat

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

tags/1.0.0^2
maeda 3 yıl önce
ebeveyn
işleme
0bcd94518f
1 değiştirilmiş dosya ile 114 ekleme ve 64 silme
  1. 114
    64
      vue-theme/src/pages/list.vue

+ 114
- 64
vue-theme/src/pages/list.vue Dosyayı Görüntüle

59
     },
59
     },
60
     computed: {
60
     computed: {
61
         type() {
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
         hasSubtype() {
69
         hasSubtype() {
65
             return this.$route.query.type
70
             return this.$route.query.type
80
             return !sansSidebarTypes.includes(this.type)
85
             return !sansSidebarTypes.includes(this.type)
81
         },
86
         },
82
         pType() {
87
         pType() {
83
-            if(!this.type) return
88
+            if (!this.type) return
84
             return `${convertTitleCase(this.type)}s`
89
             return `${convertTitleCase(this.type)}s`
85
         },
90
         },
86
         loaded() {
91
         loaded() {
87
             if (!this.pType) return
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
         posts() {
97
         posts() {
91
             if (!this.pType) return
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
         shouldLoadAllAtOnce() {
103
         shouldLoadAllAtOnce() {
95
-            return this.type == 'episode' ||
104
+            return (
105
+                this.type == 'episode' ||
96
                 this.sortBy == sortTypes.subtype ||
106
                 this.sortBy == sortTypes.subtype ||
97
                 this.sortBy == sortTypes.material ||
107
                 this.sortBy == sortTypes.material ||
98
                 this.sortBy == sortTypes.episode
108
                 this.sortBy == sortTypes.episode
99
-        }
109
+            )
110
+        },
100
     },
111
     },
101
     methods: {
112
     methods: {
102
         async loadMoreSearchResults() {
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
             const getPosts = async params => {
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
             try {
121
             try {
108
                 this.loadingFetched = true
122
                 this.loadingFetched = true
109
                 this.page++
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
                 this.loadingFetched = false
129
                 this.loadingFetched = false
118
                 // Stop trying to load more posts
130
                 // Stop trying to load more posts
119
-                if(res && !res.length) {
131
+                if (res && !res.length) {
120
                     this.keepFetching = false
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
         async loadMorePosts(shouldClear) {
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
                 console.log(`Getting more ${this.type} posts`)
149
                 console.log(`Getting more ${this.type} posts`)
131
                 // We always grab all pages on hero init so no need to do it here
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
                 this.$store.commit(`CLEAR_${this.pType.toUpperCase()}`)
160
                 this.$store.commit(`CLEAR_${this.pType.toUpperCase()}`)
140
-                
161
+
141
                 // Clear any state needed to track title inbetweens
162
                 // Clear any state needed to track title inbetweens
142
                 const hasInbetweens = ['artist']
163
                 const hasInbetweens = ['artist']
143
-                if(hasInbetweens.includes(this.type)) {
164
+                if (hasInbetweens.includes(this.type)) {
144
                     this.$store.commit(`CLEAR_${this.pType.toUpperCase()}_SEEN`)
165
                     this.$store.commit(`CLEAR_${this.pType.toUpperCase()}_SEEN`)
145
                 }
166
                 }
146
             }
167
             }
151
                 // console.log('shouldLoadAll :', this.shouldLoadAllAtOnce, this.type)
172
                 // console.log('shouldLoadAll :', this.shouldLoadAllAtOnce, this.type)
152
                 const params = {
173
                 const params = {
153
                     limit: this.shouldLoadAllAtOnce ? -1 : this.perPage,
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
                     params.type = this.$route.query.type
178
                     params.type = this.$route.query.type
158
                 }
179
                 }
159
                 const res = await getPosts(
180
                 const res = await getPosts(
160
                     params,
181
                     params,
161
-                    this.shouldLoadAllAtOnce ? 'All' : 'More'
182
+                    this.shouldLoadAllAtOnce ? 'All' : 'More',
162
                 )
183
                 )
163
                 this.loadingFetched = false
184
                 this.loadingFetched = false
164
 
185
 
165
                 // Stop trying to load more posts
186
                 // Stop trying to load more posts
166
-                if(res && !res.length || this.shouldLoadAllAtOnce) {
187
+                if ((res && !res.length) || this.shouldLoadAllAtOnce) {
167
                     this.keepFetching = false
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
         async getPage(type) {
204
         async getPage(type) {
174
             await this._getAllIfNotLoaded('page', this.$store)
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
             const page = this.allPages.filter(page => page.slug == `${type}`)[0]
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
             return page
209
             return page
179
         },
210
         },
180
         // _setHeroInfo(post) {} from mixin
211
         // _setHeroInfo(post) {} from mixin
191
                 heroJson.subtype = this.$route.query.type
222
                 heroJson.subtype = this.$route.query.type
192
                 console.log(heroJson)
223
                 console.log(heroJson)
193
                 this.$store.commit('SET_HERO', heroJson)
224
                 this.$store.commit('SET_HERO', heroJson)
194
-            } catch (err) { console.error(err) }
225
+            } catch (err) {
226
+                console.error(err)
227
+            }
195
         },
228
         },
196
         setIntersectionLoader(cb) {
229
         setIntersectionLoader(cb) {
197
             // KeepFetching is UNSET for certain post types and sort types in `loadMorePosts()`
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
             // Always unset before setting the intersection loader
236
             // Always unset before setting the intersection loader
201
             this.unsetIntersectionLoader()
237
             this.unsetIntersectionLoader()
202
 
238
 
203
             // console.warn('Setting interesection loader...')
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
             this.observer.observe(document.querySelector(INTERSECT_SELECTOR))
249
             this.observer.observe(document.querySelector(INTERSECT_SELECTOR))
211
         },
250
         },
212
         unsetIntersectionLoader() {
251
         unsetIntersectionLoader() {
213
             const footerEl = document.querySelector(INTERSECT_SELECTOR)
252
             const footerEl = document.querySelector(INTERSECT_SELECTOR)
214
             try {
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
                 this.observer.unobserve(footerEl)
257
                 this.observer.unobserve(footerEl)
218
                 this.observer.disconnect()
258
                 this.observer.disconnect()
219
-            } catch (error) { console.error(error) }
259
+            } catch (error) {
260
+                console.error(error)
261
+            }
220
         },
262
         },
221
         initPostList() {
263
         initPostList() {
222
             this.page = 0
264
             this.page = 0
223
             this.keepFetching = true
265
             this.keepFetching = true
224
 
266
 
225
-            if(this.isSearch) {
267
+            if (this.isSearch) {
226
                 this.checkAndSetHero(`search`)
268
                 this.checkAndSetHero(`search`)
227
                 this.$store.commit(`CLEAR_SEARCH_RESULTS`)
269
                 this.$store.commit(`CLEAR_SEARCH_RESULTS`)
228
                 this.loadMoreSearchResults()
270
                 this.loadMoreSearchResults()
234
                 this.loadMorePosts(true)
276
                 this.loadMorePosts(true)
235
                 this.setIntersectionLoader(this.loadMorePosts)
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
     watch: {
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
         // This only fires navigating from a list page, to another list page
297
         // This only fires navigating from a list page, to another list page
249
         type(newType) {
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
             // Ignore some types with presorts so the sortBy watcher can handle them
302
             // Ignore some types with presorts so the sortBy watcher can handle them
253
             const ignore = ['artist']
303
             const ignore = ['artist']
254
-            if(ignore.includes(newType)) {
304
+            if (ignore.includes(newType)) {
255
                 return
305
                 return
256
             }
306
             }
257
             // Reset the search term betweem loads
307
             // Reset the search term betweem loads
258
-            // so we initPostList() works correctly when 
308
+            // so we initPostList() works correctly when
259
             // navigating from search result to a type list page
309
             // navigating from search result to a type list page
260
             this.searchTerm = this.$route.query.s
310
             this.searchTerm = this.$route.query.s
261
             this.initPostList()
311
             this.initPostList()
262
         },
312
         },
263
         // Fires when navigating between pages with different sorts
313
         // Fires when navigating between pages with different sorts
264
         sortBy(newSort) {
314
         sortBy(newSort) {
265
-            if(!Object.values(sortTypes).includes(newSort)) return
315
+            if (!Object.values(sortTypes).includes(newSort)) return
266
             this.initPostList()
316
             this.initPostList()
267
         },
317
         },
268
     },
318
     },
272
     },
322
     },
273
     beforeUnmount() {
323
     beforeUnmount() {
274
         this.unsetIntersectionLoader()
324
         this.unsetIntersectionLoader()
275
-    }
325
+    },
276
 }
326
 }
277
 </script>
327
 </script>
278
 
328
 

Loading…
İptal
Kaydet