Index.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <template>
  2. <div class='route_container'>
  3. <div class='thread_sorting'>
  4. <div>
  5. <select-options
  6. :options='displayOptions'
  7. class='thread_sorting__display'
  8. v-model='selectedDisplayOption'
  9. ></select-options>
  10. <select-options
  11. :options='filterOptions'
  12. v-model='selectedFilterOption'
  13. class='thread_sorting__filter'
  14. ></select-options>
  15. </div>
  16. <button class='button' v-if='this.$store.state.username' @click='$router.push("/thread/new")'>Post new thread</button>
  17. </div>
  18. <div class='threads_main'>
  19. <div class='threads_main__side_bar'>
  20. <div class='threads_main__side_bar__title'>
  21. categories
  22. </div>
  23. <div
  24. v-for='category in categories'
  25. class='threads_main__side_bar__menu_item'
  26. :class='{"threads_main__side_bar__menu_item--selected": category.value === selectedCategory}'
  27. @click='selectedCategory = category.value'
  28. >
  29. <span class='threads_main__side_bar__menu_item__border'></span>
  30. {{category.name}}
  31. </div>
  32. </div>
  33. <table class='threads_main__threads' v-if='filteredThreads.length'>
  34. <colgroup>
  35. <col span="1" style="width: 50%;">
  36. <col span="1" style="width: 22.5%;">
  37. <col span="1" style="width: 22.5%;">
  38. <col span="1" style="width: 5%;">
  39. </colgroup>
  40. <thead>
  41. <tr class='thread thread--header'>
  42. <th>Title</th>
  43. <th>Latest post</th>
  44. <th>Category</th>
  45. <th>Replies</th>
  46. </tr>
  47. </thead>
  48. <tbody>
  49. <tr class='thread' v-for='thread in filteredThreads' @click='navigateToThread(thread.slug, thread.id)'>
  50. <td>{{thread.name}}</td>
  51. <td>
  52. <div>{{thread.Posts[0].content | stripTags | truncate(100)}}</div>
  53. <div>{{thread.Posts[0].createdAt | formatDate('time|date', ' - ') }}</div>
  54. </td>
  55. <td>{{thread.Category.name}}</td>
  56. <td>{{thread.postsCount-1}}</td>
  57. </tr>
  58. </tbody>
  59. </table>
  60. <div v-else class='threads_main__threads thread--empty'>No threads or posts.</div>
  61. </div>
  62. </div>
  63. </template>
  64. <script>
  65. import TabView from '../TabView'
  66. import SelectOptions from '../SelectOptions'
  67. import AjaxErrorHandler from '../../assets/js/errorHandler'
  68. export default {
  69. name: 'index',
  70. components: {
  71. TabView,
  72. SelectOptions
  73. },
  74. data () {
  75. return {
  76. filterOptions: [
  77. {name: 'New', value: 'NEW'},
  78. {name: 'Most active', value: 'MOST_ACTIVE'},
  79. {name: 'No replies', value: 'NO_REPLIES'}
  80. ],
  81. selectedFilterOption: 'NEW',
  82. displayOptions: [
  83. { name: 'Threads', value: 'THREADS' },
  84. { name: 'Posts', value: 'POSTS' }
  85. ],
  86. selectedDisplayOption: 'THREADS',
  87. threads: []
  88. }
  89. },
  90. computed: {
  91. filteredThreads () {
  92. var categories = {};
  93. var filter = this.selectedFilterOption
  94. this.$store.state.meta.categories.forEach(category => {
  95. categories[category.value] = category.name;
  96. });
  97. return this.threads.filter(thread => {
  98. return (thread.Category.value === this.selectedCategory) || (this.selectedCategory === 'ALL');
  99. }).map(thread => {
  100. var _thread = Object.assign({}, thread);
  101. _thread.category = categories[thread.Category.value];
  102. return _thread;
  103. }).sort((a, b) => {
  104. if(filter === 'NEW') {
  105. let aDate = new Date(a.Posts[0].createdAt)
  106. let bDate = new Date(b.Posts[0].createdAt)
  107. return aDate - bDate;
  108. } else if(filter === 'MOST_ACTIVE') {
  109. return b.postsCount - a.postsCount;
  110. }
  111. }).filter(thread => {
  112. if(filter === 'NO_REPLIES' && thread.postsCount-1) {
  113. return false
  114. } else {
  115. return true;
  116. }
  117. });
  118. },
  119. categories () {
  120. return this.$store.state.meta.categories
  121. },
  122. selectedCategory: {
  123. set (val) {
  124. this.$store.commit('setSelectedCategory', val)
  125. },
  126. get () {
  127. return this.$store.state.category.selectedCategory
  128. }
  129. }
  130. },
  131. methods: {
  132. navigateToThread (slug, id) {
  133. this.$router.push('/thread/' + slug + '/' + id);
  134. },
  135. getThreads () {
  136. this.axios
  137. .get('/api/v1/category/' + this.selectedCategory)
  138. .then(res => {
  139. this.threads = res.data.Threads
  140. })
  141. .catch(AjaxErrorHandler(this.$store))
  142. }
  143. },
  144. watch: {
  145. selectedCategory (newValue) {
  146. this.$router.push('/category/' + newValue.toLowerCase());
  147. },
  148. $route () {
  149. this.selectedCategory = this.$route.path.split('/')[2].toUpperCase()
  150. this.getThreads()
  151. }
  152. },
  153. created () {
  154. this.selectedCategory = this.$route.path.split('/')[2].toUpperCase()
  155. this.getThreads()
  156. }
  157. }
  158. </script>
  159. <style lang='scss' scoped>
  160. @import '../../assets/scss/variables.scss';
  161. .threads_main {
  162. display: flex;
  163. }
  164. .thread_sorting {
  165. margin-bottom: 1rem;
  166. display: flex;
  167. justify-content: space-between;
  168. @at-root #{&}__display {
  169. padding-right: 0.5rem;
  170. border-right: thin solid $color__gray--primary;
  171. margin-right: 1.25rem;
  172. width: 10rem;
  173. }
  174. }
  175. .threads_main__side_bar {
  176. width: 10rem;
  177. border-right: thin solid $color__gray--primary;
  178. margin-top: 0.5rem;
  179. @at-root #{&}__title {
  180. cursor: default;
  181. font-weight: 500;
  182. font-variant: small-caps;
  183. font-size: 1.125rem;
  184. }
  185. @at-root #{&}__menu_item {
  186. cursor: pointer;
  187. margin-top: 0.5rem;
  188. position: relative;
  189. #{&}__border {
  190. display: inline-block;
  191. width: 0.2rem;
  192. z-index: 1;
  193. height: 100%;
  194. position: absolute;
  195. left: -0.75rem;
  196. opacity: 0;
  197. top: 0.1rem;
  198. background-color: $color__gray--darkest;
  199. transition: all 0.2s;
  200. }
  201. &:hover #{&}__border {
  202. left: -0.5rem;
  203. opacity: 1;
  204. }
  205. &:active #{&}__border {
  206. filter: brightness(0.8);
  207. }
  208. #{&}--selected {
  209. font-weight: 500;
  210. .threads_main__side_bar__menu_item__border {
  211. opacity: 1;
  212. left: -0.5rem;
  213. &:active {
  214. filter: brightness(1);
  215. }
  216. }
  217. }
  218. }
  219. }
  220. .threads_main__threads {
  221. border-collapse: collapse;
  222. margin-top: 0.25rem;
  223. margin-left: 1rem;
  224. width: calc(100% - 11rem);
  225. }
  226. .thread {
  227. background-color: #fff;
  228. padding: 0.5rem 0;
  229. cursor: default;
  230. text-align: left;
  231. transition: background-color 0.2s;
  232. &:hover {
  233. background-color: $color__lightgray--primary;
  234. }
  235. td, th {
  236. padding: 0.3rem 0.5rem;
  237. border-bottom: solid thin $color__lightgray--primary;
  238. }
  239. @at-root #{&}--header {
  240. &:hover {
  241. background-color: #fff;
  242. }
  243. th {
  244. font-weight: 400;
  245. padding-bottom: 0.25rem;
  246. border-bottom: thin solid $color__lightgray--darkest;
  247. }
  248. }
  249. @at-root #{&}--empty {
  250. display: flex;
  251. align-items: center;
  252. justify-content: center;
  253. padding-right: 5rem;
  254. font-size: 2rem;
  255. user-select: none;
  256. cursor: default;
  257. transition: none;
  258. &:hover {
  259. transition: none;
  260. background-color: #fff;
  261. }
  262. }
  263. @at-root #{&}__section {
  264. padding: 0 0.5rem;
  265. }
  266. @at-root #{&}__user {
  267. display: inline-block;
  268. }
  269. @at-root #{&}__date {
  270. color: $color__text--secondary;
  271. display: inline-block;
  272. }
  273. }
  274. </style>