NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

list.vue 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template lang="pug">
  2. .page--list.f-col.between
  3. article.f-grow
  4. header.center.t-up
  5. .title.f-row
  6. h3 {{ type }} list
  7. span(v-if="sortBy")
  8. h3 &nbsp;sorted {{ sortBy.replace('-', ' ') }}
  9. h3(v-if="!loaded") loading...
  10. .content(
  11. v-else-if="allPagesLoaded && type && allPages[type]"
  12. v-html="allPages[type].content"
  13. )
  14. ul.posts.f-col(v-if="posts && loaded" :class="{ 'is-grid': grid }")
  15. li(v-for="(post, i) in posts" :key="post.slug").post.shadow
  16. card(:content="post" :type="type" :wide="isWide")
  17. //- Important: Do NOT remove this! Required for intersection observer
  18. footer
  19. p(v-if="loadingFetched") loading more {{ type }}...
  20. p(v-if="showMeta") {{ `${type} count: ${Object.values(posts).length}` }}
  21. p(v-if="showMeta") {{ `show sidebar: ${sidebar}` }}
  22. sidebar(v-if="sidebar" :type="`${type}`" layout="list")
  23. </template>
  24. <script>
  25. import featuredImage from '@/components/featured-image'
  26. import card from '@/components/card'
  27. import sidebar from '@/components/sidebars/sidebar'
  28. import { postTypeGetters, scrollTop, heroUtils } from './mixin-post-types'
  29. import { postTypes, sortTypes, convertTitleCase } from '@/utils/helpers'
  30. const TIMEOUT = 1
  31. const INTERSECT_SELECTOR = '.page--list > article footer'
  32. const wideTypes = ['event', 'exhibition']
  33. const gridTypes = ['episode', 'artist']
  34. const sansSidebarTypes = ['episode']
  35. export default {
  36. components: { sidebar, featuredImage, card },
  37. mixins: [postTypeGetters, scrollTop, heroUtils],
  38. data() {
  39. return {
  40. showMeta:false,
  41. page: 0,
  42. perPage: 21,
  43. keepFetching: true,
  44. loadingFetched: false,
  45. observer: null
  46. }
  47. },
  48. computed: {
  49. grid() {
  50. return gridTypes.includes(this.type)
  51. },
  52. isWide() {
  53. return wideTypes.includes(this.type)
  54. },
  55. sidebar() {
  56. return !sansSidebarTypes.includes(this.type)
  57. },
  58. type() {
  59. // Checks for type and fixes Episodes route edge case
  60. return this.$route.params.type
  61. },
  62. sortBy() {
  63. return this.$route.params.sortBy
  64. },
  65. pType() {
  66. if(!this.type) return
  67. return `${convertTitleCase(this.type)}s`
  68. },
  69. loaded() {
  70. if (!this.pType) return
  71. return this[`all${this.pType}Loaded`]
  72. },
  73. posts() {
  74. if (!this.pType) return
  75. return this[`all${this.pType}`]
  76. },
  77. shouldLoadAllAtOnce() {
  78. return this.type == 'episode' || this.type == 'artist' && this.sortBy == sortTypes.material
  79. }
  80. },
  81. methods: {
  82. async loadMorePosts(shouldClear) {
  83. if(shouldClear) this.$store.commit(`CLEAR_${this.pType.toUpperCase()}`)
  84. if(!this.keepFetching) return console.warn('nothing left to fetch...')
  85. try {
  86. this.loadingFetched = true
  87. this.page++
  88. const res = await this.getPosts(
  89. {
  90. limit: this.shouldLoadAllAtOnce ? -1 : this.perPage,
  91. page: this.page
  92. },
  93. this.shouldLoadAllAtOnce ? 'All' : 'More'
  94. )
  95. this.loadingFetched = false
  96. // Stop trying to load more posts
  97. if(res && !res.length || this.shouldLoadAllAtOnce) {
  98. this.keepFetching = false
  99. if(!res.length) console.warn(`Empty response for ${this.type}:`, res.length)
  100. if(this.shouldLoadAllAtOnce) console.warn(`Fetched all responses for ${this.type}:`, res.length)
  101. }
  102. } catch (err) { console.error(err) }
  103. },
  104. async getPosts(params, dispatchType) {
  105. if(!this.type) throw `post type: ${this.type} not found...`
  106. let res = []
  107. // We always grab all pages on hero init so no need to do it here
  108. if(this.pType && this.keepFetching && this.type != 'page') {
  109. res = await this.$store.dispatch(
  110. `get${dispatchType}${this.pType}`,
  111. { sortType: this.sortBy, params }
  112. )
  113. }
  114. return res
  115. },
  116. async getPage(type) {
  117. await this._getAllIfNotLoaded('page', this.$store)
  118. if(!this.allPages) throw 'no pages in state'
  119. const page = this.allPages.filter(page => page.slug == `${type}s`)[0]
  120. if(!page) throw `No page: ${type} found. Cannot set hero.`
  121. return page
  122. },
  123. // _setHeroInfo(post) {} from mixin
  124. // _clearHero(store) {} from mixin
  125. async checkAndSetHero(type) {
  126. this._clearHero(this.$store)
  127. try {
  128. const page = await this.getPage(type)
  129. // We always set a hero no matter what
  130. // Because the hero component will deal
  131. // with how to render based on hero.url
  132. this.$store.commit('SET_HERO', this._setHeroInfo(page))
  133. } catch (err) {
  134. console.error(err)
  135. }
  136. },
  137. setIntersectionLoader() {
  138. // KeepFetching is UNSET for certain post types and sort types in `loadMorePosts()`
  139. if(!this.keepFetching) return console.warn('Cannot setup intersection handler keepFetching is set false')
  140. // Always unset before setting the intersection loader
  141. this.unsetIntersectionLoader()
  142. // console.warn('Setting interesection loader...')
  143. this.observer = new IntersectionObserver(entries => {
  144. entries.forEach(entry => {
  145. if (!entry.isIntersecting || this.loadingFetched) return
  146. setTimeout(this.loadMorePosts, TIMEOUT)
  147. })
  148. }, { threshold: 0.80 })
  149. this.observer.observe(document.querySelector(INTERSECT_SELECTOR))
  150. },
  151. unsetIntersectionLoader() {
  152. const footerEl = document.querySelector(INTERSECT_SELECTOR)
  153. try {
  154. if(!footerEl) throw `cannot unset intersection handler missing el: ${footerEl}`
  155. if(!this.observer) throw `cannot unset intersection handler missing observer: ${this.observer}`
  156. this.observer.unobserve(footerEl)
  157. this.observer.disconnect()
  158. } catch (error) { console.error(error) }
  159. },
  160. initPostList() {
  161. this.page = 0
  162. this.keepFetching = true
  163. this.checkAndSetHero(this.type)
  164. // Clear any preloaded posts (from home, etc.)
  165. this.loadMorePosts(true)
  166. this.setIntersectionLoader()
  167. }
  168. },
  169. watch: {
  170. // This only fires navigating from
  171. // a list page, to another list page
  172. // and the post type has changed
  173. type(newType) {
  174. if(!postTypes.includes(newType)) return console.warn('Type not valid...')
  175. // Ignore types with presorts so the sortBy watcher can handle them
  176. const ignore = ['event', 'exhibition', 'artist']
  177. if(ignore.includes(newType)) return
  178. this.initPostList()
  179. },
  180. sortBy(newSort) {
  181. if(!Object.values(sortTypes).includes(newSort)) return
  182. this.initPostList()
  183. }
  184. },
  185. mounted() {
  186. // This only fires navigating from a non-list page > list page
  187. this.initPostList()
  188. },
  189. beforeDestroy() {
  190. this.unsetIntersectionLoader()
  191. }
  192. }
  193. </script>
  194. <style lang="postcss">
  195. // prettier-ignore
  196. @import '../sss/variables.sss'
  197. @import '../sss/theme.sss'
  198. .page--list article
  199. > header
  200. padding: 1em
  201. > h1
  202. margin: 0
  203. > .content
  204. padding: 0
  205. width: 100%
  206. > footer
  207. padding: $ms-0
  208. /* posts not grid list */
  209. .posts
  210. list-style: none
  211. grid-gap: $ms--2
  212. .post
  213. width: 100%
  214. /* posts in grid list */
  215. .posts.is-grid
  216. width: 100%
  217. display: grid
  218. grid-template-columns: repeat(1, 1fr)
  219. align-items: start
  220. /* This is important for how the grid lines up to the page */
  221. justify-content: right
  222. .post img
  223. width: 100%
  224. @media (min-width: $medium)
  225. .page--list
  226. &.f-col
  227. flex-direction: row
  228. > article
  229. margin: 0 $ms--2 0 0
  230. .posts.is-grid
  231. grid-template-columns: repeat(3, 1fr)
  232. </style>