ImportPlaylists.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <div class="youtube-tab section">
  3. <label class="label"> Search for a playlist from YouTube </label>
  4. <div class="control is-grouped input-with-button">
  5. <p class="control is-expanded">
  6. <input
  7. class="input"
  8. type="text"
  9. placeholder="Enter YouTube Playlist URL here..."
  10. v-model="youtubeSearch.playlist.query"
  11. @keyup.enter="importPlaylist()"
  12. />
  13. </p>
  14. <p class="control has-addons">
  15. <span class="select" id="playlist-import-type">
  16. <select
  17. v-model="youtubeSearch.playlist.isImportingOnlyMusic"
  18. >
  19. <option :value="false">Import all</option>
  20. <option :value="true">Import only music</option>
  21. </select>
  22. </span>
  23. <button
  24. class="button is-info"
  25. @click.prevent="importPlaylist()"
  26. >
  27. <i class="material-icons icon-with-button">publish</i>Import
  28. </button>
  29. </p>
  30. </div>
  31. </div>
  32. </template>
  33. <script>
  34. import { mapGetters } from "vuex";
  35. import Toast from "toasters";
  36. import { mapModalState } from "@/vuex_helpers";
  37. import SearchYoutube from "@/mixins/SearchYoutube.vue";
  38. export default {
  39. mixins: [SearchYoutube],
  40. props: {
  41. modalUuid: { type: String, default: "" }
  42. },
  43. data() {
  44. return {};
  45. },
  46. computed: {
  47. ...mapModalState("modals/editPlaylist/MODAL_UUID", {
  48. playlist: state => state.playlist
  49. }),
  50. ...mapGetters({
  51. socket: "websockets/getSocket"
  52. })
  53. },
  54. methods: {
  55. importPlaylist() {
  56. let isImportingPlaylist = true;
  57. // import query is blank
  58. if (!this.youtubeSearch.playlist.query)
  59. return new Toast("Please enter a YouTube playlist URL.");
  60. const regex = /[\\?&]list=([^&#]*)/;
  61. const splitQuery = regex.exec(this.youtubeSearch.playlist.query);
  62. if (!splitQuery) {
  63. return new Toast({
  64. content: "Please enter a valid YouTube playlist URL.",
  65. timeout: 4000
  66. });
  67. }
  68. // don't give starting import message instantly in case of instant error
  69. setTimeout(() => {
  70. if (isImportingPlaylist) {
  71. new Toast(
  72. "Starting to import your playlist. This can take some time to do."
  73. );
  74. }
  75. }, 750);
  76. return this.socket.dispatch(
  77. "playlists.addSetToPlaylist",
  78. this.youtubeSearch.playlist.query,
  79. this.playlist._id,
  80. this.youtubeSearch.playlist.isImportingOnlyMusic,
  81. res => {
  82. new Toast({ content: res.message, timeout: 20000 });
  83. if (res.status === "success") {
  84. isImportingPlaylist = false;
  85. const {
  86. songsInPlaylistTotal,
  87. videosInPlaylistTotal,
  88. alreadyInLikedPlaylist,
  89. alreadyInDislikedPlaylist
  90. } = res.data.stats;
  91. if (this.youtubeSearch.playlist.isImportingOnlyMusic) {
  92. new Toast({
  93. content: `${songsInPlaylistTotal} of the ${videosInPlaylistTotal} videos in the playlist were songs.`,
  94. timeout: 20000
  95. });
  96. }
  97. if (
  98. alreadyInLikedPlaylist > 0 ||
  99. alreadyInDislikedPlaylist > 0
  100. ) {
  101. let message = "";
  102. if (alreadyInLikedPlaylist > 0) {
  103. message = `${alreadyInLikedPlaylist} songs were already in your Liked Songs playlist. A song cannot be in both the Liked Songs playlist and the Disliked Songs playlist at the same time.`;
  104. } else {
  105. message = `${alreadyInDislikedPlaylist} songs were already in your Disliked Songs playlist. A song cannot be in both the Liked Songs playlist and the Disliked Songs playlist at the same time.`;
  106. }
  107. new Toast({
  108. content: message,
  109. timeout: 20000
  110. });
  111. }
  112. }
  113. }
  114. );
  115. }
  116. }
  117. };
  118. </script>
  119. <style lang="less" scoped>
  120. #playlist-import-type select {
  121. border-radius: 0;
  122. }
  123. @media screen and (max-width: 1300px) {
  124. .youtube-tab #song-query-results,
  125. .section {
  126. max-width: 100% !important;
  127. }
  128. }
  129. </style>