UserPosts.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <div class='route_container'>
  3. <div class='user_posts' :class='{ "user_posts--no_border_bottom": !posts.length }'>
  4. <div class='user_posts__title'>Posts by username</div>
  5. <scroll-load
  6. :loading='loadingPosts'
  7. :show='nextURL !== null'
  8. @load='loadNewPosts'
  9. v-if='sortedPosts.length'
  10. >
  11. <thread-post
  12. v-for='(post, index) in sortedPosts'
  13. :post='post'
  14. :show-thread='true'
  15. :class='{"post--last": index === posts.length-1}'
  16. ></thread-post>
  17. </scroll-load>
  18. <template v-else>This user hasn't posted anything yet</template>
  19. </div>
  20. </div>
  21. </template>
  22. <script>
  23. import ScrollLoad from '../ScrollLoad'
  24. import ThreadPost from '../ThreadPost'
  25. import AjaxErrorHandler from '../../assets/js/errorHandler'
  26. export default {
  27. name: 'user',
  28. components: {
  29. ThreadPost,
  30. ScrollLoad
  31. },
  32. data () {
  33. return {
  34. posts: [],
  35. loadingPosts: false,
  36. nextURL: ''
  37. }
  38. },
  39. computed: {
  40. sortedPosts () {
  41. return this.posts.sort((a, b) => {
  42. return new Date(a.createdAt) - new Date(b.createdAt)
  43. })
  44. }
  45. },
  46. methods: {
  47. loadNewPosts () {
  48. if(this.nextURL === null) return
  49. this.loadingPosts = true
  50. this.axios
  51. .get(this.nextURL)
  52. .then(res => {
  53. this.loadingPosts = false
  54. let currentPostsIds = this.posts.map(p => p.id)
  55. let filteredPosts =
  56. res.data.Posts.filter(p => !currentPostsIds.includes(p.id))
  57. this.posts.push(...filteredPosts)
  58. this.nextURL = res.data.meta.nextURL
  59. })
  60. .catch((e) => {
  61. this.loadingPosts = false
  62. AjaxErrorHandler(this.$store)(e)
  63. })
  64. }
  65. },
  66. created () {
  67. this.axios
  68. .get(`/api/v1/user/${this.$route.params.username}?posts=true`)
  69. .then(res => {
  70. this.posts = res.data.Posts
  71. this.nextURL = res.data.meta.nextURL
  72. })
  73. .catch(AjaxErrorHandler(this.$store))
  74. }
  75. }
  76. </script>
  77. <style lang='scss' scoped>
  78. @import '../../assets/scss/variables.scss';
  79. .user_posts {
  80. width: calc(75% + 5rem);
  81. @at-root #{&}__title {
  82. font-size: 1.5rem;
  83. margin-bottom: 1rem;
  84. }
  85. }
  86. </style>