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 ConfigStore from 'sentry/stores/configStore';
  12. type OnSubmitSuccess = Parameters<NonNullable<FormProps['onSubmitSuccess']>>;
  13. function PasswordForm() {
  14. function handleSubmitSuccess(_change: OnSubmitSuccess[0], model: OnSubmitSuccess[1]) {
  15. // Reset form on success
  16. model.resetForm();
  17. addSuccessMessage('Password has been changed');
  18. }
  19. function handleSubmitError() {
  20. addErrorMessage('Error changing password');
  21. }
  22. const user = ConfigStore.get('user');
  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;