AddToPlaylistDropdown.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. 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. fetchedPlaylists: state => state.user.playlists.fetchedPlaylists
  84. }),
  85. playlists() {
  86. return this.$store.state.user.playlists.playlists.filter(
  87. playlist => playlist.isUserModifiable
  88. );
  89. }
  90. },
  91. mounted() {
  92. ws.onConnect(this.init);
  93. this.socket.on(
  94. "event:playlist.created",
  95. res => this.addPlaylist(res.data.playlist),
  96. { replaceable: true }
  97. );
  98. this.socket.on(
  99. "event:playlist.deleted",
  100. res => this.removePlaylist(res.data.playlistId),
  101. { replaceable: true }
  102. );
  103. this.socket.on(
  104. "event:playlist.displayName.updated",
  105. res => {
  106. this.playlists.forEach((playlist, index) => {
  107. if (playlist._id === res.data.playlistId) {
  108. this.playlists[index].displayName =
  109. res.data.displayName;
  110. }
  111. });
  112. },
  113. { replaceable: true }
  114. );
  115. },
  116. methods: {
  117. init() {
  118. if (!this.fetchedPlaylists)
  119. this.socket.dispatch(
  120. "playlists.indexMyPlaylists",
  121. true,
  122. res => {
  123. if (res.status === "success")
  124. if (!this.fetchedPlaylists)
  125. this.setPlaylists(res.data.playlists);
  126. }
  127. );
  128. },
  129. toggleSongInPlaylist(playlistIndex) {
  130. const playlist = this.playlists[playlistIndex];
  131. if (!this.hasSong(playlist)) {
  132. this.socket.dispatch(
  133. "playlists.addSongToPlaylist",
  134. false,
  135. this.song.youtubeId,
  136. playlist._id,
  137. res => new Toast(res.message)
  138. );
  139. } else {
  140. this.socket.dispatch(
  141. "playlists.removeSongFromPlaylist",
  142. this.song.youtubeId,
  143. playlist._id,
  144. res => new Toast(res.message)
  145. );
  146. }
  147. },
  148. hasSong(playlist) {
  149. return (
  150. playlist.songs
  151. .map(song => song.youtubeId)
  152. .indexOf(this.song.youtubeId) !== -1
  153. );
  154. },
  155. createPlaylist() {
  156. this.$refs.dropdown.tippy.setProps({
  157. zIndex: 0,
  158. hideOnClick: false
  159. });
  160. window.addToPlaylistDropdown = this.$refs.dropdown;
  161. this.openModal("createPlaylist");
  162. },
  163. ...mapActions("user/playlists", [
  164. "setPlaylists",
  165. "addPlaylist",
  166. "removePlaylist"
  167. ]),
  168. ...mapActions("modalVisibility", ["openModal"])
  169. }
  170. };
  171. </script>
  172. <style lang="scss" scoped>
  173. .no-playlists {
  174. text-align: center;
  175. margin-top: 10px;
  176. }
  177. #create-playlist .material-icons {
  178. color: var(--white);
  179. }
  180. </style>