ChangePassword.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import React, { PureComponent, SyntheticEvent, ChangeEvent } from 'react';
  2. import { Tooltip } from '@grafana/ui';
  3. import appEvents from 'app/core/app_events';
  4. interface Props {
  5. onSubmit: (pw: string) => void;
  6. onSkip: Function;
  7. focus?: boolean;
  8. }
  9. interface State {
  10. newPassword: string;
  11. confirmNew: string;
  12. valid: boolean;
  13. }
  14. export class ChangePassword extends PureComponent<Props, State> {
  15. private userInput: HTMLInputElement;
  16. constructor(props: Props) {
  17. super(props);
  18. this.state = {
  19. newPassword: '',
  20. confirmNew: '',
  21. valid: false,
  22. };
  23. }
  24. componentDidUpdate(prevProps: Props) {
  25. if (!prevProps.focus && this.props.focus) {
  26. this.focus();
  27. }
  28. }
  29. focus() {
  30. this.userInput.focus();
  31. }
  32. onSubmit = (e: SyntheticEvent) => {
  33. e.preventDefault();
  34. const { newPassword, valid } = this.state;
  35. if (valid) {
  36. this.props.onSubmit(newPassword);
  37. } else {
  38. appEvents.emit('alert-warning', ['New passwords do not match', '']);
  39. }
  40. };
  41. onNewPasswordChange = (e: ChangeEvent<HTMLInputElement>) => {
  42. this.setState({
  43. newPassword: e.target.value,
  44. valid: this.validate('newPassword', e.target.value),
  45. });
  46. };
  47. onConfirmPasswordChange = (e: ChangeEvent<HTMLInputElement>) => {
  48. this.setState({
  49. confirmNew: e.target.value,
  50. valid: this.validate('confirmNew', e.target.value),
  51. });
  52. };
  53. onSkip = (e: SyntheticEvent) => {
  54. this.props.onSkip();
  55. };
  56. validate(changed: string, pw: string) {
  57. if (changed === 'newPassword') {
  58. return this.state.confirmNew === pw;
  59. } else if (changed === 'confirmNew') {
  60. return this.state.newPassword === pw;
  61. }
  62. return false;
  63. }
  64. render() {
  65. return (
  66. <div className="login-inner-box" id="change-password-view">
  67. <div className="text-left login-change-password-info">
  68. <h5>Change Password</h5>
  69. Before you can get started with awesome dashboards we need you to make your account more secure by changing
  70. your password.
  71. <br />
  72. You can change your password again later.
  73. </div>
  74. <form className="login-form-group gf-form-group">
  75. <div className="login-form">
  76. <input
  77. type="password"
  78. id="newPassword"
  79. name="newPassword"
  80. className="gf-form-input login-form-input"
  81. required
  82. placeholder="New password"
  83. onChange={this.onNewPasswordChange}
  84. ref={input => {
  85. this.userInput = input;
  86. }}
  87. />
  88. </div>
  89. <div className="login-form">
  90. <input
  91. type="password"
  92. name="confirmNew"
  93. className="gf-form-input login-form-input"
  94. required
  95. ng-model="command.confirmNew"
  96. placeholder="Confirm new password"
  97. onChange={this.onConfirmPasswordChange}
  98. />
  99. </div>
  100. <div className="login-button-group login-button-group--right text-right">
  101. <Tooltip
  102. placement="bottom"
  103. content="If you skip you will be prompted to change password next time you login."
  104. >
  105. <a className="btn btn-link" onClick={this.onSkip}>
  106. Skip
  107. </a>
  108. </Tooltip>
  109. <button
  110. type="submit"
  111. className={`btn btn-large p-x-2 ${this.state.valid ? 'btn-primary' : 'btn-inverse'}`}
  112. onClick={this.onSubmit}
  113. disabled={!this.state.valid}
  114. >
  115. Save
  116. </button>
  117. </div>
  118. </form>
  119. </div>
  120. );
  121. }
  122. }