RecentActivity.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <template>
  2. <div class="content recent-activity-tab">
  3. <div v-if="activities.length > 0">
  4. <h4 class="section-title">Recent activity</h4>
  5. <p class="section-description">
  6. This is a log of all actions
  7. {{ userId === myUserId ? "you have" : `${username} has` }}
  8. taken recently.
  9. </p>
  10. <hr class="section-horizontal-rule" />
  11. <div id="activity-items" @scroll="handleScroll">
  12. <activity-item
  13. class="item activity-item universal-item"
  14. v-for="activity in activities"
  15. :key="activity._id"
  16. :activity="activity"
  17. >
  18. <div slot="actions">
  19. <confirm
  20. v-if="userId === myUserId"
  21. placement="left"
  22. @confirm="hideActivity(activity._id)"
  23. >
  24. <a content="Hide Activity" v-tippy>
  25. <i class="material-icons hide-icon"
  26. >visibility_off</i
  27. >
  28. </a>
  29. </confirm>
  30. </div>
  31. </activity-item>
  32. </div>
  33. </div>
  34. <div v-else>
  35. <h3>No recent activity.</h3>
  36. </div>
  37. </div>
  38. </template>
  39. <script>
  40. import { mapState, mapGetters, mapActions } from "vuex";
  41. import Toast from "toasters";
  42. import ActivityItem from "@/components/ActivityItem.vue";
  43. import ws from "@/ws";
  44. import Confirm from "@/components/Confirm.vue";
  45. export default {
  46. components: { ActivityItem, Confirm },
  47. props: {
  48. userId: {
  49. type: String,
  50. default: ""
  51. }
  52. },
  53. data() {
  54. return {
  55. username: "",
  56. activities: [],
  57. position: 1,
  58. maxPosition: 1,
  59. offsettedFromNextSet: 0,
  60. isGettingSet: false
  61. };
  62. },
  63. computed: {
  64. ...mapState({
  65. ...mapState("modalVisibility", {
  66. modals: state => state.modals.station
  67. }),
  68. myUserId: state => state.user.auth.userId
  69. }),
  70. ...mapGetters({
  71. socket: "websockets/getSocket"
  72. })
  73. },
  74. mounted() {
  75. if (this.myUserId !== this.userId) {
  76. ws.onConnect(() =>
  77. this.socket.dispatch(
  78. "apis.joinRoom",
  79. `profile-${this.userId}-activities`
  80. )
  81. );
  82. this.getUsernameFromId(this.userId).then(username => {
  83. if (username) this.username = username;
  84. });
  85. }
  86. this.socket.dispatch("activities.length", this.userId, res => {
  87. if (res.status === "success") {
  88. this.maxPosition = Math.ceil(res.data.length / 15) + 1;
  89. this.getSet();
  90. }
  91. });
  92. this.socket.on("event:activity.create", res => {
  93. this.activities.unshift(res.data.activity);
  94. this.offsettedFromNextSet += 1;
  95. });
  96. this.socket.on("event:activity.hide", res => {
  97. this.activities = this.activities.filter(
  98. activity => activity._id !== res.data.activityId
  99. );
  100. this.offsettedFromNextSet -= 1;
  101. });
  102. this.socket.on("event:activity.removeAllForUser", () => {
  103. this.activities = [];
  104. this.position = 1;
  105. this.maxPosition = 1;
  106. this.offsettedFromNextSet = 0;
  107. });
  108. },
  109. methods: {
  110. hideActivity(activityId) {
  111. this.socket.dispatch("activities.hideActivity", activityId, res => {
  112. if (res.status !== "success") new Toast(res.message);
  113. });
  114. },
  115. getSet() {
  116. if (this.isGettingSet) return;
  117. if (this.position >= this.maxPosition) return;
  118. this.isGettingSet = true;
  119. this.socket.dispatch(
  120. "activities.getSet",
  121. this.userId,
  122. this.position,
  123. this.offsettedFromNextSet,
  124. res => {
  125. if (res.status === "success") {
  126. this.activities.push(...res.data.activities);
  127. this.position += 1;
  128. }
  129. this.isGettingSet = false;
  130. }
  131. );
  132. },
  133. handleScroll(event) {
  134. const scrollPosition =
  135. event.target.clientHeight + event.target.scrollTop;
  136. const bottomPosition = event.target.scrollHeight;
  137. if (this.loadAllSongs) return false;
  138. if (scrollPosition + 100 >= bottomPosition) this.getSet();
  139. return this.maxPosition === this.position;
  140. },
  141. ...mapActions("user/auth", ["getUsernameFromId"])
  142. }
  143. };
  144. </script>
  145. <style lang="scss" scoped>
  146. #activity-items {
  147. overflow: auto;
  148. min-height: auto;
  149. max-height: 570px;
  150. }
  151. </style>