groupStore.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { defineStore } from 'pinia';
  2. import http from '@/common/request';
  3. export default defineStore('groupStore', {
  4. state: () => {
  5. return {
  6. groupMap: new Map()
  7. }
  8. },
  9. actions: {
  10. setGroups(groups) {
  11. this.groupMap.clear();
  12. groups.forEach(g => this.groupMap.set(g.id, g))
  13. },
  14. addGroup(group) {
  15. this.groupMap.set(group.id, group);
  16. },
  17. removeGroup(id) {
  18. this.groupMap.get(id).quit = true;
  19. },
  20. updateGroup(group) {
  21. this.groupMap.set(group.id, group);
  22. },
  23. setDnd(id, isDnd) {
  24. this.groupMap.get(id).isDnd = isDnd;
  25. },
  26. clear() {
  27. this.groupMap.clear();
  28. },
  29. loadGroup() {
  30. return new Promise((resolve, reject) => {
  31. http({
  32. url: '/group/list',
  33. method: 'GET'
  34. }).then((groups) => {
  35. this.setGroups(groups);
  36. resolve();
  37. }).catch((res) => {
  38. reject(res);
  39. })
  40. });
  41. }
  42. },
  43. getters: {
  44. groups: (state) => {
  45. return Array.from(state.groupMap.values());
  46. },
  47. findGroup: (state) => (id) => {
  48. return state.groupMap.get(id);
  49. },
  50. isGroup: (state) => (id) => {
  51. let group = state.findGroup(id);
  52. return group && !group.quit
  53. }
  54. }
  55. })