Playlists.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. :class="{ selected: tab === 'current' }"
  8. @click="showTab('current')"
  9. >
  10. Current
  11. </button>
  12. <button
  13. class="button is-default"
  14. :class="{ selected: tab === 'search' }"
  15. @click="showTab('search')"
  16. >
  17. Search
  18. </button>
  19. <button
  20. v-if="station.type === 'community'"
  21. class="button is-default"
  22. :class="{ selected: tab === 'my-playlists' }"
  23. @click="showTab('my-playlists')"
  24. >
  25. My Playlists
  26. </button>
  27. </div>
  28. <div class="tab" v-show="tab === 'current'">
  29. <div v-if="currentPlaylists.length > 0">
  30. <playlist-item
  31. v-for="(playlist, index) in currentPlaylists"
  32. :key="'key-' + index"
  33. :playlist="playlist"
  34. :show-owner="true"
  35. >
  36. <div class="icons-group" slot="actions">
  37. <confirm
  38. v-if="isOwnerOrAdmin()"
  39. @confirm="deselectPlaylist(playlist._id)"
  40. >
  41. <i
  42. class="material-icons stop-icon"
  43. content="Stop playing songs from this playlist"
  44. v-tippy
  45. >
  46. stop
  47. </i>
  48. </confirm>
  49. <confirm
  50. v-if="isOwnerOrAdmin()"
  51. @confirm="blacklistPlaylist(playlist._id)"
  52. >
  53. <i
  54. class="material-icons stop-icon"
  55. content="Blacklist Playlist"
  56. v-tippy
  57. >block</i
  58. >
  59. </confirm>
  60. <i
  61. v-if="playlist.createdBy === myUserId"
  62. @click="showPlaylist(playlist._id)"
  63. class="material-icons edit-icon"
  64. content="Edit Playlist"
  65. v-tippy
  66. >edit</i
  67. >
  68. <i
  69. v-if="
  70. playlist.createdBy !== myUserId &&
  71. (playlist.privacy === 'public' ||
  72. isAdmin())
  73. "
  74. @click="showPlaylist(playlist._id)"
  75. class="material-icons edit-icon"
  76. content="View Playlist"
  77. v-tippy
  78. >visibility</i
  79. >
  80. </div>
  81. </playlist-item>
  82. </div>
  83. <p v-else class="has-text-centered scrollable-list">
  84. No playlists currently selected.
  85. </p>
  86. </div>
  87. <div class="tab" v-show="tab === 'search'">
  88. <label class="label"> Search for a public playlist </label>
  89. <div class="control is-grouped input-with-button">
  90. <p class="control is-expanded">
  91. <input
  92. class="input"
  93. type="text"
  94. placeholder="Enter your playlist query here..."
  95. v-model="search.query"
  96. @keyup.enter="searchForPlaylists(1)"
  97. />
  98. </p>
  99. <p class="control">
  100. <a class="button is-info" @click="searchForPlaylists(1)"
  101. ><i class="material-icons icon-with-button"
  102. >search</i
  103. >Search</a
  104. >
  105. </p>
  106. </div>
  107. <div v-if="search.results.length > 0">
  108. <playlist-item
  109. v-for="(playlist, index) in search.results"
  110. :key="'searchKey-' + index"
  111. :playlist="playlist"
  112. :show-owner="true"
  113. >
  114. <div class="icons-group" slot="actions">
  115. <confirm
  116. v-if="
  117. (isOwnerOrAdmin() ||
  118. (station.type === 'community' &&
  119. station.partyMode)) &&
  120. isSelected(playlist._id)
  121. "
  122. @confirm="deselectPlaylist(playlist._id)"
  123. >
  124. <i
  125. class="material-icons stop-icon"
  126. content="Stop playing songs from this playlist"
  127. v-tippy
  128. >
  129. stop
  130. </i>
  131. </confirm>
  132. <i
  133. v-if="
  134. (isOwnerOrAdmin() ||
  135. (station.type === 'community' &&
  136. station.partyMode)) &&
  137. !isSelected(playlist._id)
  138. "
  139. @click="selectPlaylist(playlist)"
  140. class="material-icons play-icon"
  141. :content="
  142. station.partyMode
  143. ? 'Request songs from this playlist'
  144. : 'Play songs from this playlist'
  145. "
  146. v-tippy
  147. >play_arrow</i
  148. >
  149. <confirm
  150. v-if="
  151. isOwnerOrAdmin() &&
  152. !isSelected(playlist._id) &&
  153. !isExcluded(playlist._id)
  154. "
  155. @confirm="blacklistPlaylist(playlist._id)"
  156. >
  157. <i
  158. class="material-icons stop-icon"
  159. content="Blacklist Playlist"
  160. v-tippy
  161. >block</i
  162. >
  163. </confirm>
  164. <i
  165. v-if="playlist.createdBy === myUserId"
  166. @click="showPlaylist(playlist._id)"
  167. class="material-icons edit-icon"
  168. content="Edit Playlist"
  169. v-tippy
  170. >edit</i
  171. >
  172. <i
  173. v-if="
  174. playlist.createdBy !== myUserId &&
  175. (playlist.privacy === 'public' ||
  176. isAdmin())
  177. "
  178. @click="showPlaylist(playlist._id)"
  179. class="material-icons edit-icon"
  180. content="View Playlist"
  181. v-tippy
  182. >visibility</i
  183. >
  184. </div>
  185. </playlist-item>
  186. <button
  187. v-if="resultsLeftCount > 0"
  188. class="button is-primary"
  189. @click="searchForPlaylists(search.page + 1)"
  190. >
  191. Load {{ nextPageResultsCount }} more results
  192. </button>
  193. </div>
  194. </div>
  195. <div
  196. v-if="station.type === 'community'"
  197. class="tab"
  198. v-show="tab === 'my-playlists'"
  199. >
  200. <button
  201. class="button is-primary"
  202. id="create-new-playlist-button"
  203. @click="openModal('createPlaylist')"
  204. >
  205. Create new playlist
  206. </button>
  207. <draggable
  208. class="menu-list scrollable-list"
  209. v-if="playlists.length > 0"
  210. v-model="playlists"
  211. v-bind="dragOptions"
  212. @start="drag = true"
  213. @end="drag = false"
  214. @change="savePlaylistOrder"
  215. >
  216. <transition-group
  217. type="transition"
  218. :name="!drag ? 'draggable-list-transition' : null"
  219. >
  220. <playlist-item
  221. class="item-draggable"
  222. v-for="playlist in playlists"
  223. :key="playlist._id"
  224. :playlist="playlist"
  225. >
  226. <div slot="actions">
  227. <i
  228. v-if="
  229. station.type === 'community' &&
  230. (isOwnerOrAdmin() ||
  231. station.partyMode) &&
  232. !isSelected(playlist._id) &&
  233. !isExcluded(playlist._id)
  234. "
  235. @click="selectPlaylist(playlist)"
  236. class="material-icons play-icon"
  237. :content="
  238. station.partyMode
  239. ? 'Request songs from this playlist'
  240. : 'Play songs from this playlist'
  241. "
  242. v-tippy
  243. >play_arrow</i
  244. >
  245. <confirm
  246. v-if="
  247. station.type === 'community' &&
  248. (isOwnerOrAdmin() ||
  249. station.partyMode) &&
  250. isSelected(playlist._id)
  251. "
  252. @confirm="deselectPlaylist(playlist._id)"
  253. >
  254. <i
  255. class="material-icons stop-icon"
  256. :content="
  257. station.partyMode
  258. ? 'Stop requesting songs from this playlist'
  259. : 'Stop playing songs from this playlist'
  260. "
  261. v-tippy
  262. >stop</i
  263. >
  264. </confirm>
  265. <confirm
  266. v-if="
  267. isOwnerOrAdmin() &&
  268. !isExcluded(playlist._id)
  269. "
  270. @confirm="blacklistPlaylist(playlist._id)"
  271. >
  272. <i
  273. class="material-icons stop-icon"
  274. content="Blacklist Playlist"
  275. v-tippy
  276. >block</i
  277. >
  278. </confirm>
  279. <i
  280. @click="showPlaylist(playlist._id)"
  281. class="material-icons edit-icon"
  282. content="Edit Playlist"
  283. v-tippy
  284. >edit</i
  285. >
  286. </div>
  287. </playlist-item>
  288. </transition-group>
  289. </draggable>
  290. <p v-else class="has-text-centered scrollable-list">
  291. You don't have any playlists!
  292. </p>
  293. </div>
  294. </div>
  295. </div>
  296. </template>
  297. <script>
  298. import { mapActions, mapState, mapGetters } from "vuex";
  299. import Toast from "toasters";
  300. import PlaylistItem from "@/components/PlaylistItem.vue";
  301. import Confirm from "@/components/Confirm.vue";
  302. import SortablePlaylists from "@/mixins/SortablePlaylists.vue";
  303. export default {
  304. components: {
  305. PlaylistItem,
  306. Confirm
  307. },
  308. mixins: [SortablePlaylists],
  309. data() {
  310. return {
  311. tab: "current",
  312. search: {
  313. query: "",
  314. searchedQuery: "",
  315. page: 0,
  316. count: 0,
  317. resultsLeft: 0,
  318. results: []
  319. }
  320. };
  321. },
  322. computed: {
  323. currentPlaylists() {
  324. if (this.station.type === "community" && this.station.partyMode) {
  325. return this.partyPlaylists;
  326. }
  327. return this.includedPlaylists;
  328. },
  329. resultsLeftCount() {
  330. return this.search.count - this.search.results.length;
  331. },
  332. nextPageResultsCount() {
  333. return Math.min(this.search.pageSize, this.resultsLeftCount);
  334. },
  335. ...mapState({
  336. loggedIn: state => state.user.auth.loggedIn,
  337. role: state => state.user.auth.role,
  338. userId: state => state.user.auth.userId,
  339. partyPlaylists: state => state.station.partyPlaylists
  340. }),
  341. ...mapState("modals/manageStation", {
  342. originalStation: state => state.originalStation,
  343. includedPlaylists: state => state.includedPlaylists,
  344. excludedPlaylists: state => state.excludedPlaylists,
  345. songsList: state => state.songsList
  346. }),
  347. ...mapGetters({
  348. socket: "websockets/getSocket"
  349. })
  350. },
  351. mounted() {
  352. this.socket.dispatch("playlists.indexMyPlaylists", true, res => {
  353. if (res.status === "success") this.setPlaylists(res.data.playlists);
  354. this.orderOfPlaylists = this.calculatePlaylistOrder(); // order in regards to the database
  355. });
  356. this.socket.dispatch(
  357. `stations.getStationIncludedPlaylistsById`,
  358. this.station._id,
  359. res => {
  360. if (res.status === "success") {
  361. this.station.includedPlaylists = res.data.playlists;
  362. this.originalStation.includedPlaylists = res.data.playlists;
  363. }
  364. }
  365. );
  366. this.socket.dispatch(
  367. `stations.getStationExcludedPlaylistsById`,
  368. this.station._id,
  369. res => {
  370. if (res.status === "success") {
  371. this.station.excludedPlaylists = res.data.playlists;
  372. this.originalStation.excludedPlaylists = res.data.playlists;
  373. }
  374. }
  375. );
  376. },
  377. methods: {
  378. showTab(tab) {
  379. this.tab = tab;
  380. },
  381. isOwner() {
  382. return this.loggedIn && this.userId === this.station.owner;
  383. },
  384. isAdmin() {
  385. return this.loggedIn && this.role === "admin";
  386. },
  387. isOwnerOrAdmin() {
  388. return this.isOwner() || this.isAdmin();
  389. },
  390. showPlaylist(playlistId) {
  391. this.editPlaylist(playlistId);
  392. this.openModal("editPlaylist");
  393. },
  394. selectPlaylist(playlist) {
  395. if (this.station.type === "community" && this.station.partyMode) {
  396. if (!this.isSelected(playlist.id)) {
  397. this.partyPlaylists.push(playlist);
  398. this.addPartyPlaylistSongToQueue();
  399. new Toast(
  400. "Successfully selected playlist to auto request songs."
  401. );
  402. } else {
  403. new Toast("Error: Playlist already selected.");
  404. }
  405. } else {
  406. this.socket.dispatch(
  407. "stations.includePlaylist",
  408. this.station._id,
  409. playlist._id,
  410. res => {
  411. new Toast(res.message);
  412. }
  413. );
  414. }
  415. },
  416. deselectPlaylist(id) {
  417. if (this.station.type === "community" && this.station.partyMode) {
  418. let selected = false;
  419. this.currentPlaylists.forEach((playlist, index) => {
  420. if (playlist._id === id) {
  421. selected = true;
  422. this.partyPlaylists.splice(index, 1);
  423. }
  424. });
  425. if (selected) {
  426. new Toast("Successfully deselected playlist.");
  427. } else {
  428. new Toast("Playlist not selected.");
  429. }
  430. } else {
  431. this.socket.dispatch(
  432. "stations.removeIncludedPlaylist",
  433. this.station._id,
  434. id,
  435. res => {
  436. new Toast(res.message);
  437. }
  438. );
  439. }
  440. },
  441. isSelected(id) {
  442. // TODO Also change this once it changes for a station
  443. let selected = false;
  444. this.currentPlaylists.forEach(playlist => {
  445. if (playlist._id === id) selected = true;
  446. });
  447. return selected;
  448. },
  449. isExcluded(id) {
  450. let selected = false;
  451. this.excludedPlaylists.forEach(playlist => {
  452. if (playlist._id === id) selected = true;
  453. });
  454. return selected;
  455. },
  456. searchForPlaylists(page) {
  457. if (
  458. this.search.page >= page ||
  459. this.search.searchedQuery !== this.search.query
  460. ) {
  461. this.search.results = [];
  462. this.search.page = 0;
  463. this.search.count = 0;
  464. this.search.resultsLeft = 0;
  465. this.search.pageSize = 0;
  466. }
  467. const { query } = this.search;
  468. const action =
  469. this.station.type === "official"
  470. ? "playlists.searchOfficial"
  471. : "playlists.searchCommunity";
  472. this.search.searchedQuery = this.search.query;
  473. this.socket.dispatch(action, query, page, res => {
  474. const { data } = res;
  475. const { count, pageSize, playlists } = data;
  476. if (res.status === "success") {
  477. this.search.results = [
  478. ...this.search.results,
  479. ...playlists
  480. ];
  481. this.search.page = page;
  482. this.search.count = count;
  483. this.search.resultsLeft =
  484. count - this.search.results.length;
  485. this.search.pageSize = pageSize;
  486. } else if (res.status === "error") {
  487. this.search.results = [];
  488. this.search.page = 0;
  489. this.search.count = 0;
  490. this.search.resultsLeft = 0;
  491. this.search.pageSize = 0;
  492. new Toast(res.message);
  493. }
  494. });
  495. },
  496. blacklistPlaylist(id) {
  497. if (this.isSelected(id)) {
  498. this.deselectPlaylist(id);
  499. }
  500. this.socket.dispatch(
  501. "stations.excludePlaylist",
  502. this.station._id,
  503. id,
  504. res => {
  505. new Toast(res.message);
  506. }
  507. );
  508. },
  509. removeExcludedPlaylist(id) {
  510. this.socket.dispatch(
  511. "stations.removeExcludedPlaylist",
  512. this.station._id,
  513. id,
  514. res => {
  515. new Toast(res.message);
  516. }
  517. );
  518. },
  519. addPartyPlaylistSongToQueue() {
  520. let isInQueue = false;
  521. if (
  522. this.station.type === "community" &&
  523. this.station.partyMode === true
  524. ) {
  525. this.songsList.forEach(queueSong => {
  526. if (queueSong.requestedBy === this.userId) isInQueue = true;
  527. });
  528. if (!isInQueue && this.partyPlaylists) {
  529. const selectedPlaylist = this.partyPlaylists[
  530. Math.floor(Math.random() * this.partyPlaylists.length)
  531. ];
  532. if (
  533. selectedPlaylist._id &&
  534. selectedPlaylist.songs.length > 0
  535. ) {
  536. const selectedSong =
  537. selectedPlaylist.songs[
  538. Math.floor(
  539. Math.random() *
  540. selectedPlaylist.songs.length
  541. )
  542. ];
  543. if (selectedSong.youtubeId) {
  544. this.socket.dispatch(
  545. "stations.addToQueue",
  546. this.station._id,
  547. selectedSong.youtubeId,
  548. data => {
  549. if (data.status !== "success")
  550. new Toast("Error auto queueing song");
  551. }
  552. );
  553. }
  554. }
  555. }
  556. }
  557. },
  558. ...mapActions("station", ["updatePartyPlaylists"]),
  559. ...mapActions("modalVisibility", ["openModal"]),
  560. ...mapActions("user/playlists", ["editPlaylist", "setPlaylists"])
  561. }
  562. };
  563. </script>
  564. <style lang="scss" scoped>
  565. .station-playlists {
  566. .tabs-container {
  567. .tab-selection {
  568. display: flex;
  569. .button {
  570. border-radius: 0;
  571. border: 0;
  572. text-transform: uppercase;
  573. font-size: 14px;
  574. color: var(--dark-grey-3);
  575. background-color: var(--light-grey-2);
  576. flex-grow: 1;
  577. height: 32px;
  578. &:not(:first-of-type) {
  579. margin-left: 5px;
  580. }
  581. }
  582. .selected {
  583. background-color: var(--dark-grey-3) !important;
  584. color: var(--white) !important;
  585. }
  586. }
  587. .tab {
  588. padding: 15px 0;
  589. border-radius: 0;
  590. .playlist-item:not(:last-of-type),
  591. .item.item-draggable:not(:last-of-type) {
  592. margin-bottom: 10px;
  593. }
  594. }
  595. }
  596. }
  597. .draggable-list-transition-move {
  598. transition: transform 0.5s;
  599. }
  600. .draggable-list-ghost {
  601. opacity: 0.5;
  602. filter: brightness(95%);
  603. }
  604. </style>