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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template lang="pug">
  2. .page--list.f-row.between
  3. article.f-grow
  4. header.f-row.center
  5. h1 {{ type }} list
  6. span(v-if="sortBy")
  7. h1 &nbsp;sorted by {{ sortBy.replace('-', ' ') }}
  8. .posts(:class="{ 'is-grid': grid }")
  9. section(v-for="post in posts" :key="post.slug").shadow.post
  10. router-link(:to="`/${type}/${post.slug}`")
  11. h4 {{ post.title }}
  12. p(style="font-size: 9px;") {{ post.date }}
  13. footer
  14. p {{ `${type} count: ${Object.values(posts).length}` }}
  15. p {{ `show sidebar: ${sidebar}` }}
  16. sidebar(v-if="sidebar" :type="`${type}`")
  17. .shadow
  18. h1.t-up slots stuff
  19. div
  20. p more body whatever
  21. p another line of stuff
  22. </template>
  23. <script>
  24. import sidebar from '@/components/sidebars/sidebar'
  25. import { postTypeGetters } from './mixin-post-types'
  26. import { convertTitleCase, typeFromRoute, sortTypes } from '@/utils/helpers'
  27. export default {
  28. components: { sidebar },
  29. props: {
  30. sidebar: { type: Boolean },
  31. grid: { type: Boolean },
  32. sortBy: { type: String }
  33. },
  34. mixins: [postTypeGetters],
  35. computed: {
  36. type() { // Checks for type and fixes Episodes route edge case
  37. return typeFromRoute(this.$route)
  38. },
  39. dispatchName() {
  40. let type = convertTitleCase(this.type)
  41. return this.sortBy ? `getAll${type.split('/')[0]}` : `getAll${type}`
  42. },
  43. posts() {
  44. let type = convertTitleCase(this.type)
  45. if(!type) return
  46. /**
  47. * We override the API to sort by date
  48. * because items are returned by ID so
  49. * we need to resort it by date
  50. */
  51. let unsortedOfType = Object.values(this[`all${type}`])
  52. let sortedByRecent = unsortedOfType.sort((postA, postB) => new Date(postB.date) - new Date(postA.date))
  53. /**
  54. * Sorting of this[`all${type}`] is also controlled by API
  55. * so if a sortBy is specified we return it sorted or
  56. * we fail over to sorted by date
  57. */
  58. return this.sortBy ? this[`all${type}`] : sortedByRecent
  59. },
  60. },
  61. methods: {
  62. setHeroAndGetPosts() {
  63. let type = convertTitleCase(this.type)
  64. let sort = this.sortBy ? this.sortBy : this.$route.path.split('/').pop()
  65. console.log('trying to sort by:', sort)
  66. console.log('includes:', Object.values(sortTypes).includes(sort))
  67. // Is this a sort type?
  68. if(this.type !== sort || !Object.values(sortTypes).includes(sort)) sort = null
  69. // Don't dispatch if there's no type
  70. if(this.type) {
  71. this.$store.dispatch(this.dispatchName, sort)
  72. }
  73. if(this.$store.state.hero.url !== type) this.$store.commit('SET_HERO', type)
  74. }
  75. },
  76. watch: {
  77. $route(to, from){
  78. let type = convertTitleCase(this.type)
  79. let sort = this.sortBy ? this.sortBy : to.path.split('/').pop()
  80. if(!this[`all${type}Loaded`] || sort) this.setHeroAndGetPosts()
  81. }
  82. },
  83. created() {
  84. let type = convertTitleCase(this.type)
  85. // console.log('already loaded ?:', this[`all${type}Loaded`])
  86. if(!this[`all${type}Loaded`]) this.setHeroAndGetPosts()
  87. }
  88. }
  89. </script>
  90. <style lang="postcss">
  91. .page--list
  92. article
  93. .is-grid
  94. display: flex
  95. flex-direction: row
  96. flex-wrap: wrap
  97. section
  98. width: 33%
  99. </style>