Youtube.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <template>
  2. <div class="youtube-tab section">
  3. <h4 class="section-title">Import from YouTube</h4>
  4. <p class="section-description">
  5. Import a playlist or song by searching or using a link from YouTube.
  6. </p>
  7. <hr class="section-horizontal-rule" />
  8. <label class="label">
  9. Search for a playlist from YouTube
  10. </label>
  11. <div class="control is-grouped input-with-button">
  12. <p class="control is-expanded">
  13. <input
  14. class="input"
  15. type="text"
  16. placeholder="Enter YouTube Playlist URL here..."
  17. v-model="search.playlist.query"
  18. @keyup.enter="importPlaylist()"
  19. />
  20. </p>
  21. <p class="control has-addons">
  22. <span class="select" id="playlist-import-type">
  23. <select v-model="search.playlist.isImportingOnlyMusic">
  24. <option :value="false">Import all</option>
  25. <option :value="true">
  26. Import only music
  27. </option>
  28. </select>
  29. </span>
  30. <a
  31. class="button is-info"
  32. @click.prevent="importPlaylist()"
  33. href="#"
  34. ><i class="material-icons icon-with-button">publish</i
  35. >Import</a
  36. >
  37. </p>
  38. </div>
  39. <label class="label">
  40. Search for a song from YouTube
  41. </label>
  42. <div class="control is-grouped input-with-button">
  43. <p class="control is-expanded">
  44. <input
  45. class="input"
  46. type="text"
  47. placeholder="Enter your YouTube query here..."
  48. v-model="search.songs.query"
  49. autofocus
  50. @keyup.enter="searchForSongs()"
  51. />
  52. </p>
  53. <p class="control">
  54. <a
  55. class="button is-info"
  56. @click.prevent="searchForSongs()"
  57. href="#"
  58. ><i class="material-icons icon-with-button">search</i
  59. >Search</a
  60. >
  61. </p>
  62. </div>
  63. <div v-if="search.songs.results.length > 0" id="song-query-results">
  64. <search-query-item
  65. v-for="(result, index) in search.songs.results"
  66. :key="result.id"
  67. :result="result"
  68. >
  69. <template #actions>
  70. <transition name="search-query-actions" mode="out-in">
  71. <a
  72. class="button is-success"
  73. v-if="result.isAddedToQueue"
  74. href="#"
  75. key="added-to-playlist"
  76. >
  77. <i class="material-icons icon-with-button">done</i>
  78. Added to playlist
  79. </a>
  80. <a
  81. class="button is-dark"
  82. v-else
  83. @click.prevent="addSongToPlaylist(result.id, index)"
  84. href="#"
  85. key="add-to-playlist"
  86. >
  87. <i class="material-icons icon-with-button">add</i>
  88. Add to playlist
  89. </a>
  90. </transition>
  91. </template>
  92. </search-query-item>
  93. <a
  94. class="button is-primary load-more-button"
  95. @click.prevent="loadMoreSongs()"
  96. href="#"
  97. >
  98. Load more...
  99. </a>
  100. </div>
  101. </div>
  102. </template>
  103. <script>
  104. import { mapState, mapGetters /* , mapActions */ } from "vuex";
  105. import Toast from "toasters";
  106. import SearchYoutube from "@/mixins/SearchYoutube.vue";
  107. import SearchQueryItem from "../../../SearchQueryItem.vue";
  108. export default {
  109. components: { SearchQueryItem },
  110. mixins: [SearchYoutube],
  111. data() {
  112. return {};
  113. },
  114. computed: {
  115. ...mapState("modals/editPlaylist", {
  116. playlist: state => state.playlist
  117. }),
  118. ...mapGetters({
  119. socket: "websockets/getSocket"
  120. })
  121. },
  122. watch: {
  123. "search.songs.results": function checkIfSongInPlaylist(songs) {
  124. songs.forEach((searchItem, index) =>
  125. this.playlist.songs.find(song => {
  126. if (song.youtubeId === searchItem.id)
  127. this.search.songs.results[index].isAddedToQueue = true;
  128. return song.youtubeId === searchItem.id;
  129. })
  130. );
  131. },
  132. "playlist.songs": function checkIfSongInPlaylist() {
  133. this.search.songs.results.forEach((searchItem, index) =>
  134. this.playlist.songs.find(song => {
  135. this.search.songs.results[index].isAddedToQueue = false;
  136. if (song.youtubeId === searchItem.id)
  137. this.search.songs.results[index].isAddedToQueue = true;
  138. return song.youtubeId === searchItem.id;
  139. })
  140. );
  141. }
  142. },
  143. mounted() {},
  144. methods: {
  145. importPlaylist() {
  146. let isImportingPlaylist = true;
  147. // import query is blank
  148. if (!this.search.playlist.query)
  149. return new Toast("Please enter a YouTube playlist URL.");
  150. const regex = new RegExp(`[\\?&]list=([^&#]*)`);
  151. const splitQuery = regex.exec(this.search.playlist.query);
  152. if (!splitQuery) {
  153. return new Toast({
  154. content: "Please enter a valid YouTube playlist URL.",
  155. timeout: 4000
  156. });
  157. }
  158. // don't give starting import message instantly in case of instant error
  159. setTimeout(() => {
  160. if (isImportingPlaylist) {
  161. new Toast(
  162. "Starting to import your playlist. This can take some time to do."
  163. );
  164. }
  165. }, 750);
  166. return this.socket.dispatch(
  167. "playlists.addSetToPlaylist",
  168. this.search.playlist.query,
  169. this.playlist._id,
  170. this.search.playlist.isImportingOnlyMusic,
  171. res => {
  172. new Toast({ content: res.message, timeout: 20000 });
  173. if (res.status === "success") {
  174. isImportingPlaylist = false;
  175. if (this.search.playlist.isImportingOnlyMusic) {
  176. new Toast({
  177. content: `${res.data.stats.songsInPlaylistTotal} of the ${res.data.stats.videosInPlaylistTotal} videos in the playlist were songs.`,
  178. timeout: 20000
  179. });
  180. }
  181. }
  182. }
  183. );
  184. }
  185. // ...mapActions("modals/editSong", ["selectDiscogsInfo"])
  186. }
  187. };
  188. </script>
  189. <style lang="scss" scoped>
  190. .youtube-tab {
  191. #import-from-youtube-section {
  192. #playlist-import-type select {
  193. border-radius: 0;
  194. }
  195. #song-query-results {
  196. padding: 10px;
  197. margin-top: 10px;
  198. border: 1px solid var(--light-grey-3);
  199. border-radius: 3px;
  200. max-width: 565px;
  201. .search-query-item:not(:last-of-type) {
  202. margin-bottom: 10px;
  203. }
  204. }
  205. .load-more-button {
  206. width: 100%;
  207. margin-top: 10px;
  208. }
  209. }
  210. }
  211. @media screen and (max-width: 1300px) {
  212. .youtube-tab #song-query-results,
  213. .section {
  214. max-width: 100% !important;
  215. }
  216. }
  217. </style>