Request.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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="sector === 'station'"
  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="sector === 'station'"
  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. },
  195. data() {
  196. return {
  197. tab: "songs"
  198. };
  199. },
  200. computed: {
  201. station: {
  202. get() {
  203. if (this.sector === "manageStation")
  204. return this.$store.state.modals.manageStation.station;
  205. return this.$store.state.station.station;
  206. },
  207. set(station) {
  208. if (this.sector === "manageStation")
  209. this.$store.commit(
  210. "modals/manageStation/updateStation",
  211. station
  212. );
  213. else this.$store.commit("station/updateStation", station);
  214. }
  215. },
  216. blacklist: {
  217. get() {
  218. if (this.sector === "manageStation")
  219. return this.$store.state.modals.manageStation.blacklist;
  220. return this.$store.state.station.blacklist;
  221. },
  222. set(blacklist) {
  223. if (this.sector === "manageStation")
  224. this.$store.commit(
  225. "modals/manageStation/setBlacklist",
  226. blacklist
  227. );
  228. else this.$store.commit("station/setBlacklist", blacklist);
  229. }
  230. },
  231. songsList: {
  232. get() {
  233. if (this.sector === "manageStation")
  234. return this.$store.state.modals.manageStation.songsList;
  235. return this.$store.state.station.songsList;
  236. },
  237. set(songsList) {
  238. if (this.sector === "manageStation")
  239. this.$store.commit(
  240. "modals/manageStation/updateSongsList",
  241. songsList
  242. );
  243. else this.$store.commit("station/updateSongsList", songsList);
  244. }
  245. },
  246. musareResultsLeftCount() {
  247. return this.musareSearch.count - this.musareSearch.results.length;
  248. },
  249. nextPageMusareResultsCount() {
  250. return Math.min(
  251. this.musareSearch.pageSize,
  252. this.musareResultsLeftCount
  253. );
  254. },
  255. songsInQueue() {
  256. if (this.station.currentSong)
  257. return this.songsList
  258. .map(song => song.youtubeId)
  259. .concat(this.station.currentSong.youtubeId);
  260. return this.songsList.map(song => song.youtubeId);
  261. },
  262. currentUserQueueSongs() {
  263. return this.songsList.filter(
  264. queueSong => queueSong.requestedBy === this.userId
  265. ).length;
  266. },
  267. ...mapState("user", {
  268. loggedIn: state => state.auth.loggedIn,
  269. role: state => state.auth.role,
  270. userId: state => state.auth.userId
  271. }),
  272. ...mapState("station", {
  273. autoRequest: state => state.autoRequest,
  274. autoRequestLock: state => state.autoRequestLock
  275. }),
  276. ...mapGetters({
  277. socket: "websockets/getSocket"
  278. })
  279. },
  280. mounted() {
  281. this.showTab("songs");
  282. this.socket.on("event:station.queue.updated", () =>
  283. this.autoRequestSong()
  284. );
  285. },
  286. methods: {
  287. showTab(tab) {
  288. this.$refs[`${tab}-tab`].scrollIntoView({ block: "nearest" });
  289. this.tab = tab;
  290. },
  291. autoRequestSong() {
  292. if (
  293. !this.autoRequestLock &&
  294. this.songsList.length < 50 &&
  295. this.currentUserQueueSongs <
  296. this.station.requests.limit * 0.5 &&
  297. this.autoRequest.length > 0
  298. ) {
  299. const selectedPlaylist =
  300. this.autoRequest[
  301. Math.floor(Math.random() * this.autoRequest.length)
  302. ];
  303. if (selectedPlaylist._id && selectedPlaylist.songs.length > 0) {
  304. const selectedSong =
  305. selectedPlaylist.songs[
  306. Math.floor(
  307. Math.random() * selectedPlaylist.songs.length
  308. )
  309. ];
  310. if (selectedSong.youtubeId) {
  311. this.updateAutoRequestLock(true);
  312. this.socket.dispatch(
  313. "stations.addToQueue",
  314. this.station._id,
  315. selectedSong.youtubeId,
  316. data => {
  317. this.updateAutoRequestLock(false);
  318. if (data.status !== "success")
  319. this.autoRequestSong();
  320. }
  321. );
  322. }
  323. }
  324. }
  325. },
  326. addSongToQueue(youtubeId, index) {
  327. this.socket.dispatch(
  328. "stations.addToQueue",
  329. this.station._id,
  330. youtubeId,
  331. res => {
  332. if (res.status !== "success")
  333. new Toast(`Error: ${res.message}`);
  334. else {
  335. if (index)
  336. this.youtubeSearch.songs.results[
  337. index
  338. ].isAddedToQueue = true;
  339. new Toast(res.message);
  340. }
  341. }
  342. );
  343. },
  344. ...mapActions("station", ["updateAutoRequest", "updateAutoRequestLock"])
  345. }
  346. };
  347. </script>
  348. <style lang="less" scoped>
  349. .night-mode {
  350. .tabs-container .tab-selection .button {
  351. background: var(--dark-grey) !important;
  352. color: var(--white) !important;
  353. }
  354. }
  355. :deep(#create-new-playlist-button) {
  356. width: 100%;
  357. }
  358. .station-playlists {
  359. .tabs-container {
  360. .tab-selection {
  361. display: flex;
  362. overflow-x: auto;
  363. .button {
  364. border-radius: 0;
  365. border: 0;
  366. text-transform: uppercase;
  367. font-size: 14px;
  368. color: var(--dark-grey-3);
  369. background-color: var(--light-grey-2);
  370. flex-grow: 1;
  371. height: 32px;
  372. &:not(:first-of-type) {
  373. margin-left: 5px;
  374. }
  375. }
  376. .selected {
  377. background-color: var(--primary-color) !important;
  378. color: var(--white) !important;
  379. font-weight: 600;
  380. }
  381. }
  382. .tab {
  383. padding: 10px 0;
  384. border-radius: 0;
  385. .item.item-draggable:not(:last-of-type) {
  386. margin-bottom: 10px;
  387. }
  388. .load-more-button {
  389. width: 100%;
  390. margin-top: 10px;
  391. }
  392. }
  393. }
  394. }
  395. .youtube-search {
  396. margin-top: 10px;
  397. .search-query-item:not(:last-of-type) {
  398. margin-bottom: 10px;
  399. }
  400. }
  401. </style>