AddToPlaylistDropdown.vue 3.7 KB

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