AddToPlaylistDropdown.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <template>
  2. <tippy
  3. class="addToPlaylistDropdown"
  4. :touch="true"
  5. :interactive="true"
  6. :placement="placement"
  7. theme="addToPlaylist"
  8. ref="dropdown"
  9. trigger="click"
  10. append-to="parent"
  11. @show="
  12. () => {
  13. $parent.showPlaylistDropdown = true;
  14. }
  15. "
  16. @hide="
  17. () => {
  18. $parent.showPlaylistDropdown = false;
  19. }
  20. "
  21. >
  22. <slot name="button" ref="trigger" />
  23. <template #content>
  24. <div class="nav-dropdown-items" v-if="playlists.length > 0">
  25. <button
  26. class="nav-item"
  27. href="#"
  28. v-for="(playlist, index) in playlists"
  29. :key="playlist._id"
  30. @click.prevent="toggleSongInPlaylist(index)"
  31. :title="playlist.displayName"
  32. >
  33. <p class="control is-expanded checkbox-control">
  34. <label class="switch">
  35. <input
  36. type="checkbox"
  37. :id="index"
  38. :checked="hasSong(playlist)"
  39. @click="toggleSongInPlaylist(index)"
  40. />
  41. <span class="slider round"></span>
  42. </label>
  43. <label :for="index">
  44. <span></span>
  45. <p>{{ playlist.displayName }}</p>
  46. </label>
  47. </p>
  48. </button>
  49. </div>
  50. <p v-else>You haven't created any playlists.</p>
  51. <button
  52. id="create-playlist"
  53. class="button is-primary"
  54. @click="createPlaylist()"
  55. >
  56. <i class="material-icons icon-with-button"> edit </i>
  57. Create Playlist
  58. </button>
  59. </template>
  60. </tippy>
  61. </template>
  62. <script>
  63. import { mapGetters, mapState, mapActions } from "vuex";
  64. import Toast from "toasters";
  65. export default {
  66. props: {
  67. song: {
  68. type: Object,
  69. default: () => {}
  70. },
  71. placement: {
  72. type: String,
  73. default: "left"
  74. }
  75. },
  76. computed: {
  77. ...mapGetters({
  78. socket: "websockets/getSocket"
  79. }),
  80. ...mapState({
  81. fetchedPlaylists: state => state.user.playlists.fetchedPlaylists
  82. }),
  83. playlists() {
  84. return this.$store.state.user.playlists.playlists.filter(
  85. playlist => playlist.isUserModifiable
  86. );
  87. }
  88. },
  89. mounted() {
  90. if (!this.fetchedPlaylists)
  91. this.socket.dispatch("playlists.indexMyPlaylists", true, res => {
  92. if (res.status === "success")
  93. if (!this.fetchedPlaylists)
  94. this.setPlaylists(res.data.playlists);
  95. });
  96. this.socket.on(
  97. "event:playlist.created",
  98. res => this.addPlaylist(res.data.playlist),
  99. { replaceable: true }
  100. );
  101. this.socket.on(
  102. "event:playlist.deleted",
  103. res => this.removePlaylist(res.data.playlistId),
  104. { replaceable: true }
  105. );
  106. this.socket.on(
  107. "event:playlist.displayName.updated",
  108. res => {
  109. this.playlists.forEach((playlist, index) => {
  110. if (playlist._id === res.data.playlistId) {
  111. this.playlists[index].displayName =
  112. res.data.displayName;
  113. }
  114. });
  115. },
  116. { replaceable: true }
  117. );
  118. },
  119. methods: {
  120. toggleSongInPlaylist(playlistIndex) {
  121. const playlist = this.playlists[playlistIndex];
  122. if (!this.hasSong(playlist)) {
  123. this.socket.dispatch(
  124. "playlists.addSongToPlaylist",
  125. false,
  126. this.song.youtubeId,
  127. playlist._id,
  128. res => new Toast(res.message)
  129. );
  130. } else {
  131. this.socket.dispatch(
  132. "playlists.removeSongFromPlaylist",
  133. this.song.youtubeId,
  134. playlist._id,
  135. res => new Toast(res.message)
  136. );
  137. }
  138. },
  139. hasSong(playlist) {
  140. return (
  141. playlist.songs.map(song => song._id).indexOf(this.song._id) !==
  142. -1
  143. );
  144. },
  145. createPlaylist() {
  146. this.$refs.dropdown.tippy.setProps({
  147. zIndex: 0,
  148. hideOnClick: false
  149. });
  150. window.addToPlaylistDropdown = this.$refs.dropdown;
  151. this.openModal("createPlaylist");
  152. },
  153. ...mapActions("user/playlists", [
  154. "setPlaylists",
  155. "addPlaylist",
  156. "removePlaylist"
  157. ]),
  158. ...mapActions("modalVisibility", ["openModal"])
  159. }
  160. };
  161. </script>
  162. <style lang="scss" scoped>
  163. .nav-dropdown-items button .control {
  164. margin-bottom: 0 !important;
  165. }
  166. #create-playlist {
  167. margin-top: 10px;
  168. i {
  169. color: #fff;
  170. }
  171. }
  172. </style>