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 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. template(v-for="(post, i) in posts" :key="post.slug")
  16. li.post.shadow(v-if="!post.inbetween" )
  17. card(:content="post" :type="type" :wide="isWide")
  18. li.post.shadow.inbetween.t-up.f-row.w-max(v-else-if="post.inbetween" :id="post.slug")
  19. p {{ post.slug }}
  20. //- Important: Do NOT remove this! Required for intersection observer
  21. footer
  22. p(v-if="loadingFetched") loading more {{ type }}...
  23. sidebar(v-if="sidebar" :type="`${type}`" layout="list")
  24. </template>
  25. <script>
  26. import featuredImage from '@/components/featured-image'
  27. import card from '@/components/card'
  28. import sidebar from '@/components/sidebars/sidebar'
  29. import { postTypeGetters, scrollTop, heroUtils } from './mixin-post-types'
  30. import { postTypes, sortTypes, convertTitleCase } from '@/utils/helpers'
  31. const TIMEOUT = 1
  32. const INTERSECT_SELECTOR = '.page--list > article footer'
  33. const wideTypes = ['event', 'exhibition']
  34. const gridTypes = ['episode', 'artist']
  35. const sansSidebarTypes = ['episode']
  36. export default {
  37. components: { sidebar, featuredImage, card },
  38. mixins: [postTypeGetters, scrollTop, heroUtils],
  39. data() {
  40. return {
  41. page: 0,
  42. perPage: 21,
  43. keepFetching: true,
  44. loadingFetched: false,
  45. observer: null
  46. }
  47. },
  48. computed: {
  49. type() {
  50. return this.$route.params.type
  51. },
  52. sortBy() {
  53. return this.$route.params.sortBy
  54. },
  55. grid() {
  56. return gridTypes.includes(this.type)
  57. },
  58. isWide() {
  59. return wideTypes.includes(this.type)
  60. },
  61. sidebar() {
  62. return !sansSidebarTypes.includes(this.type)
  63. },
  64. pType() {
  65. if(!this.type) return
  66. return `${convertTitleCase(this.type)}s`
  67. },
  68. loaded() {
  69. if (!this.pType) return
  70. return this[`all${this.pType}Loaded`]
  71. },
  72. posts() {
  73. if (!this.pType) return
  74. return this[`all${this.pType}`]
  75. },
  76. shouldLoadAllAtOnce() {
  77. return this.type == 'episode' || this.type == 'artist' && this.sortBy == sortTypes.material
  78. }
  79. },
  80. methods: {
  81. async loadMorePosts(shouldClear) {
  82. if(shouldClear) this.$store.commit(`CLEAR_${this.pType.toUpperCase()}`)
  83. if(!this.keepFetching) return console.warn('nothing left to fetch...')
  84. try {
  85. this.loadingFetched = true
  86. this.page++
  87. const res = await this.getPosts(
  88. {
  89. limit: this.shouldLoadAllAtOnce ? -1 : this.perPage,
  90. page: this.page
  91. },
  92. this.shouldLoadAllAtOnce ? 'All' : 'More'
  93. )
  94. this.loadingFetched = false
  95. // Stop trying to load more posts
  96. if(res && !res.length || this.shouldLoadAllAtOnce) {
  97. this.keepFetching = false
  98. if(!res.length) console.warn(`Empty response for ${this.type}:`, res.length)
  99. if(this.shouldLoadAllAtOnce) console.warn(`Fetched all responses for ${this.type}:`, res.length)
  100. }
  101. } catch (err) { console.error(err) }
  102. },
  103. async getPosts(params, dispatchType) {
  104. if(!this.type) throw `post type: ${this.type} not found...`
  105. let res = []
  106. console.log('$route :', $route)
  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. console.log('---')
  130. console.log(page)
  131. // We always set a hero no matter what
  132. // Because the hero component will deal
  133. // with how to render based on hero.url
  134. this.$store.commit('SET_HERO', this._setHeroInfo(page))
  135. } catch (err) { console.error(err) }
  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 a list page, to another list page
  171. type(newType) {
  172. if(!postTypes.includes(newType)) return console.warn('Type not valid...')
  173. // Ignore types with presorts so the sortBy watcher can handle them
  174. const ignore = ['event', 'exhibition', 'artist']
  175. if(ignore.includes(newType)) return
  176. this.initPostList()
  177. },
  178. sortBy(newSort) {
  179. if(!Object.values(sortTypes).includes(newSort)) return
  180. this.initPostList()
  181. }
  182. },
  183. mounted() {
  184. // This only fires navigating from a non-list page > list page
  185. this.initPostList()
  186. },
  187. beforeDestroy() {
  188. this.unsetIntersectionLoader()
  189. }
  190. }
  191. </script>
  192. <style lang="postcss">
  193. // prettier-ignore
  194. @import '../sss/variables.sss'
  195. @import '../sss/theme.sss'
  196. .page--list
  197. /* Puts the aside bar on top */
  198. flex-direction: column-reverse !important
  199. article
  200. > header
  201. padding: 1em
  202. > h1
  203. margin: 0
  204. > .content
  205. padding: 0
  206. width: 100%
  207. > footer
  208. padding: $ms-0
  209. /* posts not grid list */
  210. .posts
  211. list-style: none
  212. grid-gap: $ms--2
  213. .post
  214. width: 100%
  215. &.inbetween
  216. grid-column: span 2
  217. background: white
  218. padding: 0.3em 0
  219. font-size: 1.3em
  220. > p
  221. margin: 0
  222. /* posts in grid list */
  223. .posts.is-grid
  224. width: 100%
  225. min-width: 185px
  226. display: grid
  227. grid-template-columns: repeat(2, 1fr)
  228. align-items: start
  229. /* This is important for how the grid lines up to the page */
  230. justify-content: right
  231. .post
  232. min-width: 177px
  233. img
  234. width: 100%
  235. @media (min-width: $medium)
  236. .page--list
  237. &.f-col
  238. flex-direction: row !important
  239. > article
  240. margin: 0 $ms--2 0 0
  241. .posts.is-grid
  242. grid-template-columns: repeat(3, 1fr)
  243. .post.inbetween
  244. grid-column: span 3
  245. </style>