Index.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <template>
  2. <div class='route_container'>
  3. <div class='thread_sorting'>
  4. <select-options
  5. :options='filterOptions'
  6. v-model='selectedFilterOption'
  7. class='thread_sorting__filter'
  8. ></select-options>
  9. <button class='button button--blue' v-if='this.$store.state.username' @click='$router.push("/thread/new")'>Post new thread</button>
  10. </div>
  11. <div class='threads_main'>
  12. <div class='threads_main__side_bar'>
  13. <div class='threads_main__side_bar__title'>
  14. categories
  15. </div>
  16. <div
  17. v-for='category in categories'
  18. class='threads_main__side_bar__menu_item'
  19. :class='{"threads_main__side_bar__menu_item--selected": category.value === selectedCategory}'
  20. @click='selectedCategory = category.value'
  21. >
  22. <span
  23. class='threads_main__side_bar__menu_item__border'
  24. :style='{"background-color": category.color}'
  25. ></span>
  26. <span
  27. class='threads_main__side_bar__menu_item__text'
  28. :style='{
  29. "color": category.value === selectedCategory ? category.color : undefined
  30. }'
  31. >{{category.name}}</span>
  32. </div>
  33. </div>
  34. <scroll-load
  35. class='threads_main__threads'
  36. v-if='filteredThreads.length'
  37. :loading='loading'
  38. @loadNext='getThreads'
  39. >
  40. <thread-display-placeholder v-for='n in newThreads' v-if='loadingNewer'></thread-display-placeholder>
  41. <div class='threads_main__load_new' v-if='newThreads' @click='getNewerThreads'>
  42. Load {{newThreads}} new {{newThreads | pluralize('thread')}}</span>
  43. </div>
  44. <thread-display v-for='thread in filteredThreads' :thread='thread'></thread-display>
  45. <thread-display-placeholder v-for='n in nextThreadsCount' v-if='loading'></thread-display-placeholder>
  46. </scroll-load>
  47. <div v-else class='threads_main__threads thread--empty'>
  48. <span class='fa fa-exclamation-circle'></span>
  49. No threads or posts.
  50. </div>
  51. </div>
  52. </div>
  53. </template>
  54. <script>
  55. import TabView from '../TabView'
  56. import ScrollLoad from '../ScrollLoad'
  57. import ThreadDisplay from '../ThreadDisplay'
  58. import ThreadDisplayPlaceholder from '../ThreadDisplayPlaceholder'
  59. import SelectOptions from '../SelectOptions'
  60. import AjaxErrorHandler from '../../assets/js/errorHandler'
  61. export default {
  62. name: 'index',
  63. components: {
  64. TabView,
  65. ScrollLoad,
  66. ThreadDisplay,
  67. ThreadDisplayPlaceholder,
  68. SelectOptions
  69. },
  70. data () {
  71. return {
  72. filterOptions: [
  73. {name: 'New', value: 'NEW'},
  74. {name: 'Most active', value: 'MOST_ACTIVE'},
  75. {name: 'No replies', value: 'NO_REPLIES'}
  76. ],
  77. selectedFilterOption: 'NEW',
  78. nextURL: '',
  79. nextThreadsCount: 0,
  80. loading: false,
  81. threads: [],
  82. newThreads: 0,
  83. loadingNewer: false
  84. }
  85. },
  86. computed: {
  87. filteredThreads () {
  88. var categories = {};
  89. var filter = this.selectedFilterOption
  90. this.$store.state.meta.categories.forEach(category => {
  91. categories[category.value] = category.name;
  92. });
  93. return this.threads.filter(thread => {
  94. return (thread.Category.value === this.selectedCategory) || (this.selectedCategory === 'ALL');
  95. }).map(thread => {
  96. var _thread = Object.assign({}, thread);
  97. _thread.category = categories[thread.Category.value];
  98. return _thread;
  99. }).sort((a, b) => {
  100. if(filter === 'NEW') {
  101. let aDate = new Date(a.Posts[0].createdAt)
  102. let bDate = new Date(b.Posts[0].createdAt)
  103. return bDate - aDate;
  104. } else if(filter === 'MOST_ACTIVE') {
  105. return b.postsCount - a.postsCount;
  106. }
  107. }).filter(thread => {
  108. if(filter === 'NO_REPLIES' && thread.postsCount-1) {
  109. return false
  110. } else {
  111. return true;
  112. }
  113. });
  114. },
  115. categories () {
  116. return this.$store.getters.alphabetizedCategories
  117. },
  118. selectedCategory: {
  119. set (val) {
  120. let name = this.categories.find(c => c.value === val)
  121. this.$store.dispatch('setTitle', name ? name.name : '')
  122. this.$store.commit('setSelectedCategory', val)
  123. },
  124. get () {
  125. return this.$store.state.category.selectedCategory
  126. }
  127. }
  128. },
  129. methods: {
  130. navigateToThread (slug, id) {
  131. this.$router.push('/thread/' + slug + '/' + id);
  132. },
  133. getThreads (initial) {
  134. if(this.nextURL === null && !initial) return
  135. let URL = '/api/v1/category/' + this.selectedCategory
  136. if(!initial) {
  137. URL = this.nextURL || URL
  138. }
  139. this.loading = true
  140. this.axios
  141. .get(URL)
  142. .then(res => {
  143. this.loading = false
  144. if(initial) {
  145. this.threads = res.data.Threads
  146. } else {
  147. this.threads.push(...res.data.Threads)
  148. }
  149. this.nextURL = res.data.meta.nextURL
  150. this.nextThreadsCount = res.data.meta.nextThreadsCount
  151. })
  152. .catch((e) => {
  153. this.loading = false
  154. AjaxErrorHandler(this.$store)(e)
  155. })
  156. },
  157. getNewerThreads () {
  158. this.loadingNewer = true
  159. this.axios
  160. .get('/api/v1/category/' + this.selectedCategory + '?limit=' + this.newThreads)
  161. .then(res => {
  162. this.loadingNewer = false
  163. this.newThreads = 0
  164. this.threads.unshift(...res.data.Threads)
  165. })
  166. .catch((e) => {
  167. this.loadingNewer = false
  168. AjaxErrorHandler(this.$store)(e)
  169. })
  170. }
  171. },
  172. watch: {
  173. selectedCategory (newValue) {
  174. this.$router.push('/category/' + newValue.toLowerCase());
  175. },
  176. $route () {
  177. this.selectedCategory = this.$route.path.split('/')[2].toUpperCase()
  178. this.newThreads = 0
  179. this.getThreads(true)
  180. }
  181. },
  182. created () {
  183. this.selectedCategory = this.$route.path.split('/')[2].toUpperCase()
  184. this.getThreads(true)
  185. socket.emit('join', 'index')
  186. socket.on('new thread', data => {
  187. if(data.value === this.selectedCategory || this.selectedCategory == 'ALL') {
  188. this.newThreads++
  189. }
  190. })
  191. },
  192. destroyed () {
  193. socket.emit('leave', 'index')
  194. socket.off('new thread')
  195. }
  196. }
  197. </script>
  198. <style lang='scss' scoped>
  199. @import '../../assets/scss/elementStyles.scss';
  200. @import '../../assets/scss/variables.scss';
  201. .forum_description {
  202. padding: 1rem;
  203. margin-bottom: 2rem;
  204. background-color: #fff;
  205. border-radius: 0.25rem;
  206. @extend .shadow_border;
  207. }
  208. .threads_main {
  209. display: flex;
  210. }
  211. .thread_sorting {
  212. margin-bottom: 1rem;
  213. display: flex;
  214. justify-content: space-between;
  215. @at-root #{&}__display {
  216. padding-right: 0.5rem;
  217. border-right: thin solid $color__gray--primary;
  218. margin-right: 1.25rem;
  219. width: 10rem;
  220. }
  221. }
  222. .threads_main__side_bar {
  223. width: 12rem;
  224. height: 0%;
  225. @extend .shadow_border;
  226. background: #fff;
  227. margin-top: 0.15rem;
  228. margin-right: 1rem;
  229. border-radius: 0.25rem;
  230. padding: 0.5rem 0 1rem 1rem;
  231. @at-root #{&}__title {
  232. cursor: default;
  233. font-weight: 500;
  234. font-variant: small-caps;
  235. font-size: 1.125rem;
  236. }
  237. @at-root #{&}__menu_item {
  238. cursor: pointer;
  239. margin-top: 0.5rem;
  240. position: relative;
  241. #{&}__border {
  242. display: inline-block;
  243. height: 0.75rem;
  244. width: 0.75rem;
  245. border-radius: 0.25rem;
  246. opacity: 0.5;
  247. background-color: $color__gray--darkest;
  248. position: relative;
  249. top: 0.05rem;
  250. transition: all 0.2s;
  251. }
  252. &:hover #{&}__border {
  253. opacity: 1;
  254. }
  255. &:active #{&}__border {
  256. filter: brightness(0.8);
  257. }
  258. #{&}__text {
  259. filter: saturate(0.75), brightness(0.75);
  260. }
  261. #{&}--selected {
  262. font-weight: 500;
  263. .threads_main__side_bar__menu_item__border {
  264. opacity: 1;
  265. &:active {
  266. filter: brightness(1);
  267. }
  268. }
  269. }
  270. }
  271. }
  272. .threads_main__threads {
  273. border-collapse: collapse;
  274. margin-top: 0.25rem;
  275. margin-left: 1rem;
  276. width: calc(100% - 11rem);
  277. }
  278. .threads_main__load_new {
  279. @extend .button;
  280. font-size: 1.25rem;
  281. margin: 0 0 1rem 0;
  282. background-color: $color__lightgray--primary;
  283. border-color: $color__gray--darker;
  284. width: 100%;
  285. font-weight: 300;
  286. }
  287. .thread {
  288. background-color: #fff;
  289. padding: 0.5rem 0;
  290. cursor: default;
  291. text-align: left;
  292. transition: background-color 0.2s;
  293. &:hover {
  294. background-color: $color__lightgray--primary;
  295. }
  296. td, th {
  297. padding: 0.3rem 0.5rem;
  298. border-bottom: solid thin $color__lightgray--primary;
  299. }
  300. @at-root #{&}--header {
  301. &:hover {
  302. background-color: #fff;
  303. }
  304. th {
  305. font-weight: 400;
  306. padding-bottom: 0.25rem;
  307. border-bottom: thin solid $color__lightgray--darkest;
  308. }
  309. }
  310. @at-root #{&}--empty {
  311. display: flex;
  312. flex-direction: column;
  313. align-items: center;
  314. justify-content: center;
  315. padding-right: 5rem;
  316. font-size: 2rem;
  317. user-select: none;
  318. cursor: default;
  319. transition: none;
  320. color: $color__gray--darkest;
  321. span {
  322. font-size: 4rem;
  323. color: $color__gray--darker;
  324. }
  325. }
  326. @at-root #{&}__section {
  327. padding: 0 0.5rem;
  328. }
  329. @at-root #{&}__user {
  330. display: inline-block;
  331. }
  332. @at-root #{&}__date {
  333. color: $color__text--secondary;
  334. display: inline-block;
  335. }
  336. }
  337. </style>