Playlists.vue 5.3 KB

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