ImportPlaylist.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. computed: {
  65. localEditSongs: {
  66. get() {
  67. return this.$store.state.modals.importPlaylist
  68. .editImportedSongs;
  69. },
  70. set(editImportedSongs) {
  71. this.$store.commit(
  72. "modals/importPlaylist/updateEditImportedSongs",
  73. editImportedSongs
  74. );
  75. }
  76. },
  77. ...mapState({
  78. loggedIn: state => state.user.auth.loggedIn,
  79. station: state => state.station.station
  80. }),
  81. ...mapGetters({
  82. socket: "websockets/getSocket"
  83. })
  84. },
  85. methods: {
  86. importPlaylist() {
  87. let isImportingPlaylist = true;
  88. // import query is blank
  89. if (!this.youtubeSearch.playlist.query)
  90. return new Toast("Please enter a YouTube playlist URL.");
  91. const regex = /[\\?&]list=([^&#]*)/;
  92. const splitQuery = regex.exec(this.youtubeSearch.playlist.query);
  93. if (!splitQuery) {
  94. return new Toast({
  95. content: "Please enter a valid YouTube playlist URL.",
  96. timeout: 4000
  97. });
  98. }
  99. // don't give starting import message instantly in case of instant error
  100. setTimeout(() => {
  101. if (isImportingPlaylist) {
  102. new Toast(
  103. "Starting to import your playlist. This can take some time to do."
  104. );
  105. }
  106. }, 750);
  107. return this.socket.dispatch(
  108. "songs.requestSet",
  109. this.youtubeSearch.playlist.query,
  110. this.youtubeSearch.playlist.isImportingOnlyMusic,
  111. true,
  112. res => {
  113. isImportingPlaylist = false;
  114. if (
  115. this.localEditSongs &&
  116. res.status === "success" &&
  117. res.songs &&
  118. res.songs.length > 0
  119. ) {
  120. this.editSongs(
  121. res.songs.map(song => ({
  122. ...song,
  123. songId: song._id
  124. }))
  125. );
  126. this.openModal("editSongs");
  127. }
  128. this.closeModal("importPlaylist");
  129. return new Toast({
  130. content: res.message,
  131. timeout: 20000
  132. });
  133. }
  134. );
  135. },
  136. ...mapActions("modals/editSongs", ["editSongs"]),
  137. ...mapActions("modalVisibility", ["openModal", "closeModal"])
  138. }
  139. };
  140. </script>