Playlists.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <div class="content playlists-tab">
  3. <create-playlist v-if="modals.createPlaylist" />
  4. <div v-if="playlists.length > 0">
  5. <h4 class="section-title">
  6. {{ myUserId === userId ? "My" : null }}
  7. Playlists
  8. </h4>
  9. <p class="section-description">
  10. View
  11. {{
  12. userId === myUserId
  13. ? "and manage your personal"
  14. : `${username}'s`
  15. }}
  16. playlists.
  17. </p>
  18. <hr class="section-horizontal-rule" />
  19. <draggable
  20. class="menu-list scrollable-list"
  21. v-if="playlists.length > 0"
  22. v-model="playlists"
  23. v-bind="dragOptions"
  24. @start="drag = true"
  25. @end="drag = false"
  26. @change="savePlaylistOrder"
  27. >
  28. <transition-group
  29. type="transition"
  30. :name="!drag ? 'draggable-list-transition' : null"
  31. >
  32. <div
  33. :class="{
  34. item: true,
  35. 'item-draggable': myUserId === userId
  36. }"
  37. v-for="playlist in playlists"
  38. :key="playlist._id"
  39. >
  40. <playlist-item
  41. v-if="
  42. playlist.privacy === 'public' ||
  43. (playlist.privacy === 'private' &&
  44. playlist.createdBy === userId)
  45. "
  46. :playlist="playlist"
  47. >
  48. <div slot="actions">
  49. <i
  50. v-if="myUserId === userId"
  51. @click="showPlaylist(playlist._id)"
  52. class="material-icons edit-icon"
  53. content="Edit Playlist"
  54. v-tippy
  55. >edit</i
  56. >
  57. <i
  58. v-else
  59. @click="showPlaylist(playlist._id)"
  60. class="material-icons view-icon"
  61. content="View Playlist"
  62. v-tippy
  63. >visibility</i
  64. >
  65. </div>
  66. </playlist-item>
  67. </div>
  68. </transition-group>
  69. </draggable>
  70. <button
  71. v-if="myUserId === userId"
  72. class="button is-primary"
  73. id="create-new-playlist-button"
  74. @click="
  75. openModal({
  76. sector: 'station',
  77. modal: 'createPlaylist'
  78. })
  79. "
  80. >
  81. Create new playlist
  82. </button>
  83. </div>
  84. <div v-else>
  85. <h3>No playlists here.</h3>
  86. </div>
  87. </div>
  88. </template>
  89. <script>
  90. import draggable from "vuedraggable";
  91. import { mapActions, mapState, mapGetters } from "vuex";
  92. import PlaylistItem from "@/components/PlaylistItem.vue";
  93. import SortablePlaylists from "@/mixins/SortablePlaylists.vue";
  94. import ws from "@/ws";
  95. export default {
  96. components: {
  97. PlaylistItem,
  98. draggable,
  99. CreatePlaylist: () => import("@/components/modals/CreatePlaylist.vue")
  100. },
  101. mixins: [SortablePlaylists],
  102. props: {
  103. userId: {
  104. type: String,
  105. default: ""
  106. },
  107. username: {
  108. type: String,
  109. default: ""
  110. }
  111. },
  112. computed: {
  113. ...mapState({
  114. ...mapState("modalVisibility", {
  115. modals: state => state.modals.station
  116. }),
  117. myUserId: state => state.user.auth.userId
  118. }),
  119. playlists: {
  120. get() {
  121. return this.$store.state.user.playlists.playlists;
  122. },
  123. set(playlists) {
  124. this.$store.commit("user/playlists/setPlaylists", playlists);
  125. }
  126. },
  127. ...mapGetters({
  128. socket: "websockets/getSocket"
  129. })
  130. },
  131. mounted() {
  132. if (
  133. this.$route.query.tab === "recent-activity" ||
  134. this.$route.query.tab === "playlists"
  135. )
  136. this.tab = this.$route.query.tab;
  137. if (this.myUserId !== this.userId) {
  138. ws.onConnect(() =>
  139. this.socket.dispatch(
  140. "apis.joinRoom",
  141. `profile-${this.userId}-playlists`,
  142. () => {}
  143. )
  144. );
  145. }
  146. this.socket.dispatch("playlists.indexForUser", this.userId, res => {
  147. if (res.status === "success") this.setPlaylists(res.data.playlists);
  148. this.orderOfPlaylists = this.calculatePlaylistOrder(); // order in regards to the database
  149. });
  150. this.socket.on("event:playlist.create", playlist => {
  151. this.playlists.push(playlist);
  152. });
  153. this.socket.on("event:playlist.delete", playlistId => {
  154. this.playlists.forEach((playlist, index) => {
  155. if (playlist._id === playlistId) {
  156. this.playlists.splice(index, 1);
  157. }
  158. });
  159. });
  160. this.socket.on("event:playlist.addSong", data => {
  161. this.playlists.forEach((playlist, index) => {
  162. if (playlist._id === data.playlistId) {
  163. this.playlists[index].songs.push(data.song);
  164. }
  165. });
  166. });
  167. this.socket.on("event:playlist.removeSong", data => {
  168. this.playlists.forEach((playlist, index) => {
  169. if (playlist._id === data.playlistId) {
  170. this.playlists[index].songs.forEach((song, index2) => {
  171. if (song.youtubeId === data.youtubeId) {
  172. this.playlists[index].songs.splice(index2, 1);
  173. }
  174. });
  175. }
  176. });
  177. });
  178. this.socket.on("event:playlist.updateDisplayName", data => {
  179. this.playlists.forEach((playlist, index) => {
  180. if (playlist._id === data.playlistId) {
  181. this.playlists[index].displayName = data.displayName;
  182. }
  183. });
  184. });
  185. this.socket.on("event:playlist.updatePrivacy", data => {
  186. this.playlists.forEach((playlist, index) => {
  187. if (playlist._id === data.playlist._id) {
  188. this.playlists[index].privacy = data.playlist.privacy;
  189. }
  190. });
  191. });
  192. this.socket.on(
  193. "event:user.orderOfPlaylists.changed",
  194. orderOfPlaylists => {
  195. const sortedPlaylists = [];
  196. this.playlists.forEach(playlist => {
  197. sortedPlaylists[
  198. orderOfPlaylists.indexOf(playlist._id)
  199. ] = playlist;
  200. });
  201. this.playlists = sortedPlaylists;
  202. this.orderOfPlaylists = this.calculatePlaylistOrder();
  203. }
  204. );
  205. },
  206. methods: {
  207. showPlaylist(playlistId) {
  208. this.editPlaylist(playlistId);
  209. this.openModal({ sector: "station", modal: "editPlaylist" });
  210. },
  211. ...mapActions("modalVisibility", ["openModal"]),
  212. ...mapActions("user/playlists", ["editPlaylist", "setPlaylists"])
  213. }
  214. };
  215. </script>