AddSongToPlaylist.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <modal title="Add Song To Playlist">
  3. <template v-slot:body>
  4. <h4 class="songTitle">{{ $parent.currentSong.title }}</h4>
  5. <h5 class="songArtist">{{ $parent.currentSong.artists }}</h5>
  6. <aside class="menu">
  7. <p class="menu-label">Playlists</p>
  8. <ul class="menu-list">
  9. <li v-for="(playlist, index) in playlistsArr" :key="index">
  10. <div class="playlist">
  11. <span
  12. class="icon is-small"
  13. v-on:click="removeSongFromPlaylist(playlist._id)"
  14. v-if="playlists[playlist._id].hasSong"
  15. >
  16. <i class="material-icons">playlist_add_check</i>
  17. </span>
  18. <span class="icon" v-on:click="addSongToPlaylist(playlist._id)" v-else>
  19. <i class="material-icons">playlist_add</i>
  20. </span>
  21. {{ playlist.displayName }}
  22. </div>
  23. </li>
  24. </ul>
  25. </aside>
  26. </template>
  27. </modal>
  28. </template>
  29. <script>
  30. import { Toast } from "vue-roaster";
  31. import Modal from "./Modal.vue";
  32. import io from "../../io";
  33. import auth from "../../auth";
  34. export default {
  35. data() {
  36. return {
  37. playlists: {},
  38. playlistsArr: [],
  39. songId: null,
  40. song: null
  41. };
  42. },
  43. methods: {
  44. addSongToPlaylist: function(playlistId) {
  45. let _this = this;
  46. this.socket.emit(
  47. "playlists.addSongToPlaylist",
  48. this.$parent.currentSong.songId,
  49. playlistId,
  50. res => {
  51. Toast.methods.addToast(res.message, 4000);
  52. if (res.status === "success") {
  53. _this.playlists[playlistId].songs.push(_this.song);
  54. }
  55. _this.recalculatePlaylists();
  56. //this.$parent.modals.addSongToPlaylist = false;
  57. }
  58. );
  59. },
  60. removeSongFromPlaylist: function(playlistId) {
  61. let _this = this;
  62. this.socket.emit(
  63. "playlists.removeSongFromPlaylist",
  64. _this.songId,
  65. playlistId,
  66. res => {
  67. Toast.methods.addToast(res.message, 4000);
  68. if (res.status === "success") {
  69. _this.playlists[playlistId].songs.forEach((song, index) => {
  70. if (song.songId === _this.songId)
  71. _this.playlists[playlistId].songs.splice(index, 1);
  72. });
  73. }
  74. _this.recalculatePlaylists();
  75. //this.$parent.modals.addSongToPlaylist = false;
  76. }
  77. );
  78. },
  79. recalculatePlaylists: function() {
  80. let _this = this;
  81. _this.playlistsArr = Object.values(_this.playlists).map(playlist => {
  82. let hasSong = false;
  83. for (let i = 0; i < playlist.songs.length; i++) {
  84. if (playlist.songs[i].songId === _this.songId) {
  85. hasSong = true;
  86. }
  87. }
  88. playlist.hasSong = hasSong;
  89. _this.playlists[playlist._id] = playlist;
  90. return playlist;
  91. });
  92. }
  93. },
  94. mounted: function() {
  95. let _this = this;
  96. this.songId = this.$parent.currentSong.songId;
  97. this.song = this.$parent.currentSong;
  98. io.getSocket(socket => {
  99. _this.socket = socket;
  100. _this.socket.emit("playlists.indexForUser", res => {
  101. if (res.status === "success") {
  102. res.data.forEach(playlist => {
  103. _this.playlists[playlist._id] = playlist;
  104. });
  105. _this.recalculatePlaylists();
  106. }
  107. });
  108. });
  109. },
  110. components: { Modal }
  111. };
  112. </script>
  113. <style lang='scss' scoped>
  114. .icon.is-small {
  115. margin-right: 10px !important;
  116. }
  117. .songTitle {
  118. font-size: 22px;
  119. padding: 0 10px;
  120. }
  121. .songArtist {
  122. font-size: 19px;
  123. font-weight: 200;
  124. padding: 0 10px;
  125. }
  126. .menu-label {
  127. font-size: 16px;
  128. }
  129. </style>