superuserAccessForm.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import Alert from 'sentry/components/alert';
  4. import Form from 'sentry/components/forms/form';
  5. import Hook from 'sentry/components/hook';
  6. import ThemeAndStyleProvider from 'sentry/components/themeAndStyleProvider';
  7. import {ErrorCodes} from 'sentry/constants/superuserAccessErrors';
  8. import {t} from 'sentry/locale';
  9. type State = {
  10. error: boolean;
  11. errorType: string;
  12. };
  13. class SuperuserAccessForm extends Component<State> {
  14. state: State = {
  15. error: false,
  16. errorType: '',
  17. };
  18. handleSuccess = () => {
  19. window.location.reload();
  20. };
  21. handleError = err => {
  22. let errorType = '';
  23. if (err.status === 403) {
  24. errorType = ErrorCodes.invalidPassword;
  25. } else if (err.status === 401) {
  26. errorType = ErrorCodes.invalidSSOSession;
  27. } else if (err.status === 400) {
  28. errorType = ErrorCodes.invalidAccessCategory;
  29. } else {
  30. errorType = ErrorCodes.unknownError;
  31. }
  32. this.setState({
  33. error: true,
  34. errorType,
  35. });
  36. };
  37. render() {
  38. const {error, errorType} = this.state;
  39. return (
  40. <ThemeAndStyleProvider>
  41. <Form
  42. apiMethod="PUT"
  43. apiEndpoint="/auth/"
  44. submitLabel={t('Continue')}
  45. onSubmitSuccess={this.handleSuccess}
  46. onSubmitError={this.handleError}
  47. initialData={{isSuperuserModal: true}}
  48. resetOnError
  49. >
  50. {error && (
  51. <StyledAlert type="error" showIcon>
  52. {t(errorType)}
  53. </StyledAlert>
  54. )}
  55. <Hook name="component:superuser-access-category" />
  56. </Form>
  57. </ThemeAndStyleProvider>
  58. );
  59. }
  60. }
  61. const StyledAlert = styled(Alert)`
  62. margin-bottom: 0;
  63. `;
  64. export default SuperuserAccessForm;