PostComments.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <style scoped>
  2. span {
  3. font-size: 14px;
  4. }
  5. .comment-text {
  6. }
  7. .comment-text p {
  8. display: inline;
  9. }
  10. </style>
  11. <template>
  12. <div>
  13. <div class="postCommentsLoader text-center">
  14. <div class="lds-ring"><div></div><div></div><div></div><div></div></div>
  15. </div>
  16. <div class="postCommentsContainer d-none">
  17. <p class="mb-1 text-center load-more-link d-none"><a href="#" class="text-muted" v-on:click="loadMore">Load more comments</a></p>
  18. <div class="comments" data-min-id="0" data-max-id="0">
  19. <p v-for="(reply, index) in results" class="mb-0 d-flex justify-content-between align-items-top read-more" style="overflow-y: hidden;">
  20. <span>
  21. <a class="text-dark font-weight-bold mr-1" :href="reply.account.url">{{reply.account.username}}</a>
  22. <span v-html="reply.content" class=""></span>
  23. </span>
  24. <span class="" style="min-width:38px">
  25. <span v-on:click="likeStatus(reply, $event)"><i v-bind:class="[reply.favourited ? 'fas fa-heart fa-sm text-danger':'far fa-heart fa-sm text-lighter']"></i></span>
  26. <post-menu :status="reply" :profile="user" :size="'sm'" :modal="'true'" class="d-inline-block pl-2"></post-menu>
  27. </span>
  28. </p>
  29. </div>
  30. </div>
  31. </div>
  32. </template>
  33. <style type="text/css" scoped>
  34. .text-lighter {
  35. color:#B8C2CC !important;
  36. }
  37. </style>
  38. <script>
  39. export default {
  40. props: ['post-id', 'post-username', 'user'],
  41. data() {
  42. return {
  43. results: null,
  44. pagination: {},
  45. min_id: 0,
  46. max_id: 0,
  47. reply_to_profile_id: 0,
  48. }
  49. },
  50. mounted() {
  51. this.fetchData();
  52. },
  53. updated() {
  54. pixelfed.readmore();
  55. },
  56. methods: {
  57. embed(e) {
  58. //pixelfed.embed.build(e);
  59. },
  60. deleteComment(id, i) {
  61. axios.post('/i/delete', {
  62. type: 'comment',
  63. item: id
  64. }).then(res => {
  65. this.results.splice(i, 1);
  66. }).catch(err => {
  67. swal('Something went wrong!', 'Please try again later', 'error');
  68. });
  69. },
  70. l(e) {
  71. let len = e.length;
  72. if(len < 10) { return e; }
  73. return e.substr(0, 10)+'...';
  74. },
  75. reply(e) {
  76. this.reply_to_profile_id = e.account.id;
  77. $('.comment-form input[name=comment]').val('@'+e.account.username+' ');
  78. $('.comment-form input[name=comment]').focus();
  79. },
  80. fetchData() {
  81. let url = '/api/v2/comments/'+this.postUsername+'/status/'+this.postId;
  82. axios.get(url)
  83. .then(response => {
  84. let self = this;
  85. this.results = _.reverse(response.data.data);
  86. this.pagination = response.data.meta.pagination;
  87. if(this.results.length > 0) {
  88. $('.load-more-link').removeClass('d-none');
  89. }
  90. $('.postCommentsLoader').addClass('d-none');
  91. $('.postCommentsContainer').removeClass('d-none');
  92. }).catch(error => {
  93. if(!error.response) {
  94. $('.postCommentsLoader .lds-ring')
  95. .attr('style','width:100%')
  96. .addClass('pt-4 font-weight-bold text-muted')
  97. .text('An error occurred, cannot fetch comments. Please try again later.');
  98. } else {
  99. switch(error.response.status) {
  100. case 401:
  101. $('.postCommentsLoader .lds-ring')
  102. .attr('style','width:100%')
  103. .addClass('pt-4 font-weight-bold text-muted')
  104. .text('Please login to view.');
  105. break;
  106. default:
  107. $('.postCommentsLoader .lds-ring')
  108. .attr('style','width:100%')
  109. .addClass('pt-4 font-weight-bold text-muted')
  110. .text('An error occurred, cannot fetch comments. Please try again later.');
  111. break;
  112. }
  113. }
  114. });
  115. },
  116. loadMore(e) {
  117. e.preventDefault();
  118. if(this.pagination.total_pages == 1 || this.pagination.current_page == this.pagination.total_pages) {
  119. $('.load-more-link').addClass('d-none');
  120. return;
  121. }
  122. $('.postCommentsLoader').removeClass('d-none');
  123. let next = this.pagination.links.next;
  124. axios.get(next)
  125. .then(response => {
  126. let self = this;
  127. let res = response.data.data;
  128. $('.postCommentsLoader').addClass('d-none');
  129. for(let i=0; i < res.length; i++) {
  130. this.results.unshift(res[i]);
  131. }
  132. this.pagination = response.data.meta.pagination;
  133. });
  134. },
  135. likeStatus(status, $event) {
  136. if($('body').hasClass('loggedIn') == false) {
  137. return;
  138. }
  139. axios.post('/i/like', {
  140. item: status.id
  141. }).then(res => {
  142. status.favourites_count = res.data.count;
  143. if(status.favourited == true) {
  144. status.favourited = false;
  145. } else {
  146. status.favourited = true;
  147. }
  148. }).catch(err => {
  149. swal('Error', 'Something went wrong, please try again later.', 'error');
  150. });
  151. }
  152. },
  153. }
  154. </script>