index.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import {Fragment} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import {browserHistory} from 'react-router';
  4. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  5. import {removeTeam, updateTeamSuccess} from 'sentry/actionCreators/teams';
  6. import {hasEveryAccess} from 'sentry/components/acl/access';
  7. import {Button} from 'sentry/components/button';
  8. import Confirm from 'sentry/components/confirm';
  9. import FieldGroup from 'sentry/components/forms/fieldGroup';
  10. import type {FormProps} from 'sentry/components/forms/form';
  11. import Form from 'sentry/components/forms/form';
  12. import JsonForm from 'sentry/components/forms/jsonForm';
  13. import Panel from 'sentry/components/panels/panel';
  14. import PanelHeader from 'sentry/components/panels/panelHeader';
  15. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  16. import teamSettingsFields from 'sentry/data/forms/teamSettingsFields';
  17. import {IconDelete} from 'sentry/icons';
  18. import {t, tct} from 'sentry/locale';
  19. import type {Team} from 'sentry/types';
  20. import useApi from 'sentry/utils/useApi';
  21. import useOrganization from 'sentry/utils/useOrganization';
  22. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  23. import PermissionAlert from 'sentry/views/settings/project/permissionAlert';
  24. interface TeamSettingsProps extends RouteComponentProps<{teamId: string}, {}> {
  25. team: Team;
  26. }
  27. function TeamSettings({team, params}: TeamSettingsProps) {
  28. const organization = useOrganization();
  29. const api = useApi();
  30. const handleSubmitSuccess: FormProps['onSubmitSuccess'] = (resp: Team, _model, id) => {
  31. // Use the old slug when triggering the update so we correctly replace the
  32. // previous team in the store
  33. updateTeamSuccess(team.slug, resp);
  34. if (id === 'slug') {
  35. addSuccessMessage(t('Team name changed'));
  36. browserHistory.replace(
  37. normalizeUrl(`/settings/${organization.slug}/teams/${resp.slug}/settings/`)
  38. );
  39. }
  40. };
  41. const handleRemoveTeam = async () => {
  42. try {
  43. await removeTeam(api, {orgId: organization.slug, teamId: params.teamId});
  44. browserHistory.replace(normalizeUrl(`/settings/${organization.slug}/teams/`));
  45. } catch {
  46. // removeTeam already displays an error message
  47. }
  48. };
  49. const hasTeamWrite = hasEveryAccess(['team:write'], {organization, team});
  50. const hasTeamAdmin = hasEveryAccess(['team:admin'], {organization, team});
  51. return (
  52. <Fragment>
  53. <SentryDocumentTitle title={t('Team Settings')} orgSlug={organization.slug} />
  54. <PermissionAlert access={['team:write']} team={team} />
  55. <Form
  56. apiMethod="PUT"
  57. apiEndpoint={`/teams/${organization.slug}/${team.slug}/`}
  58. saveOnBlur
  59. allowUndo
  60. onSubmitSuccess={handleSubmitSuccess}
  61. onSubmitError={() => addErrorMessage(t('Unable to save change'))}
  62. initialData={{
  63. name: team.name,
  64. slug: team.slug,
  65. }}
  66. >
  67. <JsonForm
  68. additionalFieldProps={{
  69. hasTeamWrite,
  70. }}
  71. forms={teamSettingsFields}
  72. />
  73. </Form>
  74. <Panel>
  75. <PanelHeader>{t('Team Administration')}</PanelHeader>
  76. <FieldGroup
  77. label={t('Remove Team')}
  78. help={t(
  79. "This may affect team members' access to projects and associated alert delivery."
  80. )}
  81. >
  82. <div>
  83. <Confirm
  84. disabled={!hasTeamAdmin}
  85. onConfirm={handleRemoveTeam}
  86. priority="danger"
  87. message={tct('Are you sure you want to remove the team [team]?', {
  88. team: `#${team.slug}`,
  89. })}
  90. >
  91. <Button
  92. icon={<IconDelete />}
  93. priority="danger"
  94. data-test-id="button-remove-team"
  95. >
  96. {t('Remove Team')}
  97. </Button>
  98. </Confirm>
  99. </div>
  100. </FieldGroup>
  101. </Panel>
  102. </Fragment>
  103. );
  104. }
  105. export default TeamSettings;