teams.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. type TeamData = {data: Team};
  25. /**
  26. * This is the actual internal id, not username or email
  27. */
  28. type MemberId = {memberId: string};
  29. // Fetch teams for org
  30. export function fetchTeams(api: Client, params: OrgSlug, options: CallbackOptions) {
  31. TeamActions.fetchAll(params.orgId);
  32. return api.request(`/teams/${params.orgId}/`, {
  33. success: data => {
  34. TeamActions.fetchAllSuccess(params.orgId, data);
  35. doCallback(options, 'success', data);
  36. },
  37. error: error => {
  38. TeamActions.fetchAllError(params.orgId, error);
  39. doCallback(options, 'error', error);
  40. },
  41. });
  42. }
  43. // Fetch user teams for current org and place them in the team store
  44. export async function fetchUserTeams(api: Client, params: OrgSlug) {
  45. const teams = await api.requestPromise(`/organizations/${params.orgId}/user-teams/`);
  46. TeamActions.loadUserTeams(teams);
  47. }
  48. export function fetchTeamDetails(
  49. api: Client,
  50. params: OrgAndTeamSlug,
  51. options?: CallbackOptions
  52. ) {
  53. TeamActions.fetchDetails(params.teamId);
  54. return api.request(`/teams/${params.orgId}/${params.teamId}/`, {
  55. success: data => {
  56. TeamActions.fetchDetailsSuccess(params.teamId, data);
  57. doCallback(options, 'success', data);
  58. },
  59. error: error => {
  60. TeamActions.fetchDetailsError(params.teamId, error);
  61. doCallback(options, 'error', error);
  62. },
  63. });
  64. }
  65. export function updateTeamSuccess(teamId: OrgAndTeamSlug['teamId'], data: Team) {
  66. TeamActions.updateSuccess(teamId, data);
  67. }
  68. export function updateTeam(
  69. api: Client,
  70. params: OrgAndTeamSlug & TeamData,
  71. options: CallbackOptions
  72. ) {
  73. const endpoint = `/teams/${params.orgId}/${params.teamId}/`;
  74. TeamActions.update(params.teamId, params.data);
  75. return api.request(endpoint, {
  76. method: 'PUT',
  77. data: params.data,
  78. success: data => {
  79. updateTeamSuccess(params.teamId, data);
  80. doCallback(options, 'success', data);
  81. },
  82. error: error => {
  83. TeamActions.updateError(params.teamId, error);
  84. doCallback(options, 'error', error);
  85. },
  86. });
  87. }
  88. export function joinTeam(
  89. api: Client,
  90. params: OrgAndTeamSlug & Partial<MemberId>,
  91. options: CallbackOptions
  92. ) {
  93. const endpoint = `/organizations/${params.orgId}/members/${
  94. params.memberId ?? 'me'
  95. }/teams/${params.teamId}/`;
  96. const id = uniqueId();
  97. TeamActions.update(id, params.teamId);
  98. return api.request(endpoint, {
  99. method: 'POST',
  100. success: data => {
  101. TeamActions.updateSuccess(params.teamId, data);
  102. doCallback(options, 'success', data);
  103. },
  104. error: error => {
  105. TeamActions.updateError(id, params.teamId, error);
  106. doCallback(options, 'error', error);
  107. },
  108. });
  109. }
  110. export function leaveTeam(
  111. api: Client,
  112. params: OrgAndTeamSlug & Partial<MemberId>,
  113. options: CallbackOptions
  114. ) {
  115. const endpoint = `/organizations/${params.orgId}/members/${
  116. params.memberId || 'me'
  117. }/teams/${params.teamId}/`;
  118. const id = uniqueId();
  119. TeamActions.update(id, params.teamId);
  120. return api.request(endpoint, {
  121. method: 'DELETE',
  122. success: data => {
  123. TeamActions.updateSuccess(params.teamId, data);
  124. doCallback(options, 'success', data);
  125. },
  126. error: error => {
  127. TeamActions.updateError(id, params.teamId, error);
  128. doCallback(options, 'error', error);
  129. },
  130. });
  131. }
  132. export function createTeam(api: Client, team: Pick<Team, 'slug'>, params: OrgSlug) {
  133. TeamActions.createTeam(team);
  134. return api
  135. .requestPromise(`/organizations/${params.orgId}/teams/`, {
  136. method: 'POST',
  137. data: team,
  138. })
  139. .then(
  140. data => {
  141. TeamActions.createTeamSuccess(data);
  142. addSuccessMessage(
  143. tct('[team] has been added to the [organization] organization', {
  144. team: `#${data.slug}`,
  145. organization: params.orgId,
  146. })
  147. );
  148. return data;
  149. },
  150. err => {
  151. TeamActions.createTeamError(team.slug, err);
  152. addErrorMessage(
  153. tct('Unable to create [team] in the [organization] organization', {
  154. team: `#${team.slug}`,
  155. organization: params.orgId,
  156. })
  157. );
  158. throw err;
  159. }
  160. );
  161. }
  162. export function removeTeam(api: Client, params: OrgAndTeamSlug) {
  163. TeamActions.removeTeam(params.teamId);
  164. return api
  165. .requestPromise(`/teams/${params.orgId}/${params.teamId}/`, {
  166. method: 'DELETE',
  167. })
  168. .then(
  169. data => {
  170. TeamActions.removeTeamSuccess(params.teamId, data);
  171. addSuccessMessage(
  172. tct('[team] has been removed from the [organization] organization', {
  173. team: `#${params.teamId}`,
  174. organization: params.orgId,
  175. })
  176. );
  177. return data;
  178. },
  179. err => {
  180. TeamActions.removeTeamError(params.teamId, err);
  181. addErrorMessage(
  182. tct('Unable to remove [team] from the [organization] organization', {
  183. team: `#${params.teamId}`,
  184. organization: params.orgId,
  185. })
  186. );
  187. throw err;
  188. }
  189. );
  190. }