Playlists.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. >edit</i
  54. >
  55. <i
  56. v-else
  57. @click="showPlaylist(playlist._id)"
  58. class="material-icons view-icon"
  59. >visibility</i
  60. >
  61. </div>
  62. </playlist-item>
  63. </div>
  64. </transition-group>
  65. </draggable>
  66. <button
  67. v-if="myUserId === userId"
  68. class="button is-primary"
  69. id="create-new-playlist-button"
  70. @click="
  71. openModal({
  72. sector: 'station',
  73. modal: 'createPlaylist'
  74. })
  75. "
  76. >
  77. Create new playlist
  78. </button>
  79. </div>
  80. <div v-else>
  81. <h3>No playlists here.</h3>
  82. </div>
  83. </div>
  84. </template>
  85. <script>
  86. import draggable from "vuedraggable";
  87. import { mapActions, mapState, mapGetters } from "vuex";
  88. import ws from "../../../ws";
  89. import SortablePlaylists from "../../../mixins/SortablePlaylists.vue";
  90. import PlaylistItem from "../../../components/ui/PlaylistItem.vue";
  91. export default {
  92. components: {
  93. PlaylistItem,
  94. draggable,
  95. CreatePlaylist: () =>
  96. import("../../../components/modals/CreatePlaylist.vue")
  97. },
  98. mixins: [SortablePlaylists],
  99. props: {
  100. userId: {
  101. type: String,
  102. default: ""
  103. },
  104. username: {
  105. type: String,
  106. default: ""
  107. }
  108. },
  109. computed: {
  110. ...mapState({
  111. ...mapState("modalVisibility", {
  112. modals: state => state.modals.station
  113. }),
  114. myUserId: state => state.user.auth.userId
  115. }),
  116. playlists: {
  117. get() {
  118. return this.$store.state.user.playlists.playlists;
  119. },
  120. set(playlists) {
  121. this.$store.commit("user/playlists/setPlaylists", playlists);
  122. }
  123. },
  124. ...mapGetters({
  125. socket: "websockets/getSocket"
  126. })
  127. },
  128. mounted() {
  129. if (
  130. this.$route.query.tab === "recent-activity" ||
  131. this.$route.query.tab === "playlists"
  132. )
  133. this.tab = this.$route.query.tab;
  134. if (this.myUserId !== this.userId) {
  135. ws.onConnect(() =>
  136. this.socket.dispatch(
  137. "apis.joinRoom",
  138. `profile-${this.userId}-playlists`,
  139. () => {}
  140. )
  141. );
  142. }
  143. this.socket.dispatch("playlists.indexForUser", this.userId, res => {
  144. if (res.status === "success") this.setPlaylists(res.data);
  145. this.orderOfPlaylists = this.calculatePlaylistOrder(); // order in regards to the database
  146. });
  147. this.socket.on("event:playlist.create", playlist => {
  148. this.playlists.push(playlist);
  149. });
  150. this.socket.on("event:playlist.delete", playlistId => {
  151. this.playlists.forEach((playlist, index) => {
  152. if (playlist._id === playlistId) {
  153. this.playlists.splice(index, 1);
  154. }
  155. });
  156. });
  157. this.socket.on("event:playlist.addSong", data => {
  158. this.playlists.forEach((playlist, index) => {
  159. if (playlist._id === data.playlistId) {
  160. this.playlists[index].songs.push(data.song);
  161. }
  162. });
  163. });
  164. this.socket.on("event:playlist.removeSong", data => {
  165. this.playlists.forEach((playlist, index) => {
  166. if (playlist._id === data.playlistId) {
  167. this.playlists[index].songs.forEach((song, index2) => {
  168. if (song.songId === data.songId) {
  169. this.playlists[index].songs.splice(index2, 1);
  170. }
  171. });
  172. }
  173. });
  174. });
  175. this.socket.on("event:playlist.updateDisplayName", data => {
  176. this.playlists.forEach((playlist, index) => {
  177. if (playlist._id === data.playlistId) {
  178. this.playlists[index].displayName = data.displayName;
  179. }
  180. });
  181. });
  182. this.socket.on("event:playlist.updatePrivacy", data => {
  183. this.playlists.forEach((playlist, index) => {
  184. if (playlist._id === data.playlist._id) {
  185. this.playlists[index].privacy = data.playlist.privacy;
  186. }
  187. });
  188. });
  189. this.socket.on(
  190. "event:user.orderOfPlaylists.changed",
  191. orderOfPlaylists => {
  192. const sortedPlaylists = [];
  193. this.playlists.forEach(playlist => {
  194. sortedPlaylists[
  195. orderOfPlaylists.indexOf(playlist._id)
  196. ] = playlist;
  197. });
  198. this.playlists = sortedPlaylists;
  199. this.orderOfPlaylists = this.calculatePlaylistOrder();
  200. }
  201. );
  202. },
  203. methods: {
  204. showPlaylist(playlistId) {
  205. this.editPlaylist(playlistId);
  206. this.openModal({ sector: "station", modal: "editPlaylist" });
  207. },
  208. ...mapActions("modalVisibility", ["openModal"]),
  209. ...mapActions("user/playlists", ["editPlaylist", "setPlaylists"])
  210. }
  211. };
  212. </script>