teams.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  2. import TeamActions from 'sentry/actions/teamActions';
  3. import {Client} from 'sentry/api';
  4. import {tct} from 'sentry/locale';
  5. import {Team} from 'sentry/types';
  6. import {callIfFunction} from 'sentry/utils/callIfFunction';
  7. import {uniqueId} from 'sentry/utils/guid';
  8. type CallbackOptions = {
  9. error?: Function;
  10. success?: Function;
  11. };
  12. const doCallback = (
  13. params: CallbackOptions = {},
  14. name: keyof CallbackOptions,
  15. ...args: any[]
  16. ) => {
  17. callIfFunction(params[name], ...args);
  18. };
  19. /**
  20. * Note these are both slugs
  21. */
  22. type OrgSlug = {orgId: string};
  23. type OrgAndTeamSlug = OrgSlug & {teamId: string};
  24. /**
  25. * This is the actual internal id, not username or email
  26. */
  27. type MemberId = {memberId: string};
  28. // Fetch teams for org
  29. export function fetchTeams(api: Client, params: OrgSlug, options: CallbackOptions) {
  30. TeamActions.fetchAll(params.orgId);
  31. return api.request(`/teams/${params.orgId}/`, {
  32. success: data => {
  33. TeamActions.fetchAllSuccess(params.orgId, data);
  34. doCallback(options, 'success', data);
  35. },
  36. error: error => {
  37. TeamActions.fetchAllError(params.orgId, error);
  38. doCallback(options, 'error', error);
  39. },
  40. });
  41. }
  42. // Fetch user teams for current org and place them in the team store
  43. export async function fetchUserTeams(api: Client, params: OrgSlug) {
  44. const teams = await api.requestPromise(`/organizations/${params.orgId}/user-teams/`);
  45. TeamActions.loadUserTeams(teams);
  46. }
  47. export function fetchTeamDetails(
  48. api: Client,
  49. params: OrgAndTeamSlug,
  50. options?: CallbackOptions
  51. ) {
  52. TeamActions.fetchDetails(params.teamId);
  53. return api.request(`/teams/${params.orgId}/${params.teamId}/`, {
  54. success: data => {
  55. TeamActions.fetchDetailsSuccess(params.teamId, data);
  56. doCallback(options, 'success', data);
  57. },
  58. error: error => {
  59. TeamActions.fetchDetailsError(params.teamId, error);
  60. doCallback(options, 'error', error);
  61. },
  62. });
  63. }
  64. export function updateTeamSuccess(teamId: OrgAndTeamSlug['teamId'], data: Team) {
  65. TeamActions.updateSuccess(teamId, data);
  66. }
  67. export function joinTeam(
  68. api: Client,
  69. params: OrgAndTeamSlug & Partial<MemberId>,
  70. options: CallbackOptions
  71. ) {
  72. const endpoint = `/organizations/${params.orgId}/members/${
  73. params.memberId ?? 'me'
  74. }/teams/${params.teamId}/`;
  75. const id = uniqueId();
  76. TeamActions.update(id, params.teamId);
  77. return api.request(endpoint, {
  78. method: 'POST',
  79. success: data => {
  80. TeamActions.updateSuccess(params.teamId, data);
  81. doCallback(options, 'success', data);
  82. },
  83. error: error => {
  84. TeamActions.updateError(id, params.teamId, error);
  85. doCallback(options, 'error', error);
  86. },
  87. });
  88. }
  89. export function leaveTeam(
  90. api: Client,
  91. params: OrgAndTeamSlug & Partial<MemberId>,
  92. options: CallbackOptions
  93. ) {
  94. const endpoint = `/organizations/${params.orgId}/members/${
  95. params.memberId || 'me'
  96. }/teams/${params.teamId}/`;
  97. const id = uniqueId();
  98. TeamActions.update(id, params.teamId);
  99. return api.request(endpoint, {
  100. method: 'DELETE',
  101. success: data => {
  102. TeamActions.updateSuccess(params.teamId, data);
  103. doCallback(options, 'success', data);
  104. },
  105. error: error => {
  106. TeamActions.updateError(id, params.teamId, error);
  107. doCallback(options, 'error', error);
  108. },
  109. });
  110. }
  111. export function createTeam(api: Client, team: Pick<Team, 'slug'>, params: OrgSlug) {
  112. TeamActions.createTeam(team);
  113. return api
  114. .requestPromise(`/organizations/${params.orgId}/teams/`, {
  115. method: 'POST',
  116. data: team,
  117. })
  118. .then(
  119. data => {
  120. TeamActions.createTeamSuccess(data);
  121. addSuccessMessage(
  122. tct('[team] has been added to the [organization] organization', {
  123. team: `#${data.slug}`,
  124. organization: params.orgId,
  125. })
  126. );
  127. return data;
  128. },
  129. err => {
  130. TeamActions.createTeamError(team.slug, err);
  131. addErrorMessage(
  132. tct('Unable to create [team] in the [organization] organization', {
  133. team: `#${team.slug}`,
  134. organization: params.orgId,
  135. })
  136. );
  137. throw err;
  138. }
  139. );
  140. }
  141. export function removeTeam(api: Client, params: OrgAndTeamSlug) {
  142. TeamActions.removeTeam(params.teamId);
  143. return api
  144. .requestPromise(`/teams/${params.orgId}/${params.teamId}/`, {
  145. method: 'DELETE',
  146. })
  147. .then(
  148. data => {
  149. TeamActions.removeTeamSuccess(params.teamId, data);
  150. addSuccessMessage(
  151. tct('[team] has been removed from the [organization] organization', {
  152. team: `#${params.teamId}`,
  153. organization: params.orgId,
  154. })
  155. );
  156. return data;
  157. },
  158. err => {
  159. TeamActions.removeTeamError(params.teamId, err);
  160. addErrorMessage(
  161. tct('Unable to remove [team] from the [organization] organization', {
  162. team: `#${params.teamId}`,
  163. organization: params.orgId,
  164. })
  165. );
  166. throw err;
  167. }
  168. );
  169. }