manageStation.ts 3.0 KB

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