Profile.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <template>
  2. <div class="profile-timeline-component">
  3. <div v-if="isLoaded" class="container-fluid mt-3">
  4. <div class="row">
  5. <div class="col-md-3 d-md-block px-md-3 px-xl-5">
  6. <profile-sidebar
  7. :profile="profile"
  8. :relationship="relationship"
  9. :user="curUser"
  10. v-on:back="goBack"
  11. v-on:toggletab="toggleTab"
  12. v-on:updateRelationship="updateRelationship"
  13. @follow="follow"
  14. @unfollow="unfollow" />
  15. </div>
  16. <div class="col-md-8 px-md-5">
  17. <component
  18. v-bind:is="getTabComponentName()"
  19. :key="getTabComponentName() + profile.id"
  20. :profile="profile"
  21. :relationship="relationship" />
  22. </div>
  23. </div>
  24. <drawer />
  25. </div>
  26. </div>
  27. </template>
  28. <script type="text/javascript">
  29. import Drawer from './partials/drawer.vue';
  30. import ProfileFeed from './partials/profile/ProfileFeed.vue';
  31. import ProfileSidebar from './partials/profile/ProfileSidebar.vue';
  32. import ProfileFollowers from './partials/profile/ProfileFollowers.vue';
  33. import ProfileFollowing from './partials/profile/ProfileFollowing.vue';
  34. export default {
  35. props: {
  36. id: {
  37. type: String
  38. },
  39. profileId: {
  40. type: String
  41. },
  42. username: {
  43. type: String
  44. },
  45. cachedProfile: {
  46. type: Object
  47. },
  48. cachedUser: {
  49. type: Object
  50. }
  51. },
  52. components: {
  53. "drawer": Drawer,
  54. "profile-feed": ProfileFeed,
  55. "profile-sidebar": ProfileSidebar,
  56. "profile-followers": ProfileFollowers,
  57. "profile-following": ProfileFollowing
  58. },
  59. data() {
  60. return {
  61. isLoaded: false,
  62. curUser: undefined,
  63. tab: "index",
  64. profile: undefined,
  65. relationship: undefined
  66. }
  67. },
  68. mounted() {
  69. this.init();
  70. },
  71. watch: {
  72. '$route': 'init'
  73. },
  74. methods: {
  75. init() {
  76. this.tab = 'index';
  77. this.isLoaded = false;
  78. this.relationship = undefined;
  79. this.owner = false;
  80. if(this.cachedProfile && this.cachedUser) {
  81. this.curUser = this.cachedUser;
  82. this.profile = this.cachedProfile;
  83. // this.fetchPosts();
  84. // this.isLoaded = true;
  85. this.fetchRelationship();
  86. } else {
  87. this.curUser = window._sharedData.user;
  88. this.fetchProfile();
  89. }
  90. },
  91. getTabComponentName() {
  92. switch(this.tab) {
  93. case 'index':
  94. return "profile-feed";
  95. break;
  96. default:
  97. return `profile-${this.tab}`;
  98. break;
  99. }
  100. },
  101. fetchProfile() {
  102. let id = this.profileId ? this.profileId : this.id;
  103. axios.get('/api/pixelfed/v1/accounts/' + id)
  104. .then(res => {
  105. this.profile = res.data;
  106. if(res.data.id == this.curUser.id) {
  107. this.owner = true;
  108. // this.isLoaded = true;
  109. // this.loaded();
  110. // this.fetchPosts();
  111. this.fetchRelationship();
  112. } else {
  113. this.owner = false;
  114. this.fetchRelationship();
  115. }
  116. })
  117. .catch(err => {
  118. this.$router.push('/i/web/404');
  119. });
  120. },
  121. fetchRelationship() {
  122. if(this.owner) {
  123. this.relationship = {};
  124. this.isLoaded = true;
  125. return;
  126. }
  127. axios.get('/api/v1/accounts/relationships', {
  128. params: {
  129. 'id[]': this.profile.id
  130. }
  131. }).then(res => {
  132. this.relationship = res.data[0];
  133. this.isLoaded = true;
  134. })
  135. },
  136. toggleTab(tab) {
  137. this.tab = tab;
  138. },
  139. goBack() {
  140. this.$router.go(-1);
  141. },
  142. unfollow() {
  143. axios.post('/api/v1/accounts/' + this.profile.id + '/unfollow')
  144. .then(res => {
  145. this.$store.commit('updateRelationship', [res.data])
  146. this.relationship = res.data;
  147. if(this.profile.locked) {
  148. location.reload();
  149. }
  150. this.profile.followers_count--;
  151. }).catch(err => {
  152. swal('Oops!', 'An error occured when attempting to unfollow this account.', 'error');
  153. this.relationship.following = true;
  154. });
  155. },
  156. follow() {
  157. axios.post('/api/v1/accounts/' + this.profile.id + '/follow')
  158. .then(res => {
  159. this.$store.commit('updateRelationship', [res.data])
  160. this.relationship = res.data;
  161. if(this.profile.locked) {
  162. this.relationship.requested = true;
  163. }
  164. this.profile.followers_count++;
  165. }).catch(err => {
  166. swal('Oops!', 'An error occured when attempting to follow this account.', 'error');
  167. this.relationship.following = false;
  168. });
  169. },
  170. updateRelationship(val) {
  171. this.relationship = val;
  172. }
  173. }
  174. }
  175. </script>
  176. <style lang="scss" scoped>
  177. .profile-timeline-component {
  178. margin-bottom: 10rem;
  179. }
  180. </style>