groupStore.js 919 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import httpRequest from '../api/httpRequest.js'
  2. export default {
  3. state: {
  4. groups: [],
  5. activeIndex: -1,
  6. },
  7. mutations: {
  8. initGroupStore(state) {
  9. httpRequest({
  10. url: '/group/list',
  11. method: 'get'
  12. }).then((groups) => {
  13. this.commit("setGroups",groups);
  14. })
  15. },
  16. setGroups(state,groups){
  17. state.groups = groups;
  18. },
  19. activeGroup(state,index){
  20. state.activeIndex = index;
  21. },
  22. addGroup(state,group){
  23. state.groups.unshift(group);
  24. },
  25. removeGroup(state,groupId){
  26. state.groups.forEach((g,index)=>{
  27. if(g.id==groupId){
  28. state.groups.splice(index, 1);
  29. if(state.activeIndex >= state.groups.length){
  30. state.activeIndex = state.groups.length-1;
  31. }
  32. }
  33. })
  34. },
  35. updateGroup(state,group){
  36. state.groups.forEach((g,idx)=>{
  37. if(g.id==group.id){
  38. // 拷贝属性
  39. state.groups[idx] = Object.assign(state.groups[idx], group);
  40. }
  41. })
  42. }
  43. }
  44. }