passwordForm.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import styled from '@emotion/styled';
  2. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  3. import {Button} from 'sentry/components/button';
  4. import type {FormProps} from 'sentry/components/forms/form';
  5. import Form from 'sentry/components/forms/form';
  6. import JsonForm from 'sentry/components/forms/jsonForm';
  7. import PanelAlert from 'sentry/components/panels/panelAlert';
  8. import PanelItem from 'sentry/components/panels/panelItem';
  9. import accountPasswordFields from 'sentry/data/forms/accountPassword';
  10. import {t} from 'sentry/locale';
  11. import {useUser} from 'sentry/utils/useUser';
  12. type OnSubmitSuccess = Parameters<NonNullable<FormProps['onSubmitSuccess']>>;
  13. function PasswordForm() {
  14. const user = useUser();
  15. function handleSubmitSuccess(_change: OnSubmitSuccess[0], model: OnSubmitSuccess[1]) {
  16. // Reset form on success
  17. model.resetForm();
  18. addSuccessMessage('Password has been changed');
  19. }
  20. function handleSubmitError() {
  21. addErrorMessage('Error changing password');
  22. }
  23. return (
  24. <Form
  25. apiMethod="PUT"
  26. apiEndpoint="/users/me/password/"
  27. initialData={{}}
  28. onSubmitSuccess={handleSubmitSuccess}
  29. onSubmitError={handleSubmitError}
  30. hideFooter
  31. >
  32. <JsonForm
  33. forms={accountPasswordFields}
  34. additionalFieldProps={{user}}
  35. renderFooter={() => (
  36. <Actions>
  37. <Button type="submit" priority="primary">
  38. {t('Change password')}
  39. </Button>
  40. </Actions>
  41. )}
  42. renderHeader={() => (
  43. <PanelAlert type="info">
  44. {t('Changing your password will invalidate all logged in sessions.')}
  45. </PanelAlert>
  46. )}
  47. />
  48. </Form>
  49. );
  50. }
  51. const Actions = styled(PanelItem)`
  52. justify-content: flex-end;
  53. `;
  54. export default PasswordForm;