teams.tsx 3.9 KB

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