SortablePlaylists.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <script>
  2. import { mapState } 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. ...mapState({
  15. station: state => state.station.station,
  16. myUserId: state => state.user.auth.userId
  17. }),
  18. playlists: {
  19. get() {
  20. return this.$store.state.user.playlists.playlists;
  21. },
  22. set(playlists) {
  23. this.$store.commit("user/playlists/setPlaylists", playlists);
  24. }
  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.playlists.push(res.data.playlist),
  39. { replaceable: true }
  40. );
  41. this.socket.on(
  42. "event:playlist.deleted",
  43. res => {
  44. this.playlists.forEach((playlist, index) => {
  45. if (playlist._id === res.data.playlistId) {
  46. this.playlists.splice(index, 1);
  47. }
  48. });
  49. },
  50. { replaceable: true }
  51. );
  52. this.socket.on(
  53. "event:playlist.song.added",
  54. res => {
  55. this.playlists.forEach((playlist, index) => {
  56. if (playlist._id === res.data.playlistId) {
  57. this.playlists[index].songs.push(res.data.song);
  58. }
  59. });
  60. },
  61. { replaceable: true }
  62. );
  63. this.socket.on(
  64. "event:playlist.song.removed",
  65. res => {
  66. this.playlists.forEach((playlist, index) => {
  67. if (playlist._id === res.data.playlistId) {
  68. this.playlists[index].songs.forEach((song, index2) => {
  69. if (song.youtubeId === res.data.youtubeId) {
  70. this.playlists[index].songs.splice(index2, 1);
  71. }
  72. });
  73. }
  74. });
  75. },
  76. { replaceable: true }
  77. );
  78. this.socket.on(
  79. "event:playlist.displayName.updated",
  80. res => {
  81. this.playlists.forEach((playlist, index) => {
  82. if (playlist._id === res.data.playlistId) {
  83. this.playlists[index].displayName =
  84. res.data.displayName;
  85. }
  86. });
  87. },
  88. { replaceable: true }
  89. );
  90. this.socket.on(
  91. "event:playlist.privacy.updated",
  92. res => {
  93. this.playlists.forEach((playlist, index) => {
  94. if (playlist._id === res.data.playlist._id) {
  95. this.playlists[index].privacy =
  96. res.data.playlist.privacy;
  97. }
  98. });
  99. },
  100. { replaceable: true }
  101. );
  102. this.socket.on(
  103. "event:user.orderOfPlaylists.updated",
  104. res => {
  105. const sortedPlaylists = [];
  106. this.playlists.forEach(playlist => {
  107. sortedPlaylists[
  108. res.data.order.indexOf(playlist._id)
  109. ] = playlist;
  110. });
  111. this.playlists = sortedPlaylists;
  112. this.orderOfPlaylists = this.calculatePlaylistOrder();
  113. },
  114. { replaceable: true }
  115. );
  116. },
  117. methods: {
  118. calculatePlaylistOrder() {
  119. const calculatedOrder = [];
  120. this.playlists.forEach(playlist =>
  121. calculatedOrder.push(playlist._id)
  122. );
  123. return calculatedOrder;
  124. },
  125. savePlaylistOrder() {
  126. const recalculatedOrder = this.calculatePlaylistOrder();
  127. if (
  128. JSON.stringify(this.orderOfPlaylists) ===
  129. JSON.stringify(recalculatedOrder)
  130. )
  131. return; // nothing has changed
  132. this.socket.dispatch(
  133. "users.updateOrderOfPlaylists",
  134. recalculatedOrder,
  135. res => {
  136. if (res.status === "error") return new Toast(res.message);
  137. this.orderOfPlaylists = this.calculatePlaylistOrder(); // new order in regards to the database
  138. return new Toast(res.message);
  139. }
  140. );
  141. }
  142. }
  143. };
  144. </script>