Category.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <template>
  2. <div class='route_container'>
  3. <div class='thread_sorting'>
  4. <div>
  5. <select-button style='margin-right: 1rem' v-model='selectedCategory' :options='categories'></select-button>
  6. <select-options :options='filterOptions' v-model='selectedFilterOption'></select-options>
  7. </div>
  8. <button class='button' v-if='this.$store.state.username' @click='$router.push("/thread/new")'>Post new thread</button>
  9. </div>
  10. <table class='threads'>
  11. <colgroup>
  12. <col span="1" style="width: 50%;">
  13. <col span="1" style="width: 22.5%;">
  14. <col span="1" style="width: 22.5%;">
  15. <col span="1" style="width: 5%;">
  16. </colgroup>
  17. <thead>
  18. <tr class='thread thread--header'>
  19. <th>Title</th>
  20. <th>Latest post</th>
  21. <th>Category</th>
  22. <th>Replies</th>
  23. </tr>
  24. </thead>
  25. <tbody>
  26. <tr class='thread' v-for='thread in filteredThreads' @click='navigateToThread(thread.slug, thread.id)'>
  27. <td>{{thread.name}}</td>
  28. <td>
  29. <div>{{thread.Posts[0].content | stripTags | truncate(100)}}</div>
  30. <div>{{thread.Posts[0].createdAt | formatDate('time|date', ' - ') }}</div>
  31. </td>
  32. <td>{{thread.Category.name}}</td>
  33. <td>{{thread.replies}}</td>
  34. </tr>
  35. <tr class='thread' v-if='!filteredThreads.length' colspan='4'>
  36. <td colspan='4' class='thread--empty'>No threads or posts.</td>
  37. </tr>
  38. </tbody>
  39. </div>
  40. </div>
  41. </template>
  42. <script>
  43. import SelectButton from '../SelectButton'
  44. import TabView from '../TabView'
  45. import SelectOptions from '../SelectOptions'
  46. import AjaxErrorHandler from '../../assets/js/errorHandler'
  47. export default {
  48. name: 'index',
  49. components: {
  50. SelectButton,
  51. TabView,
  52. SelectOptions
  53. },
  54. data () {
  55. return {
  56. filterOptions: [
  57. {name: 'New', value: 'NEW'},
  58. {name: 'Most active', value: 'MOST_ACTIVE'},
  59. {name: 'No replies', value: 'NO_REPLIES'}
  60. ],
  61. selectedFilterOption: 'NEW',
  62. threads: []
  63. }
  64. },
  65. computed: {
  66. filteredThreads () {
  67. var categories = {};
  68. var filter = this.selectedFilterOption
  69. this.$store.state.meta.categories.forEach(category => {
  70. categories[category.value] = category.name;
  71. });
  72. return this.threads.filter(thread => {
  73. return (thread.Category.value === this.selectedCategory) || (this.selectedCategory === 'ALL');
  74. }).map(thread => {
  75. var _thread = Object.assign({}, thread);
  76. _thread.category = categories[thread.Category.value];
  77. return _thread;
  78. }).sort((a, b) => {
  79. if(filter === 'NEW') {
  80. let aDate = new Date(a.Posts[0].createdAt)
  81. let bDate = new Date(b.Posts[0].createdAt)
  82. return aDate - bDate;
  83. } else if(filter === 'MOST_ACTIVE') {
  84. return 0 //b.replies - a.replies;
  85. }
  86. }).filter(thread => {
  87. if(filter === 'NO_REPLIES') {
  88. return 0//!thread.replies;
  89. } else {
  90. return true;
  91. }
  92. });
  93. },
  94. categories () {
  95. return this.$store.state.meta.categories
  96. },
  97. selectedCategory: {
  98. set (val) {
  99. this.$store.commit('setSelectedCategory', val)
  100. },
  101. get () {
  102. return this.$store.state.category.selectedCategory
  103. }
  104. }
  105. },
  106. methods: {
  107. navigateToThread (slug, id) {
  108. this.$router.push('/thread/' + slug + '/' + id);
  109. },
  110. getThreads () {
  111. this.axios
  112. .get('/api/v1/category/' + this.selectedCategory)
  113. .then(res => {
  114. this.threads = res.data.Threads
  115. })
  116. .catch(AjaxErrorHandler(this.$store))
  117. }
  118. },
  119. watch: {
  120. selectedCategory (newValue) {
  121. this.$router.push('/category/' + newValue.toLowerCase());
  122. },
  123. $route () {
  124. this.selectedCategory = this.$route.path.split('/')[2].toUpperCase()
  125. this.getThreads()
  126. }
  127. },
  128. created () {
  129. this.selectedCategory = this.$route.path.split('/')[2].toUpperCase()
  130. this.getThreads()
  131. }
  132. }
  133. </script>
  134. <style lang='scss' scoped>
  135. @import '../../assets/scss/variables.scss';
  136. .thread_sorting {
  137. margin-bottom: 1rem;
  138. display: flex;
  139. justify-content: space-between;
  140. }
  141. .threads {
  142. border-collapse: collapse;
  143. }
  144. .thread {
  145. background-color: #fff;
  146. padding: 0.5rem 0;
  147. cursor: default;
  148. text-align: left;
  149. transition: background-color 0.2s;
  150. &:hover {
  151. background-color: $color__lightgray--primary;
  152. }
  153. td, th {
  154. padding: 0.3rem 0.5rem;
  155. border-bottom: solid thin $color__lightgray--primary;
  156. }
  157. @at-root #{&}--header {
  158. &:hover {
  159. background-color: #fff;
  160. }
  161. th {
  162. font-weight: 400;
  163. padding-bottom: 0.25rem;
  164. border-bottom: thin solid $color__lightgray--darkest;
  165. }
  166. }
  167. @at-root #{&}--empty {
  168. height: 5rem;
  169. text-align: center;
  170. font-size: 2rem;
  171. user-select: none;
  172. cursor: default;
  173. transition: none;
  174. &:hover {
  175. transition: none;
  176. background-color: #fff;
  177. }
  178. }
  179. @at-root #{&}__section {
  180. padding: 0 0.5rem;
  181. }
  182. @at-root #{&}__user {
  183. display: inline-block;
  184. }
  185. @at-root #{&}__date {
  186. color: $color__text--secondary;
  187. display: inline-block;
  188. }
  189. }
  190. </style>