Playlists.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <template>
  2. <div id="my-playlists">
  3. <draggable
  4. class="menu-list scrollable-list"
  5. v-if="playlists.length > 0"
  6. v-model="playlists"
  7. v-bind="dragOptions"
  8. @start="drag = true"
  9. @end="drag = false"
  10. @change="savePlaylistOrder"
  11. >
  12. <transition-group
  13. type="transition"
  14. :name="!drag ? 'draggable-list-transition' : null"
  15. >
  16. <playlist-item
  17. :playlist="playlist"
  18. v-for="(playlist, index) in playlists"
  19. :key="'key-' + index"
  20. class="item-draggable"
  21. >
  22. <div class="icons-group" slot="actions">
  23. <i
  24. v-if="
  25. station.type === 'community' &&
  26. (userId === station.owner ||
  27. role === 'admin' ||
  28. station.partyMode) &&
  29. !isSelected(playlist._id)
  30. "
  31. @click="selectPlaylist(playlist)"
  32. class="material-icons play-icon"
  33. :content="
  34. station.partyMode
  35. ? 'Request songs from this playlist'
  36. : 'Play songs from this playlist'
  37. "
  38. v-tippy
  39. >play_arrow</i
  40. >
  41. <i
  42. v-if="
  43. station.type === 'community' &&
  44. (userId === station.owner ||
  45. role === 'admin' ||
  46. station.partyMode) &&
  47. isSelected(playlist._id)
  48. "
  49. @click="deselectPlaylist(playlist._id)"
  50. class="material-icons stop-icon"
  51. :content="
  52. station.partyMode
  53. ? 'Stop requesting songs from this playlist'
  54. : 'Stop playing songs from this playlist'
  55. "
  56. v-tippy
  57. >stop</i
  58. >
  59. <i
  60. @click="edit(playlist._id)"
  61. class="material-icons edit-icon"
  62. content="Edit Playlist"
  63. v-tippy
  64. >edit</i
  65. >
  66. </div>
  67. </playlist-item>
  68. </transition-group>
  69. </draggable>
  70. <p v-else class="nothing-here-text scrollable-list">
  71. No Playlists found
  72. </p>
  73. <a
  74. class="button create-playlist tab-actionable-button"
  75. href="#"
  76. @click="openModal('createPlaylist')"
  77. >
  78. <i class="material-icons icon-with-button">create</i>
  79. <span class="optional-desktop-only-text"> Create Playlist </span>
  80. </a>
  81. </div>
  82. </template>
  83. <script>
  84. import { mapState, mapActions, mapGetters } from "vuex";
  85. import Toast from "toasters";
  86. import draggable from "vuedraggable";
  87. import PlaylistItem from "@/components/PlaylistItem.vue";
  88. import SortablePlaylists from "@/mixins/SortablePlaylists.vue";
  89. export default {
  90. components: { PlaylistItem, draggable },
  91. mixins: [SortablePlaylists],
  92. data() {
  93. return {
  94. playlists: []
  95. };
  96. },
  97. computed: {
  98. currentPlaylists() {
  99. if (this.station.type === "community" && this.station.partyMode) {
  100. return this.partyPlaylists;
  101. }
  102. return this.includedPlaylists;
  103. },
  104. ...mapState({
  105. role: state => state.user.auth.role,
  106. userId: state => state.user.auth.userId
  107. }),
  108. ...mapState("station", {
  109. station: state => state.station,
  110. partyPlaylists: state => state.partyPlaylists,
  111. includedPlaylists: state => state.includedPlaylists,
  112. excludedPlaylists: state => state.excludedPlaylists,
  113. songsList: state => state.songsList
  114. }),
  115. ...mapGetters({
  116. socket: "websockets/getSocket"
  117. })
  118. },
  119. mounted() {
  120. /** Get playlists for user */
  121. this.socket.dispatch("playlists.indexMyPlaylists", true, res => {
  122. if (res.status === "success") this.playlists = res.data.playlists;
  123. this.orderOfPlaylists = this.calculatePlaylistOrder(); // order in regards to the database
  124. });
  125. this.socket.on("event:playlist.create", res => {
  126. this.playlists.push(res.data.playlist);
  127. });
  128. this.socket.on("event:playlist.delete", res => {
  129. this.playlists.forEach((playlist, index) => {
  130. if (playlist._id === res.data.playlistId) {
  131. this.playlists.splice(index, 1);
  132. }
  133. });
  134. });
  135. this.socket.on("event:playlist.addSong", res => {
  136. this.playlists.forEach((playlist, index) => {
  137. if (playlist._id === res.data.playlistId) {
  138. this.playlists[index].songs.push(res.data.song);
  139. }
  140. });
  141. });
  142. this.socket.on("event:playlist.removeSong", res => {
  143. this.playlists.forEach((playlist, index) => {
  144. if (playlist._id === res.data.playlistId) {
  145. this.playlists[index].songs.forEach((song, index2) => {
  146. if (song.youtubeId === res.data.youtubeId) {
  147. this.playlists[index].songs.splice(index2, 1);
  148. }
  149. });
  150. }
  151. });
  152. });
  153. this.socket.on("event:playlist.updateDisplayName", res => {
  154. this.playlists.forEach((playlist, index) => {
  155. if (playlist._id === res.data.playlistId) {
  156. this.playlists[index].displayName = res.data.displayName;
  157. }
  158. });
  159. });
  160. this.socket.on("event:playlist.updatePrivacy", res => {
  161. this.playlists.forEach((playlist, index) => {
  162. if (playlist._id === res.data.playlist._id) {
  163. this.playlists[index].privacy = res.data.playlist.privacy;
  164. }
  165. });
  166. });
  167. this.socket.on(
  168. "event:user.orderOfPlaylists.changed",
  169. orderOfPlaylists => {
  170. const sortedPlaylists = [];
  171. this.playlists.forEach(playlist => {
  172. sortedPlaylists[
  173. orderOfPlaylists.indexOf(playlist._id)
  174. ] = playlist;
  175. });
  176. this.playlists = sortedPlaylists;
  177. this.orderOfPlaylists = this.calculatePlaylistOrder();
  178. }
  179. );
  180. },
  181. methods: {
  182. edit(id) {
  183. this.editPlaylist(id);
  184. this.openModal("editPlaylist");
  185. },
  186. selectPlaylist(playlist) {
  187. if (this.station.type === "community" && this.station.partyMode) {
  188. if (!this.isSelected(playlist.id)) {
  189. this.partyPlaylists.push(playlist);
  190. this.addPartyPlaylistSongToQueue();
  191. new Toast(
  192. "Successfully selected playlist to auto request songs."
  193. );
  194. } else {
  195. new Toast("Error: Playlist already selected.");
  196. }
  197. } else {
  198. this.socket.dispatch(
  199. "stations.includePlaylist",
  200. this.station._id,
  201. playlist._id,
  202. res => {
  203. new Toast(res.message);
  204. }
  205. );
  206. }
  207. },
  208. deselectPlaylist(id) {
  209. if (this.station.type === "community" && this.station.partyMode) {
  210. let selected = false;
  211. this.currentPlaylists.forEach((playlist, index) => {
  212. if (playlist._id === id) {
  213. selected = true;
  214. this.partyPlaylists.splice(index, 1);
  215. }
  216. });
  217. if (selected) {
  218. new Toast("Successfully deselected playlist.");
  219. } else {
  220. new Toast("Playlist not selected.");
  221. }
  222. } else {
  223. this.socket.dispatch(
  224. "stations.removeIncludedPlaylist",
  225. this.station._id,
  226. id,
  227. res => {
  228. new Toast(res.message);
  229. }
  230. );
  231. }
  232. },
  233. isSelected(id) {
  234. // TODO Also change this once it changes for a station
  235. let selected = false;
  236. this.currentPlaylists.forEach(playlist => {
  237. if (playlist._id === id) selected = true;
  238. });
  239. return selected;
  240. },
  241. addPartyPlaylistSongToQueue() {
  242. let isInQueue = false;
  243. if (
  244. this.station.type === "community" &&
  245. this.station.partyMode === true
  246. ) {
  247. this.songsList.forEach(queueSong => {
  248. if (queueSong.requestedBy === this.userId) isInQueue = true;
  249. });
  250. if (!isInQueue && this.partyPlaylists) {
  251. const selectedPlaylist = this.partyPlaylists[
  252. Math.floor(Math.random() * this.partyPlaylists.length)
  253. ];
  254. if (
  255. selectedPlaylist._id &&
  256. selectedPlaylist.songs.length > 0
  257. ) {
  258. const selectedSong =
  259. selectedPlaylist.songs[
  260. Math.floor(
  261. Math.random() *
  262. selectedPlaylist.songs.length
  263. )
  264. ];
  265. if (selectedSong.youtubeId) {
  266. this.socket.dispatch(
  267. "stations.addToQueue",
  268. this.station._id,
  269. selectedSong.youtubeId,
  270. data => {
  271. if (data.status !== "success")
  272. new Toast("Error auto queueing song");
  273. }
  274. );
  275. }
  276. }
  277. }
  278. }
  279. },
  280. ...mapActions("station", ["updatePartyPlaylists"]),
  281. ...mapActions("modalVisibility", ["openModal"]),
  282. ...mapActions("user/playlists", ["editPlaylist"])
  283. }
  284. };
  285. </script>
  286. <style lang="scss" scoped>
  287. #my-playlists {
  288. background-color: var(--white);
  289. margin-bottom: 20px;
  290. border-radius: 0 0 5px 5px;
  291. max-height: 100%;
  292. }
  293. .night-mode {
  294. #my-playlists {
  295. background-color: var(--dark-grey-3) !important;
  296. border: 0 !important;
  297. }
  298. .draggable-list-ghost {
  299. filter: brightness(95%);
  300. }
  301. }
  302. .nothing-here-text {
  303. margin-bottom: 10px;
  304. }
  305. .icons-group {
  306. display: flex;
  307. align-items: center;
  308. .edit-icon {
  309. color: var(--primary-color);
  310. }
  311. }
  312. .menu-list .playlist-item:not(:last-of-type) {
  313. margin-bottom: 10px;
  314. }
  315. .create-playlist {
  316. width: 100%;
  317. height: 40px;
  318. border-radius: 5px;
  319. border: 0;
  320. &:active,
  321. &:focus {
  322. border: 0;
  323. }
  324. }
  325. .draggable-list-transition-move {
  326. transition: transform 0.5s;
  327. }
  328. .draggable-list-ghost {
  329. opacity: 0.5;
  330. filter: brightness(95%);
  331. }
  332. </style>