Sfoglia il codice sorgente

:fire: really slimming down post list template

tags/0.9.0
J 4 anni fa
parent
commit
0250e6ea97

+ 34
- 45
vue-theme/src/pages/list.vue Vedi File

@@ -33,7 +33,7 @@ import card from '@/components/card'
33 33
 import sidebar from '@/components/sidebars/sidebar'
34 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 38
 const TIMEOUT = 1
39 39
 const INTERSECT_SELECTOR = '.page--list > article footer'
@@ -84,40 +84,40 @@ export default {
84 84
             if (!this.pType) return
85 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 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 94
             if(!this.keepFetching) return console.warn('nothing left to fetch...')
96 95
 
97 96
             try {
98 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 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 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 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 121
             let res = []
122 122
             // We always grab all pages on hero init so no need to do it here
123 123
             if(this.pType && this.keepFetching && this.type != 'page') {
@@ -126,16 +126,10 @@ export default {
126 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 133
             if(!this.allPages) throw 'no pages in state'
140 134
             const page = this.allPages.filter(page => page.slug == `${type}s`)[0]
141 135
             if(!page) throw `No page: ${type} found. Cannot set hero.`
@@ -146,7 +140,7 @@ export default {
146 140
         async checkAndSetHero(type) {
147 141
             this._clearHero(this.$store)
148 142
             try {
149
-                const page = await this.getPageForType(type)
143
+                const page = await this.getPage(type)
150 144
                 // We always set a hero no matter what
151 145
                 // Because the hero component will deal
152 146
                 // with how to render based on hero.url
@@ -178,21 +172,16 @@ export default {
178 172
                 if(!this.observer) throw `cannot unset intersection handler missing observer: ${this.observer}`
179 173
                 this.observer.unobserve(footerEl)
180 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 178
             this.page = 0
187 179
             this.keepFetching = true
188 180
 
189 181
             this.checkAndSetHero(this.type)
190 182
 
191
-            // Fires when loading from home
192 183
             // Clear any preloaded posts (from home, etc.)
193
-            this.clearAllPosts()
194
-            this.loadMorePosts()
195
-
184
+            this.loadMorePosts(true)
196 185
             this.setIntersectionLoader()
197 186
         }
198 187
     },
@@ -206,16 +195,16 @@ export default {
206 195
             // Ignore types with presorts so the sortBy watcher can handle them
207 196
             const ignore = ['event', 'exhibition', 'artist']
208 197
             if(ignore.includes(newType)) return
209
-            this.clearAndInitPostList('type change')
198
+            this.initPostList()
210 199
         },
211 200
         sortBy(newSort) {
212 201
             if(!Object.values(sortTypes).includes(newSort)) return
213
-            this.clearAndInitPostList('sort change')
202
+            this.initPostList()
214 203
         }
215 204
     },
216 205
     mounted() {
217 206
         // This only fires navigating from a non-list page > list page
218
-        this.clearAndInitPostList('mounted')
207
+        this.initPostList()
219 208
     },
220 209
     beforeDestroy() {
221 210
         this.unsetIntersectionLoader()

+ 1
- 1
vue-theme/src/pages/mixin-post-types.js Vedi File

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

+ 2
- 4
vue-theme/src/pages/single.vue Vedi File

@@ -47,7 +47,7 @@ import breadcrumb from '@/components/breadcrumb'
47 47
 
48 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 52
 const TIMEOUT = 1
53 53
 
@@ -225,10 +225,8 @@ export default {
225 225
         },
226 226
     },
227 227
     watch: {
228
-        // This fires navigating to and away
228
+        // ONLY load post data when navigating TO a single page
229 229
         slug(newSlug, oldSlug) {
230
-            // Only load post data when navigating TO a single page
231
-            console.log('single :', newSlug, oldSlug)
232 230
             if(newSlug && !oldSlug) {
233 231
                 this._clearHero(this.$store)
234 232
                 this.loadPostData()

+ 3
- 12
vue-theme/src/router/routes.js Vedi File

@@ -2,26 +2,17 @@ import indexPage from '@/pages/index.vue'
2 2
 import listPage from '@/pages/list.vue'
3 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 5
 export default [
12 6
     // Home Page
13 7
     { path: '/', component: indexPage },
8
+
14 9
     // List Pages
15
-    {
16
-        path: '/:type',
17
-        component: listPage,
18
-        props: gridWithSidebar,
19
-    },
10
+    { path: '/:type', component: listPage },
20 11
     {
21 12
         path: '/:type/sorted/:sortBy',
22 13
         component: listPage,
23
-        props: gridWithSidebar,
24 14
     },
15
+    
25 16
     // Single Pages
26 17
     {
27 18
         path: '/:type/:slug',

+ 0
- 22
vue-theme/src/utils/helpers.js Vedi File

@@ -30,27 +30,6 @@ const postTypes = [
30 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 33
 const ytThumbnail = (url, desiredSize) => {
55 34
     const remove = [
56 35
         '',
@@ -92,7 +71,6 @@ const formatDate = (unix, includeTime) => {
92 71
 
93 72
 export {
94 73
     convertTitleCase,
95
-    typeFromRoute,
96 74
     sortTypes,
97 75
     postTypes,
98 76
     ytThumbnail,

Loading…
Annulla
Salva