SortablePlaylists.vue 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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: "description",
  21. disabled: false,
  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.emit(
  42. "users.updateOrderOfPlaylists",
  43. recalculatedOrder,
  44. res => {
  45. if (res.status === "failure")
  46. return new Toast({
  47. content: res.message,
  48. timeout: 8000
  49. });
  50. this.orderOfPlaylists = this.calculatePlaylistOrder(); // new order in regards to the database
  51. return new Toast({ content: res.message, timeout: 4000 });
  52. }
  53. );
  54. }
  55. }
  56. };
  57. </script>