SortablePlaylists.vue 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. }),
  17. dragOptions() {
  18. return {
  19. animation: 200,
  20. group: "playlists",
  21. disabled: this.myUserId !== this.userId,
  22. ghostClass: "draggable-list-ghost"
  23. };
  24. }
  25. },
  26. methods: {
  27. calculatePlaylistOrder() {
  28. const calculatedOrder = [];
  29. this.playlists.forEach(playlist =>
  30. calculatedOrder.push(playlist._id)
  31. );
  32. return calculatedOrder;
  33. },
  34. savePlaylistOrder() {
  35. const recalculatedOrder = this.calculatePlaylistOrder();
  36. if (
  37. JSON.stringify(this.orderOfPlaylists) ===
  38. JSON.stringify(recalculatedOrder)
  39. )
  40. return; // nothing has changed
  41. this.socket.dispatch(
  42. "users.updateOrderOfPlaylists",
  43. recalculatedOrder,
  44. res => {
  45. if (res.status === "error") return new Toast(res.message);
  46. this.orderOfPlaylists = this.calculatePlaylistOrder(); // new order in regards to the database
  47. return new Toast(res.message);
  48. }
  49. );
  50. }
  51. }
  52. };
  53. </script>