PostComments.vue 5.5 KB

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