RequestSong.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <template>
  2. <modal title="Request Song">
  3. <template #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="youtubeSearch.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="youtubeSearch.songs.results.length > 0"
  37. >
  38. <search-query-item
  39. v-for="(result, index) in youtubeSearch.songs.results"
  40. :key="result.id"
  41. :result="result"
  42. >
  43. <template #actions>
  44. <transition
  45. name="search-query-actions"
  46. mode="out-in"
  47. >
  48. <a
  49. class="button is-success"
  50. v-if="result.isRequested"
  51. href="#"
  52. key="added-to-playlist"
  53. >
  54. <i class="material-icons icon-with-button"
  55. >done</i
  56. >
  57. Requested song
  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. Request song
  72. </a>
  73. </transition>
  74. </template>
  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="youtubeSearch.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. youtubeSearch.playlist
  107. .isImportingOnlyMusic
  108. "
  109. >
  110. <option :value="false">Import all</option>
  111. <option :value="true">
  112. Import only music
  113. </option>
  114. </select>
  115. </span>
  116. <a
  117. class="button is-info"
  118. @click.prevent="importPlaylist()"
  119. href="#"
  120. ><i class="material-icons icon-with-button"
  121. >publish</i
  122. >Import</a
  123. >
  124. </p>
  125. </div>
  126. </div>
  127. </div>
  128. </template>
  129. </modal>
  130. </template>
  131. <script>
  132. import { mapState, mapGetters } from "vuex";
  133. import Toast from "toasters";
  134. import SearchYoutube from "@/mixins/SearchYoutube.vue";
  135. import SearchQueryItem from "../SearchQueryItem.vue";
  136. import Modal from "../Modal.vue";
  137. export default {
  138. components: { Modal, SearchQueryItem },
  139. mixins: [SearchYoutube],
  140. computed: {
  141. ...mapState({
  142. loggedIn: state => state.user.auth.loggedIn,
  143. station: state => state.station.station
  144. }),
  145. ...mapGetters({
  146. socket: "websockets/getSocket"
  147. })
  148. },
  149. methods: {
  150. addSongToQueue(youtubeId, index) {
  151. if (this.station.type === "community") {
  152. this.socket.dispatch(
  153. "stations.addToQueue",
  154. this.station._id,
  155. youtubeId,
  156. res => {
  157. if (res.status !== "success")
  158. new Toast(`Error: ${res.message}`);
  159. else {
  160. this.youtubeSearch.songs.results[
  161. index
  162. ].isRequested = true;
  163. new Toast(res.message);
  164. }
  165. }
  166. );
  167. } else {
  168. this.socket.dispatch("songs.request", youtubeId, false, res => {
  169. if (res.status !== "success")
  170. new Toast(`Error: ${res.message}`);
  171. else {
  172. this.youtubeSearch.songs.results[
  173. index
  174. ].isRequested = true;
  175. new Toast(res.message);
  176. }
  177. });
  178. }
  179. },
  180. importPlaylist() {
  181. let isImportingPlaylist = true;
  182. // import query is blank
  183. if (!this.youtubeSearch.playlist.query)
  184. return new Toast("Please enter a YouTube playlist URL.");
  185. const regex = new RegExp(`[\\?&]list=([^&#]*)`);
  186. const splitQuery = regex.exec(this.youtubeSearch.playlist.query);
  187. if (!splitQuery) {
  188. return new Toast({
  189. content: "Please enter a valid YouTube playlist URL.",
  190. timeout: 4000
  191. });
  192. }
  193. // don't give starting import message instantly in case of instant error
  194. setTimeout(() => {
  195. if (isImportingPlaylist) {
  196. new Toast(
  197. "Starting to import your playlist. This can take some time to do."
  198. );
  199. }
  200. }, 750);
  201. return this.socket.dispatch(
  202. "songs.requestSet",
  203. this.youtubeSearch.playlist.query,
  204. this.youtubeSearch.playlist.isImportingOnlyMusic,
  205. false,
  206. res => {
  207. isImportingPlaylist = false;
  208. return new Toast({ content: res.message, timeout: 20000 });
  209. }
  210. );
  211. }
  212. }
  213. };
  214. </script>
  215. <style lang="scss" scoped>
  216. .night-mode {
  217. div {
  218. color: var(--dark-grey);
  219. }
  220. }
  221. .song-actions {
  222. .button {
  223. height: 36px;
  224. width: 140px;
  225. }
  226. }
  227. .song-thumbnail div {
  228. width: 96px;
  229. height: 54px;
  230. background-position: center;
  231. background-repeat: no-repeat;
  232. }
  233. .table {
  234. margin-bottom: 0;
  235. margin-top: 20px;
  236. }
  237. .vertical-padding {
  238. padding: 20px;
  239. }
  240. #song-query-results {
  241. padding: 10px;
  242. max-height: 500px;
  243. overflow: auto;
  244. border: 1px solid var(--light-grey-3);
  245. border-radius: 3px;
  246. .search-query-item:not(:last-of-type) {
  247. margin-bottom: 10px;
  248. }
  249. .load-more-button {
  250. width: 100%;
  251. margin-top: 10px;
  252. }
  253. }
  254. </style>