groupStore.js 1.0 KB

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