RequestSong.vue 6.0 KB

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