ShareModal.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <template>
  2. <div>
  3. <b-modal
  4. ref="sharesModal"
  5. centered
  6. size="md"
  7. :scrollable="true"
  8. hide-footer
  9. header-class="py-2"
  10. body-class="p-0"
  11. title-class="w-100 text-center pl-4 font-weight-bold"
  12. title-tag="p"
  13. title="Shared By">
  14. <div v-if="isLoading" class="likes-loader list-group border-top-0" style="max-height: 500px;">
  15. <like-placeholder />
  16. </div>
  17. <div v-else>
  18. <div v-if="!likes.length" class="d-flex justify-content-center align-items-center" style="height: 140px;">
  19. <p class="font-weight-bold mb-0">Nobody has shared this yet!</p>
  20. </div>
  21. <div v-else class="list-group" style="max-height: 500px;">
  22. <div v-for="(account, index) in likes" class="list-group-item border-left-0 border-right-0 px-3" :class="[ index === 0 ? 'border-top-0' : '']">
  23. <div class="media align-items-center">
  24. <img :src="account.avatar" width="40" height="40" style="border-radius: 8px;" class="mr-3 shadow-sm" onerror="this.src='/storage/avatars/default.jpg?v=0';this.onerror=null;">
  25. <div class="media-body">
  26. <p class="mb-0 text-truncate"><a :href="account.url" class="text-dark font-weight-bold text-decoration-none" @click.prevent="goToProfile(account)">{{ getUsername(account) }}</a></p>
  27. <p class="mb-0 mt-n1 text-dark font-weight-bold small text-break">&commat;{{ account.acct }}</p>
  28. </div>
  29. <div>
  30. <button
  31. v-if="account.id == user.id"
  32. class="btn btn-outline-muted rounded-pill btn-sm font-weight-bold"
  33. @click="goToProfile(profile)"
  34. style="width:110px;">
  35. View Profile
  36. </button>
  37. <button
  38. v-else-if="account.follows"
  39. class="btn btn-outline-muted rounded-pill btn-sm font-weight-bold"
  40. :disabled="isUpdatingFollowState"
  41. @click="handleUnfollow(index)"
  42. style="width:110px;">
  43. <span v-if="isUpdatingFollowState && followStateIndex === index">
  44. <b-spinner small />
  45. </span>
  46. <span v-else>Following</span>
  47. </button>
  48. <button
  49. v-else-if="!account.follows"
  50. class="btn btn-primary rounded-pill btn-sm font-weight-bold"
  51. :disabled="isUpdatingFollowState"
  52. @click="handleFollow(index)"
  53. style="width:110px;">
  54. <span v-if="isUpdatingFollowState && followStateIndex === index">
  55. <b-spinner small />
  56. </span>
  57. <span v-else>Follow</span>
  58. </button>
  59. </div>
  60. </div>
  61. </div>
  62. <div v-if="canLoadMore">
  63. <intersect @enter="enterIntersect">
  64. <like-placeholder class="border-top-0" />
  65. </intersect>
  66. <like-placeholder />
  67. </div>
  68. </div>
  69. </div>
  70. </b-modal>
  71. </div>
  72. </template>
  73. <script type="text/javascript">
  74. import Intersect from 'vue-intersect'
  75. import LikePlaceholder from './LikeListPlaceholder.vue';
  76. import { parseLinkHeader } from '@web3-storage/parse-link-header';
  77. export default {
  78. props: {
  79. status: {
  80. type: Object
  81. },
  82. profile: {
  83. type: Object
  84. }
  85. },
  86. components: {
  87. "intersect": Intersect,
  88. "like-placeholder": LikePlaceholder
  89. },
  90. data() {
  91. return {
  92. isOpen: false,
  93. isLoading: true,
  94. canLoadMore: false,
  95. isFetchingMore: false,
  96. likes: [],
  97. ids: [],
  98. page: undefined,
  99. isUpdatingFollowState: false,
  100. followStateIndex: undefined,
  101. user: window._sharedData.user
  102. }
  103. },
  104. methods: {
  105. clear() {
  106. this.isOpen = false;
  107. this.isLoading = true;
  108. this.canLoadMore = false;
  109. this.isFetchingMore = false;
  110. this.likes = [];
  111. this.ids = [];
  112. this.page = undefined;
  113. },
  114. fetchShares() {
  115. axios.get('/api/v1/statuses/'+this.status.id+'/reblogged_by', {
  116. params: {
  117. limit: 40
  118. }
  119. })
  120. .then(res => {
  121. this.ids = res.data.map(a => a.id);
  122. this.likes = res.data;
  123. if(res.headers && res.headers.link) {
  124. const links = parseLinkHeader(res.headers.link);
  125. if(links.next) {
  126. this.page = links.next.cursor;
  127. this.canLoadMore = true;
  128. } else {
  129. this.canLoadMore = false;
  130. }
  131. }
  132. this.isLoading = false;
  133. });
  134. },
  135. open() {
  136. if(this.page) {
  137. this.clear();
  138. }
  139. this.isOpen = true;
  140. this.fetchShares();
  141. this.$refs.sharesModal.show();
  142. },
  143. enterIntersect() {
  144. if(this.isFetchingMore) {
  145. return;
  146. }
  147. this.isFetchingMore = true;
  148. axios.get('/api/v1/statuses/'+this.status.id+'/reblogged_by', {
  149. params: {
  150. limit: 10,
  151. cursor: this.page
  152. }
  153. }).then(res => {
  154. if(!res.data || !res.data.length) {
  155. this.canLoadMore = false;
  156. this.isFetchingMore = false;
  157. return;
  158. }
  159. res.data.forEach(user => {
  160. if(this.ids.indexOf(user.id) == -1) {
  161. this.ids.push(user.id);
  162. this.likes.push(user);
  163. }
  164. })
  165. if(res.headers && res.headers.link) {
  166. const links = parseLinkHeader(res.headers.link);
  167. if(links.next) {
  168. this.page = links.next.cursor;
  169. } else {
  170. this.canLoadMore = false;
  171. }
  172. }
  173. this.isFetchingMore = false;
  174. })
  175. },
  176. getUsername(account) {
  177. return account.display_name ? account.display_name : account.username;
  178. },
  179. goToProfile(account) {
  180. this.$router.push({
  181. name: 'profile',
  182. path: `/i/web/profile/${account.id}`,
  183. params: {
  184. id: account.id,
  185. cachedProfile: account,
  186. cachedUser: this.profile
  187. }
  188. })
  189. },
  190. handleFollow(index) {
  191. event.currentTarget.blur();
  192. this.followStateIndex = index;
  193. this.isUpdatingFollowState = true;
  194. let account = this.likes[index];
  195. axios.post('/api/v1/accounts/' + account.id + '/follow')
  196. .then(res => {
  197. this.likes[index].follows = true;
  198. this.followStateIndex = undefined;
  199. this.isUpdatingFollowState = false;
  200. });
  201. },
  202. handleUnfollow(index) {
  203. event.currentTarget.blur();
  204. this.followStateIndex = index;
  205. this.isUpdatingFollowState = true;
  206. let account = this.likes[index];
  207. axios.post('/api/v1/accounts/' + account.id + '/unfollow')
  208. .then(res => {
  209. this.likes[index].follows = false;
  210. this.followStateIndex = undefined;
  211. this.isUpdatingFollowState = false;
  212. });
  213. }
  214. }
  215. }
  216. </script>