RequestSong.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <modal title="Request Song">
  3. <div slot="body">
  4. <div class="vertical-padding">
  5. <!-- Choosing a song from youtube -->
  6. <h4 class="section-title">Choose a song</h4>
  7. <p class="section-description">
  8. Choose a song by searching or using a link from YouTube.
  9. </p>
  10. <br />
  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 your YouTube query here..."
  17. v-model="search.songs.query"
  18. autofocus
  19. @keyup.enter="searchForSongs()"
  20. />
  21. </p>
  22. <p class="control">
  23. <a
  24. class="button is-info"
  25. @click.prevent="searchForSongs()"
  26. href="#"
  27. ><i class="material-icons icon-with-button"
  28. >search</i
  29. >Search</a
  30. >
  31. </p>
  32. </div>
  33. <!-- Choosing a song from youtube - query results -->
  34. <div
  35. id="song-query-results"
  36. v-if="search.songs.results.length > 0"
  37. >
  38. <search-query-item
  39. v-for="(result, index) in search.songs.results"
  40. :key="index"
  41. :result="result"
  42. >
  43. <div slot="actions">
  44. <transition
  45. name="search-query-actions"
  46. mode="out-in"
  47. >
  48. <a
  49. class="button is-success"
  50. v-if="result.isAddedToQueue"
  51. href="#"
  52. key="added-to-playlist"
  53. >
  54. <i class="material-icons icon-with-button"
  55. >done</i
  56. >
  57. Added to queue
  58. </a>
  59. <a
  60. class="button is-dark"
  61. v-else
  62. @click.prevent="
  63. addSongToQueue(result.id, index)
  64. "
  65. href="#"
  66. key="add-to-queue"
  67. >
  68. <i class="material-icons icon-with-button"
  69. >add</i
  70. >
  71. Add to queue
  72. </a>
  73. </transition>
  74. </div>
  75. </search-query-item>
  76. <a
  77. class="button is-default load-more-button"
  78. @click.prevent="loadMoreSongs()"
  79. href="#"
  80. >
  81. Load more...
  82. </a>
  83. </div>
  84. <!-- Import a playlist from youtube -->
  85. <div v-if="station.type === 'official'">
  86. <hr class="section-horizontal-rule" />
  87. <h4 class="section-title">Import a playlist</h4>
  88. <p class="section-description">
  89. Import a playlist by using a link from YouTube.
  90. </p>
  91. <br />
  92. <div class="control is-grouped input-with-button">
  93. <p class="control is-expanded">
  94. <input
  95. class="input"
  96. type="text"
  97. placeholder="YouTube Playlist URL"
  98. v-model="search.playlist.query"
  99. @keyup.enter="importPlaylist()"
  100. />
  101. </p>
  102. <p class="control has-addons">
  103. <span class="select" id="playlist-import-type">
  104. <select
  105. v-model="
  106. search.playlist.isImportingOnlyMusic
  107. "
  108. >
  109. <option :value="false">Import all</option>
  110. <option :value="true">
  111. Import only music
  112. </option>
  113. </select>
  114. </span>
  115. <a
  116. class="button is-info"
  117. @click.prevent="importPlaylist()"
  118. href="#"
  119. ><i class="material-icons icon-with-button"
  120. >publish</i
  121. >Import</a
  122. >
  123. </p>
  124. </div>
  125. </div>
  126. </div>
  127. </div>
  128. </modal>
  129. </template>
  130. <script>
  131. import { mapState, mapGetters } from "vuex";
  132. import Toast from "toasters";
  133. import SearchYoutube from "@/mixins/SearchYoutube.vue";
  134. import SearchQueryItem from "../SearchQueryItem.vue";
  135. import Modal from "../Modal.vue";
  136. export default {
  137. components: { Modal, SearchQueryItem },
  138. mixins: [SearchYoutube],
  139. computed: {
  140. ...mapState({
  141. loggedIn: state => state.user.auth.loggedIn,
  142. station: state => state.station.station
  143. }),
  144. ...mapGetters({
  145. socket: "websockets/getSocket"
  146. })
  147. },
  148. methods: {
  149. addSongToQueue(youtubeId, index) {
  150. if (this.station.type === "community") {
  151. this.socket.dispatch(
  152. "stations.addToQueue",
  153. this.station._id,
  154. youtubeId,
  155. res => {
  156. if (res.status !== "success")
  157. new Toast(`Error: ${res.message}`);
  158. else {
  159. this.search.songs.results[
  160. index
  161. ].isAddedToQueue = true;
  162. new Toast(res.message);
  163. }
  164. }
  165. );
  166. } else {
  167. this.socket.dispatch("songs.request", youtubeId, res => {
  168. if (res.status !== "success")
  169. new Toast(`Error: ${res.message}`);
  170. else {
  171. this.search.songs.results[index].isAddedToQueue = true;
  172. new Toast(res.message);
  173. }
  174. });
  175. }
  176. },
  177. importPlaylist() {
  178. let isImportingPlaylist = true;
  179. // import query is blank
  180. if (!this.search.playlist.query)
  181. return new Toast("Please enter a YouTube playlist URL.");
  182. const regex = new RegExp(`[\\?&]list=([^&#]*)`);
  183. const splitQuery = regex.exec(this.search.playlist.query);
  184. if (!splitQuery) {
  185. return new Toast({
  186. content: "Please enter a valid YouTube playlist URL.",
  187. timeout: 4000
  188. });
  189. }
  190. // don't give starting import message instantly in case of instant error
  191. setTimeout(() => {
  192. if (isImportingPlaylist) {
  193. new Toast(
  194. "Starting to import your playlist. This can take some time to do."
  195. );
  196. }
  197. }, 750);
  198. return this.socket.dispatch(
  199. "songs.requestSet",
  200. this.search.playlist.query,
  201. this.search.playlist.isImportingOnlyMusic,
  202. res => {
  203. isImportingPlaylist = false;
  204. return new Toast({ content: res.message, timeout: 20000 });
  205. }
  206. );
  207. }
  208. }
  209. };
  210. </script>
  211. <style lang="scss" scoped>
  212. .night-mode {
  213. div {
  214. color: var(--dark-grey);
  215. }
  216. }
  217. .song-actions {
  218. .button {
  219. height: 36px;
  220. width: 140px;
  221. }
  222. }
  223. .song-thumbnail div {
  224. width: 96px;
  225. height: 54px;
  226. background-position: center;
  227. background-repeat: no-repeat;
  228. }
  229. .table {
  230. margin-bottom: 0;
  231. margin-top: 20px;
  232. }
  233. .vertical-padding {
  234. padding: 20px;
  235. }
  236. #song-query-results {
  237. padding: 10px;
  238. max-height: 500px;
  239. overflow: auto;
  240. border: 1px solid var(--light-grey-3);
  241. border-radius: 3px;
  242. .search-query-item:not(:last-of-type) {
  243. margin-bottom: 10px;
  244. }
  245. .load-more-button {
  246. width: 100%;
  247. margin-top: 10px;
  248. }
  249. }
  250. </style>