RequestSong.vue 6.0 KB

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