Pārlūkot izejas kodu

:fire: really slimming down post list template

tags/0.9.0
J 4 gadus atpakaļ
vecāks
revīzija
0250e6ea97

+ 34
- 45
vue-theme/src/pages/list.vue Parādīt failu

33
 import sidebar from '@/components/sidebars/sidebar'
33
 import sidebar from '@/components/sidebars/sidebar'
34
 import { postTypeGetters, scrollTop, heroUtils } from './mixin-post-types'
34
 import { postTypeGetters, scrollTop, heroUtils } from './mixin-post-types'
35
 
35
 
36
-import { postTypes, sortTypes, convertTitleCase, typeFromRoute, sortFromRoute } from '@/utils/helpers'
36
+import { postTypes, sortTypes, convertTitleCase } from '@/utils/helpers'
37
 
37
 
38
 const TIMEOUT = 1
38
 const TIMEOUT = 1
39
 const INTERSECT_SELECTOR = '.page--list > article footer'
39
 const INTERSECT_SELECTOR = '.page--list > article footer'
84
             if (!this.pType) return
84
             if (!this.pType) return
85
             return this[`all${this.pType}`]
85
             return this[`all${this.pType}`]
86
         },
86
         },
87
+        shouldLoadAllAtOnce() {
88
+            return this.type == 'episode' || this.type == 'artist' && this.sortBy == sortTypes.material
89
+        }
87
     },
90
     },
88
     methods: {
91
     methods: {
89
-        clearAllPosts() {
90
-            if(!this.type) return console.error(`post type: ${this.type} not found...`)
91
-            this.$store.commit(`CLEAR_${this.pType.toUpperCase()}`)
92
-            // this.$store.commit(`${this.type.toUpperCase()}S_LOADED`)
93
-        },
94
-        async loadMorePosts() {
92
+        async loadMorePosts(shouldClear) {
93
+            if(shouldClear) this.$store.commit(`CLEAR_${this.pType.toUpperCase()}`)
95
             if(!this.keepFetching) return console.warn('nothing left to fetch...')
94
             if(!this.keepFetching) return console.warn('nothing left to fetch...')
96
 
95
 
97
             try {
96
             try {
98
                 this.loadingFetched = true
97
                 this.loadingFetched = true
99
-                // console.warn(`loading page ${this.page + 1} of ${type} posts: ${this.page * this.perPage + 1} to ${(this.page + 1) * this.perPage}`)
100
                 this.page++
98
                 this.page++
101
-                await this.getPosts()
99
+
100
+                const res = await this.getPosts(
101
+                    {
102
+                        limit: this.shouldLoadAllAtOnce ? -1 : this.perPage,
103
+                        page: this.page
104
+                    },
105
+                    this.shouldLoadAllAtOnce ? 'All' : 'More'
106
+                )
107
+
102
                 this.loadingFetched = false
108
                 this.loadingFetched = false
103
-            } catch (err) {
104
-                console.error(err)
105
-            }
109
+                
110
+                // Stop trying to load more posts
111
+                if(res && !res.length || this.shouldLoadAllAtOnce) {
112
+                    this.keepFetching = false
113
+                    if(!res.length) console.warn(`Empty response for ${this.type}:`, res.length)
114
+                    if(this.shouldLoadAllAtOnce) console.warn(`Fetched all responses for ${this.type}:`, res.length)
115
+                }
116
+            } catch (err) { console.error(err) }
106
         },
117
         },
107
-        async getPosts() {
118
+        async getPosts(params, dispatchType) {
108
             if(!this.type) throw `post type: ${this.type} not found...`
119
             if(!this.type) throw `post type: ${this.type} not found...`
109
-
110
-            // Set some default dispatch paramters
111
-            let dispatchType = 'More'
112
-            let params = { limit: this.perPage, page: this.page }
113
             
120
             
114
-            // For episodes, or material sort we grab EVERYTHING
115
-            const getAllClause = this.type == 'episode' || this.type == 'artist' && this.sortBy == sortTypes.material
116
-            if(getAllClause) {
117
-                // dispatchType = 'All'
118
-                params.limit = -1
119
-            }
120
-
121
             let res = []
121
             let res = []
122
             // We always grab all pages on hero init so no need to do it here
122
             // We always grab all pages on hero init so no need to do it here
123
             if(this.pType && this.keepFetching && this.type != 'page') {
123
             if(this.pType && this.keepFetching && this.type != 'page') {
126
                     { sortType: this.sortBy, params }
126
                     { sortType: this.sortBy, params }
127
                 )
127
                 )
128
             }
128
             }
129
-
130
-            // Stop trying to load more posts
131
-            if(res && !res.length || getAllClause) {
132
-                this.keepFetching = false
133
-                if(!res.length) console.warn(`Empty response for ${this.type}:`, res.length)
134
-                if(getAllClause) console.warn(`Fetched all responses for ${this.type}:`, res.length)
135
-            }
129
+            return res
136
         },
130
         },
