AddToPlaylistDropdown.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. import ws from "@/ws";
  66. export default {
  67. props: {
  68. song: {
  69. type: Object,
  70. default: () => {}
  71. },
  72. placement: {
  73. type: String,
  74. default: "left"
  75. }
  76. },
  77. computed: {
  78. ...mapGetters({
  79. socket: "websockets/getSocket"
  80. }),
  81. ...mapState({
  82. fetchedPlaylists: state => state.user.playlists.fetchedPlaylists
  83. }),
  84. playlists() {
  85. return this.$store.state.user.playlists.playlists.filter(
  86. playlist => playlist.isUserModifiable
  87. );
  88. }
  89. },
  90. mounted() {
  91. ws.onConnect(this.init);
  92. this.socket.on(
  93. "event:playlist.created",
  94. res => this.addPlaylist(res.data.playlist),
  95. { replaceable: true }
  96. );
  97. this.socket.on(
  98. "event:playlist.deleted",
  99. res => this.removePlaylist(res.data.playlistId),
  100. { replaceable: true }
  101. );
  102. this.socket.on(
  103. "event:playlist.displayName.updated",
  104. res => {
  105. this.playlists.forEach((playlist, index) => {
  106. if (playlist._id === res.data.playlistId) {
  107. this.playlists[index].displayName =
  108. res.data.displayName;
  109. }
  110. });
  111. },
  112. { replaceable: true }
  113. );
  114. },
  115. methods: {
  116. init() {
  117. if (!this.fetchedPlaylists)
  118. this.socket.dispatch(
  119. "playlists.indexMyPlaylists",
  120. true,
  121. res => {
  122. if (res.status === "success")
  123. if (!this.fetchedPlaylists)
  124. this.setPlaylists(res.data.playlists);
  125. }
  126. );
  127. },
  128. toggleSongInPlaylist(playlistIndex) {
  129. const playlist = this.playlists[playlistIndex];
  130. if (!this.hasSong(playlist)) {
  131. this.socket.dispatch(
  132. "playlists.addSongToPlaylist",
  133. false,
  134. this.song.youtubeId,
  135. playlist._id,
  136. res => new Toast(res.message)
  137. );
  138. } else {
  139. this.socket.dispatch(
  140. "playlists.removeSongFromPlaylist",
  141. this.song.youtubeId,
  142. playlist._id,
  143. res => new Toast(res.message)
  144. );
  145. }
  146. },
  147. hasSong(playlist) {
  148. return (
  149. playlist.songs
  150. .map(song => song.youtubeId)
  151. .indexOf(this.song.youtubeId) !== -1
  152. );
  153. },
  154. createPlaylist() {
  155. this.$refs.dropdown.tippy.setProps({
  156. zIndex: 0,
  157. hideOnClick: false
  158. });
  159. window.addToPlaylistDropdown = this.$refs.dropdown;
  160. this.openModal("createPlaylist");
  161. },
  162. ...mapActions("user/playlists", [
  163. "setPlaylists",
  164. "addPlaylist",
  165. "removePlaylist"
  166. ]),
  167. ...mapActions("modalVisibility", ["openModal"])
  168. }
  169. };
  170. </script>
  171. <style lang="scss" scoped>
  172. .nav-dropdown-items button .control {
  173. margin-bottom: 0 !important;
  174. }
  175. #create-playlist {
  176. margin-top: 10px;
  177. i {
  178. color: #fff;
  179. }
  180. }
  181. </style>