Request.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <template>
  2. <div class="station-playlists">
  3. <div class="tabs-container">
  4. <div class="tab-selection">
  5. <button
  6. class="button is-default"
  7. ref="songs-tab"
  8. :class="{ selected: tab === 'songs' }"
  9. @click="showTab('songs')"
  10. >
  11. Songs
  12. </button>
  13. <button
  14. v-if="!disableAutoRequest"
  15. class="button is-default"
  16. ref="autorequest-tab"
  17. :class="{ selected: tab === 'autorequest' }"
  18. @click="showTab('autorequest')"
  19. >
  20. Autorequest
  21. </button>
  22. <button
  23. v-else
  24. class="button is-default disabled"
  25. content="Only available on station pages"
  26. v-tippy
  27. >
  28. Autorequest
  29. </button>
  30. </div>
  31. <div class="tab" v-show="tab === 'songs'">
  32. <div class="musare-songs">
  33. <label class="label"> Search for a song on Musare </label>
  34. <div class="control is-grouped input-with-button">
  35. <p class="control is-expanded">
  36. <input
  37. class="input"
  38. type="text"
  39. placeholder="Enter your song query here..."
  40. v-model="musareSearch.query"
  41. @keyup.enter="searchForMusareSongs(1)"
  42. />
  43. </p>
  44. <p class="control">
  45. <a
  46. class="button is-info"
  47. @click="searchForMusareSongs(1)"
  48. ><i class="material-icons icon-with-button"
  49. >search</i
  50. >Search</a
  51. >
  52. </p>
  53. </div>
  54. <div v-if="musareSearch.results.length > 0">
  55. <song-item
  56. v-for="song in musareSearch.results"
  57. :key="song._id"
  58. :song="song"
  59. >
  60. <template #actions>
  61. <transition
  62. name="musare-search-query-actions"
  63. mode="out-in"
  64. >
  65. <i
  66. v-if="
  67. songsInQueue.indexOf(
  68. song.youtubeId
  69. ) !== -1
  70. "
  71. class="material-icons added-to-playlist-icon"
  72. content="Song is already in queue"
  73. v-tippy
  74. >done</i
  75. >
  76. <i
  77. v-else
  78. class="material-icons add-to-queue-icon"
  79. @click="addSongToQueue(song.youtubeId)"
  80. content="Add Song to Queue"
  81. v-tippy
  82. >queue</i
  83. >
  84. </transition>
  85. </template>
  86. </song-item>
  87. <button
  88. v-if="musareResultsLeftCount > 0"
  89. class="button is-primary load-more-button"
  90. @click="searchForMusareSongs(musareSearch.page + 1)"
  91. >
  92. Load {{ nextPageMusareResultsCount }} more results
  93. </button>
  94. </div>
  95. </div>
  96. <div class="youtube-search">
  97. <label class="label"> Search for a song on YouTube </label>
  98. <div class="control is-grouped input-with-button">
  99. <p class="control is-expanded">
  100. <input
  101. class="input"
  102. type="text"
  103. placeholder="Enter your YouTube query here..."
  104. v-model="youtubeSearch.songs.query"
  105. autofocus
  106. @keyup.enter="searchForSongs()"
  107. />
  108. </p>
  109. <p class="control">
  110. <a
  111. class="button is-info"
  112. @click.prevent="searchForSongs()"
  113. ><i class="material-icons icon-with-button"
  114. >search</i
  115. >Search</a
  116. >
  117. </p>
  118. </div>
  119. <div
  120. v-if="youtubeSearch.songs.results.length > 0"
  121. id="song-query-results"
  122. >
  123. <search-query-item
  124. v-for="(result, index) in youtubeSearch.songs
  125. .results"
  126. :key="result.id"
  127. :result="result"
  128. >
  129. <template #actions>
  130. <transition
  131. name="youtube-search-query-actions"
  132. mode="out-in"
  133. >
  134. <i
  135. v-if="
  136. songsInQueue.indexOf(result.id) !==
  137. -1
  138. "
  139. class="material-icons added-to-playlist-icon"
  140. content="Song is already in queue"
  141. v-tippy
  142. >done</i
  143. >
  144. <i
  145. v-else
  146. class="material-icons add-to-queue-icon"
  147. @click="
  148. addSongToQueue(result.id, index)
  149. "
  150. content="Add Song to Queue"
  151. v-tippy
  152. >queue</i
  153. >
  154. </transition>
  155. </template>
  156. </search-query-item>
  157. <a
  158. class="button is-primary load-more-button"
  159. @click.prevent="loadMoreSongs()"
  160. >
  161. Load more...
  162. </a>
  163. </div>
  164. </div>
  165. </div>
  166. <playlist-tab-base
  167. v-if="!disableAutoRequest"
  168. class="tab"
  169. v-show="tab === 'autorequest'"
  170. :type="'autorequest'"
  171. :sector="sector"
  172. @selected="autoRequestSong()"
  173. />
  174. </div>
  175. </div>
  176. </template>
  177. <script>
  178. import { mapActions, mapState, mapGetters } from "vuex";
  179. import Toast from "toasters";
  180. import SongItem from "@/components/SongItem.vue";
  181. import SearchQueryItem from "@/components/SearchQueryItem.vue";
  182. import PlaylistTabBase from "@/components/PlaylistTabBase.vue";
  183. import SearchYoutube from "@/mixins/SearchYoutube.vue";
  184. import SearchMusare from "@/mixins/SearchMusare.vue";
  185. export default {
  186. components: {
  187. SongItem,
  188. SearchQueryItem,
  189. PlaylistTabBase
  190. },
  191. mixins: [SearchYoutube, SearchMusare],
  192. props: {
  193. sector: { type: String, default: "station" },
  194. disableAutoRequest: { type: Boolean, default: false }
  195. },
  196. data() {
  197. return {
  198. tab: "songs"
  199. };
  200. },
  201. computed: {
  202. station: {
  203. get() {
  204. if (this.sector === "manageStation")
  205. return this.$store.state.modals.manageStation.station;
  206. return this.$store.state.station.station;
  207. },
  208. set(station) {
  209. if (this.sector === "manageStation")
  210. this.$store.commit(
  211. "modals/manageStation/updateStation",
  212. station
  213. );
  214. else this.$store.commit("station/updateStation", station);
  215. }
  216. },
  217. blacklist: {
  218. get() {
  219. if (this.sector === "manageStation")
  220. return this.$store.state.modals.manageStation.blacklist;
  221. return this.$store.state.station.blacklist;
  222. },
  223. set(blacklist) {
  224. if (this.sector === "manageStation")
  225. this.$store.commit(
  226. "modals/manageStation/setBlacklist",
  227. blacklist
  228. );
  229. else this.$store.commit("station/setBlacklist", blacklist);
  230. }
  231. },
  232. songsList: {
  233. get() {
  234. if (this.sector === "manageStation")
  235. return this.$store.state.modals.manageStation.songsList;
  236. return this.$store.state.station.songsList;
  237. },
  238. set(songsList) {
  239. if (this.sector === "manageStation")
  240. this.$store.commit(
  241. "modals/manageStation/updateSongsList",
  242. songsList
  243. );
  244. else this.$store.commit("station/updateSongsList", songsList);
  245. }
  246. },
  247. musareResultsLeftCount() {
  248. return this.musareSearch.count - this.musareSearch.results.length;
  249. },
  250. nextPageMusareResultsCount() {
  251. return Math.min(
  252. this.musareSearch.pageSize,
  253. this.musareResultsLeftCount
  254. );
  255. },
  256. songsInQueue() {
  257. if (this.station.currentSong)
  258. return this.songsList
  259. .map(song => song.youtubeId)
  260. .concat(this.station.currentSong.youtubeId);
  261. return this.songsList.map(song => song.youtubeId);
  262. },
  263. currentUserQueueSongs() {
  264. return this.songsList.filter(
  265. queueSong => queueSong.requestedBy === this.userId
  266. ).length;
  267. },
  268. ...mapState("user", {
  269. loggedIn: state => state.auth.loggedIn,
  270. role: state => state.auth.role,
  271. userId: state => state.auth.userId
  272. }),
  273. ...mapState("station", {
  274. autoRequest: state => state.autoRequest,
  275. autoRequestLock: state => state.autoRequestLock
  276. }),
  277. ...mapGetters({
  278. socket: "websockets/getSocket"
  279. })
  280. },
  281. mounted() {
  282. this.showTab("songs");
  283. this.socket.on("event:station.queue.updated", () =>
  284. this.autoRequestSong()
  285. );
  286. },
  287. methods: {
  288. showTab(tab) {
  289. this.$refs[`${tab}-tab`].scrollIntoView({ block: "nearest" });
  290. this.tab = tab;
  291. },
  292. autoRequestSong() {
  293. if (
  294. !this.autoRequestLock &&
  295. this.songsList.length < 50 &&
  296. this.currentUserQueueSongs <
  297. this.station.requests.limit * 0.5 &&
  298. this.autoRequest.length > 0
  299. ) {
  300. const selectedPlaylist =
  301. this.autoRequest[
  302. Math.floor(Math.random() * this.autoRequest.length)
  303. ];
  304. if (selectedPlaylist._id && selectedPlaylist.songs.length > 0) {
  305. const selectedSong =
  306. selectedPlaylist.songs[
  307. Math.floor(
  308. Math.random() * selectedPlaylist.songs.length
  309. )
  310. ];
  311. if (selectedSong.youtubeId) {
  312. this.updateAutoRequestLock(true);
  313. this.socket.dispatch(
  314. "stations.addToQueue",
  315. this.station._id,
  316. selectedSong.youtubeId,
  317. data => {
  318. this.updateAutoRequestLock(false);
  319. if (data.status !== "success")
  320. this.autoRequestSong();
  321. }
  322. );
  323. }
  324. }
  325. }
  326. },
  327. addSongToQueue(youtubeId, index) {
  328. this.socket.dispatch(
  329. "stations.addToQueue",
  330. this.station._id,
  331. youtubeId,
  332. res => {
  333. if (res.status !== "success")
  334. new Toast(`Error: ${res.message}`);
  335. else {
  336. if (index)
  337. this.youtubeSearch.songs.results[
  338. index
  339. ].isAddedToQueue = true;
  340. new Toast(res.message);
  341. }
  342. }
  343. );
  344. },
  345. ...mapActions("station", ["updateAutoRequest", "updateAutoRequestLock"])
  346. }
  347. };
  348. </script>
  349. <style lang="less" scoped>
  350. .night-mode {
  351. .tabs-container .tab-selection .button {
  352. background: var(--dark-grey) !important;
  353. color: var(--white) !important;
  354. }
  355. }
  356. :deep(#create-new-playlist-button) {
  357. width: 100%;
  358. }
  359. .station-playlists {
  360. .tabs-container {
  361. .tab-selection {
  362. display: flex;
  363. overflow-x: auto;
  364. .button {
  365. border-radius: 0;
  366. border: 0;
  367. text-transform: uppercase;
  368. font-size: 14px;
  369. color: var(--dark-grey-3);
  370. background-color: var(--light-grey-2);
  371. flex-grow: 1;
  372. height: 32px;
  373. &:not(:first-of-type) {
  374. margin-left: 5px;
  375. }
  376. }
  377. .selected {
  378. background-color: var(--primary-color) !important;
  379. color: var(--white) !important;
  380. font-weight: 600;
  381. }
  382. }
  383. .tab {
  384. padding: 10px 0;
  385. border-radius: 0;
  386. .item.item-draggable:not(:last-of-type) {
  387. margin-bottom: 10px;
  388. }
  389. .load-more-button {
  390. width: 100%;
  391. margin-top: 10px;
  392. }
  393. }
  394. }
  395. }
  396. .youtube-search {
  397. margin-top: 10px;
  398. .search-query-item:not(:last-of-type) {
  399. margin-bottom: 10px;
  400. }
  401. }
  402. </style>