137
-        async getPageForType(type) {
138
-            await this._getAll('page', this.$store)
131
+        async getPage(type) {
132
+            await this._getAllIfNotLoaded('page', this.$store)
139
             if(!this.allPages) throw 'no pages in state'
133
             if(!this.allPages) throw 'no pages in state'
140
             const page = this.allPages.filter(page => page.slug == `${type}s`)[0]
134
             const page = this.allPages.filter(page => page.slug == `${type}s`)[0]
141
             if(!page) throw `No page: ${type} found. Cannot set hero.`
135
             if(!page) throw `No page: ${type} found. Cannot set hero.`
146
         async checkAndSetHero(type) {
140
         async checkAndSetHero(type) {
147
             this._clearHero(this.$store)
141
             this._clearHero(this.$store)
148
             try {
142
             try {
149
-                const page = await this.getPageForType(type)
143
+                const page = await this.getPage(type)
150
                 // We always set a hero no matter what
144
                 // We always set a hero no matter what
151
                 // Because the hero component will deal
145
                 // Because the hero component will deal
152
                 // with how to render based on hero.url
146
                 // with how to render based on hero.url
178
                 if(!this.observer) throw `cannot unset intersection handler missing observer: ${this.observer}`
172
                 if(!this.observer) throw `cannot unset intersection handler missing observer: ${this.observer}`
179
                 this.observer.unobserve(footerEl)
173
                 this.observer.unobserve(footerEl)
180
                 this.observer.disconnect()
174
                 this.observer.disconnect()
181
-            } catch (error) {
182
-                console.error(error)
183
-            }
175
+            } catch (error) { console.error(error) }
184
         },
176
         },
185
-        clearAndInitPostList(caller) {
177
+        initPostList() {
186
             this.page = 0
178
             this.page = 0
187
             this.keepFetching = true
179
             this.keepFetching = true
188
 
180
 
189
             this.checkAndSetHero(this.type)
181
             this.checkAndSetHero(this.type)
190
 
182
 
191
-            // Fires when loading from home
192
             // Clear any preloaded posts (from home, etc.)
183
             // Clear any preloaded posts (from home, etc.)
193
-            this.clearAllPosts()
194
-            this.loadMorePosts()
195
-
184
+            this.loadMorePosts(true)
196
             this.setIntersectionLoader()
185
             this.setIntersectionLoader()
197
         }
186
         }
198
     },
187
     },
206
             // Ignore types with presorts so the sortBy watcher can handle them
195
             // Ignore types with presorts so the sortBy watcher can handle them
207
             const ignore = ['event', 'exhibition', 'artist']
196
             const ignore = ['event', 'exhibition', 'artist']
208
             if(ignore.includes(newType)) return
197
             if(ignore.includes(newType)) return
209
-            this.clearAndInitPostList('type change')
198
+            this.initPostList()
210
         },
199
         },
211
         sortBy(newSort) {
200
         sortBy(newSort) {
212
             if(!Object.values(sortTypes).includes(newSort)) return
201
             if(!Object.values(sortTypes).includes(newSort)) return
213
-            this.clearAndInitPostList('sort change')
202
+            this.initPostList()
214
         }
203
         }
215
     },
204
     },
216
     mounted() {
205
     mounted() {
217
         // This only fires navigating from a non-list page > list page
206
         // This only fires navigating from a non-list page > list page
218
-        this.clearAndInitPostList('mounted')
207
+        this.initPostList()
219
     },
208
     },
