SortablePlaylists.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <script>
  2. import { mapState, mapActions } from "vuex";
  3. import Toast from "toasters";
  4. import draggable from "vuedraggable";
  5. export default {
  6. components: { draggable },
  7. data() {
  8. return {
  9. orderOfPlaylists: [],
  10. drag: false
  11. };
  12. },
  13. computed: {
  14. playlists: {
  15. get() {
  16. return this.$store.state.user.playlists.playlists;
  17. },
  18. set(playlists) {
  19. this.$store.commit("user/playlists/updatePlaylists", playlists);
  20. }
  21. },
  22. ...mapState({
  23. station: state => state.station.station,
  24. myUserId: state => state.user.auth.userId
  25. }),
  26. dragOptions() {
  27. return {
  28. animation: 200,
  29. group: "playlists",
  30. disabled: this.myUserId !== this.userId,
  31. ghostClass: "draggable-list-ghost"
  32. };
  33. }
  34. },
  35. mounted() {
  36. this.socket.on(
  37. "event:playlist.created",
  38. res => this.addPlaylist(res.data.playlist),
  39. { replaceable: true }
  40. );
  41. this.socket.on(
  42. "event:playlist.deleted",
  43. res => this.removePlaylist(res.data.playlistId),
  44. { replaceable: true }
  45. );
  46. this.socket.on(
  47. "event:playlist.song.added",
  48. res => {
  49. this.playlists.forEach((playlist, index) => {
  50. if (playlist._id === res.data.playlistId) {
  51. this.playlists[index].songs.push(res.data.song);
  52. }
  53. });
  54. },
  55. { replaceable: true }
  56. );
  57. this.socket.on(
  58. "event:playlist.song.removed",
  59. res => {
  60. this.playlists.forEach((playlist, playlistIndex) => {
  61. if (playlist._id === res.data.playlistId) {
  62. this.playlists[playlistIndex].songs.forEach(
  63. (song, songIndex) => {
  64. if (song.youtubeId === res.data.youtubeId) {
  65. this.playlists[playlistIndex].songs.splice(
  66. songIndex,
  67. 1
  68. );
  69. }
  70. }
  71. );
  72. }
  73. });
  74. },
  75. { replaceable: true }
  76. );
  77. this.socket.on(
  78. "event:playlist.displayName.updated",
  79. res => {
  80. this.playlists.forEach((playlist, index) => {
  81. if (playlist._id === res.data.playlistId) {
  82. this.playlists[index].displayName =
  83. res.data.displayName;
  84. }
  85. });
  86. },
  87. { replaceable: true }
  88. );
  89. this.socket.on(
  90. "event:playlist.privacy.updated",
  91. res => {
  92. this.playlists.forEach((playlist, index) => {
  93. if (playlist._id === res.data.playlist._id) {
  94. this.playlists[index].privacy =
  95. res.data.playlist.privacy;
  96. }
  97. });
  98. },
  99. { replaceable: true }
  100. );
  101. this.socket.on(
  102. "event:user.orderOfPlaylists.updated",
  103. res => {
  104. const sortedPlaylists = [];
  105. this.playlists.forEach(playlist => {
  106. sortedPlaylists[res.data.order.indexOf(playlist._id)] =
  107. playlist;
  108. });
  109. this.playlists = sortedPlaylists;
  110. this.orderOfPlaylists = this.calculatePlaylistOrder();
  111. },
  112. { replaceable: true }
  113. );
  114. },
  115. methods: {
  116. calculatePlaylistOrder() {
  117. const calculatedOrder = [];
  118. this.playlists.forEach(playlist =>
  119. calculatedOrder.push(playlist._id)
  120. );
  121. return calculatedOrder;
  122. },
  123. savePlaylistOrder() {
  124. const recalculatedOrder = this.calculatePlaylistOrder();
  125. if (
  126. JSON.stringify(this.orderOfPlaylists) ===
  127. JSON.stringify(recalculatedOrder)
  128. )
  129. return; // nothing has changed
  130. this.socket.dispatch(
  131. "users.updateOrderOfPlaylists",
  132. recalculatedOrder,
  133. res => {
  134. if (res.status === "error") return new Toast(res.message);
  135. this.orderOfPlaylists = this.calculatePlaylistOrder(); // new order in regards to the database
  136. return new Toast(res.message);
  137. }
  138. );
  139. },
  140. ...mapActions("user/playlists", ["addPlaylist", "removePlaylist"])
  141. }
  142. };
  143. </script>