teamStore.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import Reflux from 'reflux';
  2. import TeamActions from 'sentry/actions/teamActions';
  3. import {Team} from 'sentry/types';
  4. import {defined} from 'sentry/utils';
  5. import {CommonStoreInterface} from './types';
  6. type State = {
  7. teams: Team[];
  8. loading: boolean;
  9. hasMore: boolean | null;
  10. cursor: string | null;
  11. loadedUserTeams: boolean;
  12. };
  13. type TeamStoreInterface = CommonStoreInterface<State> & {
  14. initialized: boolean;
  15. state: State;
  16. reset(): void;
  17. loadInitialData(items: Team[], hasMore?: boolean | null, cursor?: string | null): void;
  18. onUpdateSuccess(itemId: string, response: Team): void;
  19. onRemoveSuccess(slug: string): void;
  20. onCreateSuccess(team: Team): void;
  21. getAll(): Team[];
  22. getById(id: string): Team | null;
  23. getBySlug(slug: string): Team | null;
  24. };
  25. const teamStoreConfig: Reflux.StoreDefinition & TeamStoreInterface = {
  26. initialized: false,
  27. state: {
  28. teams: [],
  29. loadedUserTeams: false,
  30. loading: true,
  31. hasMore: null,
  32. cursor: null,
  33. },
  34. init() {
  35. this.reset();
  36. this.listenTo(TeamActions.createTeamSuccess, this.onCreateSuccess);
  37. this.listenTo(TeamActions.fetchDetailsSuccess, this.onUpdateSuccess);
  38. this.listenTo(TeamActions.loadTeams, this.loadInitialData);
  39. this.listenTo(TeamActions.loadUserTeams, this.loadUserTeams);
  40. this.listenTo(TeamActions.removeTeamSuccess, this.onRemoveSuccess);
  41. this.listenTo(TeamActions.updateSuccess, this.onUpdateSuccess);
  42. },
  43. reset() {
  44. this.state = {
  45. teams: [],
  46. loadedUserTeams: false,
  47. loading: true,
  48. hasMore: null,
  49. cursor: null,
  50. };
  51. },
  52. loadInitialData(items, hasMore, cursor) {
  53. this.initialized = true;
  54. this.state = {
  55. teams: items.sort((a, b) => a.slug.localeCompare(b.slug)),
  56. loadedUserTeams: defined(hasMore) ? !hasMore : this.state.loadedUserTeams,
  57. loading: false,
  58. hasMore: hasMore ?? this.state.hasMore,
  59. cursor: cursor ?? this.state.cursor,
  60. };
  61. this.trigger(new Set(items.map(item => item.id)));
  62. },
  63. loadUserTeams(userTeams: Team[]) {
  64. const teamIdMap = this.state.teams.reduce((acc: Record<string, Team>, team: Team) => {
  65. acc[team.id] = team;
  66. return acc;
  67. }, {});
  68. // Replace or insert new user teams
  69. userTeams.reduce((acc: Record<string, Team>, userTeam: Team) => {
  70. acc[userTeam.id] = userTeam;
  71. return acc;
  72. }, teamIdMap);
  73. const teams = Object.values(teamIdMap).sort((a, b) => a.slug.localeCompare(b.slug));
  74. this.state = {
  75. ...this.state,
  76. loadedUserTeams: true,
  77. teams,
  78. };
  79. this.trigger(new Set(Object.keys(teamIdMap)));
  80. },
  81. onUpdateSuccess(itemId, response) {
  82. if (!response) {
  83. return;
  84. }
  85. const item = this.getBySlug(itemId);
  86. if (!item) {
  87. this.state = {
  88. ...this.state,
  89. teams: [...this.state.teams, response],
  90. };
  91. this.trigger(new Set([itemId]));
  92. return;
  93. }
  94. // Slug was changed
  95. // Note: This is the proper way to handle slug changes but unfortunately not all of our
  96. // components use stores correctly. To be safe reload browser :((
  97. if (response.slug !== itemId) {
  98. // Replace the team
  99. const teams = [...this.state.teams.filter(({slug}) => slug !== itemId), response];
  100. this.state = {...this.state, teams};
  101. this.trigger(new Set([response.slug]));
  102. return;
  103. }
  104. const newTeams = [...this.state.teams];
  105. const index = newTeams.findIndex(team => team.slug === response.slug);
  106. newTeams[index] = response;
  107. this.state = {...this.state, teams: newTeams};
  108. this.trigger(new Set([itemId]));
  109. },
  110. onRemoveSuccess(slug: string) {
  111. const {teams} = this.state;
  112. this.loadInitialData(teams.filter(team => team.slug !== slug));
  113. },
  114. onCreateSuccess(team: Team) {
  115. this.loadInitialData([...this.state.teams, team]);
  116. },
  117. getState() {
  118. return this.state;
  119. },
  120. getById(id: string) {
  121. const {teams} = this.state;
  122. return teams.find(item => item.id.toString() === id.toString()) || null;
  123. },
  124. getBySlug(slug: string) {
  125. const {teams} = this.state;
  126. return teams.find(item => item.slug === slug) || null;
  127. },
  128. getAll() {
  129. return this.state.teams;
  130. },
  131. };
  132. const TeamStore = Reflux.createStore(teamStoreConfig) as Reflux.Store &
  133. TeamStoreInterface;
  134. export default TeamStore;