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.

single.vue 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template lang="pug">
  2. .page--single.f-row.between
  3. article(v-if="!post")
  4. header
  5. p loading...
  6. article(v-else).f-grow.shadow
  7. header
  8. h1 {{ type }}:{{ $route.params.slug }} {{ post.title }}
  9. p(v-if="post.categories") categories: {{ post.categories }}
  10. p(v-if="post.type") type: {{ post.type }}
  11. p(v-if="post.subtypes") subtypes: {{ post.subtypes }}
  12. .date-info(v-if="type === 'events' || post.type === 'exhibitions'")
  13. p start: {{ dateFrom(post.start) }}
  14. p end: {{ dateFrom(post.end) }}
  15. block.post-single.block-wrapper(v-for="(block, index) in post.blocks" :block="block" :key="`block-${index}`")
  16. credits(v-if="type === 'episodes'" :post="post")
  17. sidebar(v-if="sidebar" :type="`${type}`" layout="single")
  18. .shadow(v-if="Object.keys(p2pPostsByType).length" v-for="p2pPostType in Object.keys(p2pPostsByType)")
  19. h3.t-up related {{ p2pPostType }}s
  20. ul
  21. li(v-for="relatedPost in p2pPostsByType[p2pPostType]")
  22. router-link(v-if="relatedPost" :to="`/${relatedPost.type}s/${relatedPost.slug}`")
  23. p {{ relatedPost.title }}
  24. </template>
  25. <script>
  26. import sidebar from '@/components/sidebars/sidebar'
  27. import gallery from '@/components/gallery/'
  28. import block from '@/components/content-block/block'
  29. import credits from '@/components/credits'
  30. import { postTypeGetters } from './mixin-post-types'
  31. import { convertTitleCase, dePluralize, typeFromRoute } from '@/utils/helpers'
  32. export default {
  33. components: { sidebar, gallery, block, credits },
  34. props: {
  35. sidebar: { type: Boolean },
  36. id: { type: Number }
  37. },
  38. mixins: [postTypeGetters],
  39. data() {
  40. return {
  41. // Gallery control
  42. activeGalleryIndex: -1,
  43. activeImageID: -1
  44. }
  45. },
  46. computed: {
  47. type() { // Checks for type and fixes Episodes route edge case
  48. return typeFromRoute(this.$route)
  49. },
  50. /**
  51. * We get the actual post data using the slug
  52. */
  53. post() {
  54. if(!this[this.type]) return
  55. const type = dePluralize(this.type)
  56. // State not a getter!
  57. const singleOfTypeFromState = this[this.type][`single${convertTitleCase(type)}`]
  58. if(!singleOfTypeFromState) return
  59. return singleOfTypeFromState
  60. },
  61. idsForGallery() {
  62. return this.post.galleries[this.activeGalleryIndex].attrs.ids
  63. },
  64. /**
  65. * We need a convenient way to get all the images
  66. * broken down by gallery. We use the active gallery
  67. * image IDs to create a map. We match the ID to the
  68. * image size and url information returned by post.attached
  69. */
  70. imagesInGallery() {
  71. if(!this.activeGalleryIndex < 0) return {}
  72. // Create a map of images by their ID
  73. // {
  74. // imageId: { 'thumbnail': url, 'large': url }
  75. // }
  76. return this.idsForGallery.reduce((imageMap, id) => {
  77. imageMap[id] = this.post.attached[id]
  78. return imageMap
  79. }, {})
  80. },
  81. activeImageIndex() {
  82. return Object.keys(this.imagesInGallery).indexOf(this.activeImageID.toString())
  83. },
  84. p2pPostsByType() {
  85. return this.post ? Object.values(this.post.relatedto).reduce((byType, relatedPost) => {
  86. if(!byType[relatedPost.type]) byType[relatedPost.type] = []
  87. byType[relatedPost.type].push(relatedPost)
  88. return byType
  89. }, {}) : {}
  90. }
  91. },
  92. methods: {
  93. getImageIdsForGallery(galleryIndex) {
  94. console.log(galleryIndex)
  95. return galleryIndex >= 0 ? this.post.galleries[galleryIndex].attrs.ids : []
  96. },
  97. /**
  98. * We set the active gallery to the index.
  99. * Everything kicks off when activeGallery
  100. * is set. We also need to set the activeImageID
  101. * to the image clicked
  102. * @param {number} galleryIndex
  103. * @param {string} imageID
  104. */
  105. openGallery(galleryIndex, imageID) {
  106. this.activeGalleryIndex = galleryIndex
  107. this.activeImageID = imageID ? imageID : 0
  108. },
  109. /**
  110. * Everytime the posts object changes
  111. * we use this to set a new HERO
  112. * in vuex
  113. * @param {object} posts
  114. */
  115. checkAndSetHero(post) {
  116. if(!post || !post.hero) return
  117. const json = JSON.parse(post.hero)
  118. this.$store.commit('SET_HERO', json)
  119. },
  120. /**
  121. * Date Object from unix strings from db
  122. */
  123. dateFrom(unix) {
  124. return new Date( parseInt(unix) * 1000 )
  125. },
  126. async loadPostData() {
  127. /**
  128. * Conditionally load based on post type
  129. * which is derived from the route
  130. * !: posts watcher fires when this finishes
  131. */
  132. let type = convertTitleCase(this.type)
  133. let allPostsOfType = this.$store.state[this.type].all
  134. /**
  135. * Load posts if they're not already in state
  136. */
  137. if(!this[`all${type}Loaded`] && allPostsOfType.length < 1) {
  138. const res = await this.$store.dispatch(`getAll${type}`)
  139. allPostsOfType = res
  140. console.log(`reloaded ${type}...`)
  141. }
  142. const singlePostData = Object.values(allPostsOfType).filter(post => post.slug == this.$route.params.slug)[0]
  143. if(!singlePostData) return
  144. await this.$store.dispatch(`getSingle${dePluralize(type)}`, singlePostData.id)
  145. }
  146. },
  147. watch: {
  148. post(newVal, oldVal) {
  149. this.checkAndSetHero(newVal)
  150. },
  151. $route(newVal, oldVal) {
  152. console.log('route changed...')
  153. this.loadPostData()
  154. }
  155. },
  156. async created() {
  157. this.loadPostData()
  158. }
  159. }
  160. </script>
  161. <style lang="postcss">
  162. .page--single
  163. article
  164. > ul
  165. list-style: none
  166. li
  167. img
  168. width: 100%
  169. </style>