ImportPlaylist.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <modal title="Import Playlist">
  3. <template #body>
  4. <div class="vertical-padding">
  5. <p class="section-description">
  6. Import a playlist by using a link from YouTube
  7. </p>
  8. <div class="control is-grouped">
  9. <p class="control is-expanded">
  10. <input
  11. class="input"
  12. type="text"
  13. placeholder="YouTube Playlist URL"
  14. v-model="youtubeSearch.playlist.query"
  15. @keyup.enter="importPlaylist()"
  16. />
  17. </p>
  18. <p id="playlist-import-type" class="control select">
  19. <select
  20. v-model="
  21. youtubeSearch.playlist.isImportingOnlyMusic
  22. "
  23. >
  24. <option :value="false">Import all</option>
  25. <option :value="true">Import only music</option>
  26. </select>
  27. </p>
  28. <p class="control">
  29. <button
  30. class="button is-info"
  31. @click.prevent="importPlaylist()"
  32. >
  33. <i class="material-icons icon-with-button"
  34. >publish</i
  35. >Import
  36. </button>
  37. </p>
  38. </div>
  39. </div>
  40. </template>
  41. <template #footer>
  42. <p class="is-expanded checkbox-control">
  43. <label class="switch">
  44. <input
  45. type="checkbox"
  46. id="edit-imported-songs"
  47. v-model="localEditSongs"
  48. />
  49. <span class="slider round"></span>
  50. </label>
  51. <label for="edit-imported-songs">
  52. <p>Edit Songs</p>
  53. </label>
  54. </p>
  55. </template>
  56. </modal>
  57. </template>
  58. <script>
  59. import { mapActions, mapState, mapGetters } from "vuex";
  60. import Toast from "toasters";
  61. import SearchYoutube from "@/mixins/SearchYoutube.vue";
  62. export default {
  63. mixins: [SearchYoutube],
  64. props: {
  65. modalUuid: { type: String, default: "" }
  66. },
  67. computed: {
  68. localEditSongs: {
  69. get() {
  70. return this.$store.state.modals.importPlaylist[this.modalUuid]
  71. .editImportedSongs;
  72. },
  73. set(editImportedSongs) {
  74. this.$store.commit(
  75. `modals/importPlaylist/${this.modalUuid}/updateEditImportedSongs`,
  76. editImportedSongs
  77. );
  78. }
  79. },
  80. ...mapState({
  81. loggedIn: state => state.user.auth.loggedIn
  82. }),
  83. ...mapGetters({
  84. socket: "websockets/getSocket"
  85. })
  86. },
  87. beforeUnmount() {
  88. // Delete the VueX module that was created for this modal, after all other cleanup tasks are performed
  89. this.$store.unregisterModule([
  90. "modals",
  91. "importPlaylist",
  92. this.modalUuid
  93. ]);
  94. },
  95. methods: {
  96. importPlaylist() {
  97. let isImportingPlaylist = true;
  98. // import query is blank
  99. if (!this.youtubeSearch.playlist.query)
  100. return new Toast("Please enter a YouTube playlist URL.");
  101. const regex = /[\\?&]list=([^&#]*)/;
  102. const splitQuery = regex.exec(this.youtubeSearch.playlist.query);
  103. if (!splitQuery) {
  104. return new Toast({
  105. content: "Please enter a valid YouTube playlist URL.",
  106. timeout: 4000
  107. });
  108. }
  109. // don't give starting import message instantly in case of instant error
  110. setTimeout(() => {
  111. if (isImportingPlaylist) {
  112. new Toast(
  113. "Starting to import your playlist. This can take some time to do."
  114. );
  115. }
  116. }, 750);
  117. return this.socket.dispatch(
  118. "songs.requestSet",
  119. this.youtubeSearch.playlist.query,
  120. this.youtubeSearch.playlist.isImportingOnlyMusic,
  121. true,
  122. res => {
  123. isImportingPlaylist = false;
  124. if (
  125. this.localEditSongs &&
  126. res.status === "success" &&
  127. res.songs &&
  128. res.songs.length > 0
  129. ) {
  130. this.editSongs(
  131. res.songs.map(song => ({
  132. ...song,
  133. songId: song._id
  134. }))
  135. );
  136. this.openModal("editSongs");
  137. }
  138. this.closeCurrentModal();
  139. return new Toast({
  140. content: res.message,
  141. timeout: 20000
  142. });
  143. }
  144. );
  145. },
  146. ...mapActions("modals/editSongs", ["editSongs"]),
  147. ...mapActions("modalVisibility", ["openModal", "closeCurrentModal"])
  148. }
  149. };
  150. </script>