passwordForm.tsx 1.8 KB

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