manageStation.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { defineStore } from "pinia";
  2. import { Station } from "@/types/station";
  3. import { Playlist } from "@/types/playlist";
  4. import { CurrentSong, Song } from "@/types/song";
  5. import ws from "@/ws";
  6. export const useManageStationStore = props => {
  7. const { modalUuid } = props;
  8. if (!modalUuid) return null;
  9. return defineStore(`manageStation-${modalUuid}`, {
  10. state: () => ({
  11. stationId: null,
  12. sector: "admin",
  13. tab: "settings",
  14. station: <Station>{},
  15. stationPlaylist: <Playlist>{ songs: [] },
  16. autofill: <Playlist[]>[],
  17. blacklist: <Playlist[]>[],
  18. songsList: <Song[]>[],
  19. stationPaused: true,
  20. currentSong: <CurrentSong>{},
  21. permissions: {}
  22. }),
  23. actions: {
  24. init({ stationId, sector }) {
  25. this.stationId = stationId;
  26. if (sector) this.sector = sector;
  27. },
  28. showTab(tab) {
  29. this.tab = tab;
  30. },
  31. editStation(station) {
  32. this.station = JSON.parse(JSON.stringify(station));
  33. },
  34. setAutofillPlaylists(autofillPlaylists) {
  35. this.autofill = JSON.parse(JSON.stringify(autofillPlaylists));
  36. },
  37. setBlacklist(blacklist) {
  38. this.blacklist = JSON.parse(JSON.stringify(blacklist));
  39. },
  40. clearStation() {
  41. this.station = {};
  42. this.stationPlaylist = { songs: [] };
  43. this.autofill = [];
  44. this.blacklist = [];
  45. this.songsList = [];
  46. this.stationPaused = true;
  47. this.currentSong = {};
  48. this.permissions = {};
  49. },
  50. updateSongsList(songsList) {
  51. this.songsList = songsList;
  52. },
  53. updateStationPlaylist(stationPlaylist) {
  54. this.stationPlaylist = stationPlaylist;
  55. },
  56. repositionSongInList(song) {
  57. if (
  58. this.songsList[song.newIndex] &&
  59. this.songsList[song.newIndex].youtubeId === song.youtubeId
  60. )
  61. return;
  62. this.songsList.splice(
  63. song.newIndex,
  64. 0,
  65. this.songsList.splice(song.oldIndex, 1)[0]
  66. );
  67. },
  68. updateStationPaused(stationPaused) {
  69. this.stationPaused = stationPaused;
  70. },
  71. updateCurrentSong(currentSong) {
  72. this.currentSong = currentSong;
  73. },
  74. updateStation(station) {
  75. this.station = { ...this.station, ...station };
  76. },
  77. updateIsFavorited(isFavorited) {
  78. this.station.isFavorited = isFavorited;
  79. },
  80. hasPermission(permission) {
  81. return !!(this.permissions && this.permissions[permission]);
  82. },
  83. updatePermissions() {
  84. return new Promise(resolve => {
  85. ws.socket.dispatch(
  86. "utils.getPermissions",
  87. this.station._id,
  88. res => {
  89. this.permissions = res.data.permissions;
  90. resolve(this.permissions);
  91. }
  92. );
  93. });
  94. },
  95. addDj(user) {
  96. this.station.djs.push(user);
  97. },
  98. removeDj(user) {
  99. this.station.djs.forEach((dj, index) => {
  100. if (dj._id === user._id) {
  101. this.station.djs.splice(index, 1);
  102. }
  103. });
  104. }
  105. }
  106. })();
  107. };