220
     beforeDestroy() {
209
     beforeDestroy() {
221
         this.unsetIntersectionLoader()
210
         this.unsetIntersectionLoader()

+ 1
- 1
vue-theme/src/pages/mixin-post-types.js Parādīt failu

63
         _clearHero(store) {
63
         _clearHero(store) {
64
             store.commit('SET_HERO', { url: null, heroType: null })
64
             store.commit('SET_HERO', { url: null, heroType: null })
65
         },
65
         },
66
-        async _getAll(type, store) {
66
+        async _getAllIfNotLoaded(type, store) {
67
             const capitalized = convertTitleCase(type) + 's'
67
             const capitalized = convertTitleCase(type) + 's'
68
             if (!this[`all${capitalized}Loaded`]) {
68
             if (!this[`all${capitalized}Loaded`]) {
69
                 await store.dispatch(`getAll${capitalized}`, {
69
                 await store.dispatch(`getAll${capitalized}`, {

+ 2
- 4
vue-theme/src/pages/single.vue Parādīt failu

47
 
47
 
48
 import { postTypeGetters, scrollTop, heroUtils } from './mixin-post-types'
48
 import { postTypeGetters, scrollTop, heroUtils } from './mixin-post-types'
49
 
49
 
50
-import { postTypes, sortTypes, convertTitleCase, dePluralize, typeFromRoute, formatDate } from '@/utils/helpers'
50
+import { convertTitleCase, formatDate } from '@/utils/helpers'
51
 
51
 
52
 const TIMEOUT = 1
52
 const TIMEOUT = 1
53
 
53
 
225
         },
225
         },
226
     },
226
     },
227
     watch: {
227
     watch: {
228
-        // This fires navigating to and away
228
+        // ONLY load post data when navigating TO a single page
229
         slug(newSlug, oldSlug) {
229
         slug(newSlug, oldSlug) {
230
-            // Only load post data when navigating TO a single page
231
-            console.log('single :', newSlug, oldSlug)
232
             if(newSlug && !oldSlug) {
230
             if(newSlug && !oldSlug) {
233
                 this._clearHero(this.$store)
231
                 this._clearHero(this.$store)
234
                 this.loadPostData()
232
                 this.loadPostData()

+ 3
- 12
vue-theme/src/router/routes.js Parādīt failu

2
 import listPage from '@/pages/list.vue'
2
 import listPage from '@/pages/list.vue'
3
 import singlePage from '@/pages/single.vue'
3
 import singlePage from '@/pages/single.vue'
4
 
4
 
5
-import { sortTypes } from '@/utils/helpers'
6
-
7
-const gridWithSidebar = { sidebar: true, grid: true }
8
-const gridSansSidebar = { sidebar: false, grid: true }
9
-const sansGridWithSidebar = { sidebar: true, grid: false, isWide: true }
10
-
11
 export default [
5
 export default [
12
     // Home Page
6
     // Home Page
13
     { path: '/', component: indexPage },
7
     { path: '/', component: indexPage },
8
+
14
     // List Pages
9
     // List Pages
15
-    {
16
-        path: '/:type',
17
-        component: listPage,
18
-        props: gridWithSidebar,
19
-    },
10
+    { path: '/:type', component: listPage },
20
     {
11
     {
21
         path: '/:type/sorted/:sortBy',
12
         path: '/:type/sorted/:sortBy',
22
         component: listPage,
13
         component: listPage,
23
-        props: gridWithSidebar,
24
     },
14
     },
15
+    
25
     // Single Pages
16
     // Single Pages
26
     {
17
     {
27
         path: '/:type/:slug',
18
         path: '/:type/:slug',

+ 0
- 22
vue-theme/src/utils/helpers.js Parādīt failu

30
     'page',
30
     'page',
31
 ]
31
 ]
32
 
32
 
33
-/**
34
- * Type assigned from Route :type
35
- * In case of failure, tries to derive type
36
- * matching pieces to postTypes array
37
- */
38
-const typeFromRoute = route => {
39
-    let type = route.params.type ? route.params.type : route.fullPath.split('/')
40
-    
41
-    if (!route.params.type) {
42
-        // Remove blank path sections and match to postTypes array
43
-        type = type.filter(pathSection => pathSection && postTypes.includes(pathSection))
44
-
45
-        // Only take the first match
46
-        type = type[0]
47
-        // console.log(`type derived from route.pajth: ${type}`)
48
-    }
49
-    // console.log('type from route:',type)
50
-    return type
51
-}
52
-
53
-
54
 const ytThumbnail = (url, desiredSize) => {
33
 const ytThumbnail = (url, desiredSize) => {
55
     const remove = [
34
     const remove = [
56
         '',
35
         '',
92
 
71
 
93
 export {
72
 export {
94
     convertTitleCase,
73
     convertTitleCase,
95
-    typeFromRoute,
96
     sortTypes,
74
     sortTypes,
97
     postTypes,
75
     postTypes,
98
     ytThumbnail,
76
     ytThumbnail,

Notiek ielāde…
Atcelt
Saglabāt