Playlists.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 ws from "../../../ws";
  93. import SortablePlaylists from "../../../mixins/SortablePlaylists.vue";
  94. import PlaylistItem from "../../../components/PlaylistItem.vue";
  95. export default {
  96. components: {
  97. PlaylistItem,
  98. draggable,
  99. CreatePlaylist: () =>
  100. import("../../../components/modals/CreatePlaylist.vue")
  101. },
  102. mixins: [SortablePlaylists],
  103. props: {
  104. userId: {
  105. type: String,
  106. default: ""
  107. },
  108. username: {
  109. type: String,
  110. default: ""
  111. }
  112. },
  113. computed: {
  114. ...mapState({
  115. ...mapState("modalVisibility", {
  116. modals: state => state.modals.station
  117. }),
  118. myUserId: state => state.user.auth.userId
  119. }),
  120. playlists: {
  121. get() {
  122. return this.$store.state.user.playlists.playlists;
  123. },
  124. set(playlists) {
  125. this.$store.commit("user/playlists/setPlaylists", playlists);
  126. }
  127. },
  128. ...mapGetters({
  129. socket: "websockets/getSocket"
  130. })
  131. },
  132. mounted() {
  133. if (
  134. this.$route.query.tab === "recent-activity" ||
  135. this.$route.query.tab === "playlists"
  136. )
  137. this.tab = this.$route.query.tab;
  138. if (this.myUserId !== this.userId) {
  139. ws.onConnect(() =>
  140. this.socket.dispatch(
  141. "apis.joinRoom",
  142. `profile-${this.userId}-playlists`,
  143. () => {}
  144. )
  145. );
  146. }
  147. this.socket.dispatch("playlists.indexForUser", this.userId, res => {
  148. if (res.status === "success") this.setPlaylists(res.data);
  149. this.orderOfPlaylists = this.calculatePlaylistOrder(); // order in regards to the database
  150. });
  151. this.socket.on("event:playlist.create", playlist => {
  152. this.playlists.push(playlist);
  153. });
  154. this.socket.on("event:playlist.delete", playlistId => {
  155. this.playlists.forEach((playlist, index) => {
  156. if (playlist._id === playlistId) {
  157. this.playlists.splice(index, 1);
  158. }
  159. });
  160. });
  161. this.socket.on("event:playlist.addSong", data => {
  162. this.playlists.forEach((playlist, index) => {
  163. if (playlist._id === data.playlistId) {
  164. this.playlists[index].songs.push(data.song);
  165. }
  166. });
  167. });
  168. this.socket.on("event:playlist.removeSong", data => {
  169. this.playlists.forEach((playlist, index) => {
  170. if (playlist._id === data.playlistId) {
  171. this.playlists[index].songs.forEach((song, index2) => {
  172. if (song.songId === data.songId) {
  173. this.playlists[index].songs.splice(index2, 1);
  174. }
  175. });
  176. }
  177. });
  178. });
  179. this.socket.on("event:playlist.updateDisplayName", data => {
  180. this.playlists.forEach((playlist, index) => {
  181. if (playlist._id === data.playlistId) {
  182. this.playlists[index].displayName = data.displayName;
  183. }
  184. });
  185. });
  186. this.socket.on("event:playlist.updatePrivacy", data => {
  187. this.playlists.forEach((playlist, index) => {
  188. if (playlist._id === data.playlist._id) {
  189. this.playlists[index].privacy = data.playlist.privacy;
  190. }
  191. });
  192. });
  193. this.socket.on(
  194. "event:user.orderOfPlaylists.changed",
  195. orderOfPlaylists => {
  196. const sortedPlaylists = [];
  197. this.playlists.forEach(playlist => {
  198. sortedPlaylists[
  199. orderOfPlaylists.indexOf(playlist._id)
  200. ] = playlist;
  201. });
  202. this.playlists = sortedPlaylists;
  203. this.orderOfPlaylists = this.calculatePlaylistOrder();
  204. }
  205. );
  206. },
  207. methods: {
  208. showPlaylist(playlistId) {
  209. this.editPlaylist(playlistId);
  210. this.openModal({ sector: "station", modal: "editPlaylist" });
  211. },
  212. ...mapActions("modalVisibility", ["openModal"]),
  213. ...mapActions("user/playlists", ["editPlaylist", "setPlaylists"])
  214. }
  215. };
  216. </script>