RequestAccount.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import React from 'react';
  2. class RequestAccount extends React.Component {
  3. constructor(props) {
  4. super(props);
  5. this.state = {
  6. creating: false,
  7. passwordInputType: 'text',
  8. pw: '',
  9. pwRepeat: '',
  10. showPassword: false,
  11. showRepeat: false
  12. };
  13. }
  14. resetForm() {
  15. this.setState({
  16. pw: '',
  17. pwRepeat: '',
  18. showRepeat: false,
  19. creating: false
  20. });
  21. }
  22. handleCancel(e) {
  23. e.preventDefault();
  24. ipc.send('backendAction_closePopupWindow');
  25. }
  26. handleSubmit(e) {
  27. e.preventDefault();
  28. const { pw, pwRepeat } = this.state;
  29. // ask for password repeat
  30. if (!pwRepeat.length) {
  31. this.setState({ showRepeat: true });
  32. this.refs.password_repeat.focus();
  33. return;
  34. }
  35. // check passwords
  36. if (pw !== pwRepeat) {
  37. GlobalNotification.warning({
  38. content: TAPi18n.__(
  39. 'mist.popupWindows.requestAccount.errors.passwordMismatch'
  40. ),
  41. duration: 3
  42. });
  43. this.resetForm();
  44. } else if (pw && pw.length < 8) {
  45. GlobalNotification.warning({
  46. content: TAPi18n.__(
  47. 'mist.popupWindows.requestAccount.errors.passwordTooShort'
  48. ),
  49. duration: 3
  50. });
  51. this.resetForm();
  52. } else if (pw && pw.length >= 8) {
  53. this.setState({ creating: true }, () => this.createAccount(pwRepeat));
  54. }
  55. }
  56. createAccount(pw) {
  57. web3.eth.personal.newAccount(pw).then(address => {
  58. ipc.send('backendAction_windowMessageToOwner', null, address);
  59. // notify about backing up!
  60. alert(TAPi18n.__('mist.popupWindows.requestAccount.backupHint'));
  61. this.resetForm();
  62. ipc.send('backendAction_closePopupWindow');
  63. });
  64. }
  65. renderFormBody() {
  66. if (this.state.creating) {
  67. return <h2>{i18n.t('mist.popupWindows.requestAccount.creating')}</h2>;
  68. } else {
  69. return (
  70. <div>
  71. <div
  72. className={`field-container ${
  73. this.state.showRepeat ? 'repeat-field' : ''
  74. }`}
  75. >
  76. <input
  77. autoFocus
  78. type={this.state.showPassword ? 'text' : 'password'}
  79. placeholder={i18n.t(
  80. 'mist.popupWindows.requestAccount.enterPassword'
  81. )}
  82. className="password"
  83. value={this.state.pw}
  84. onChange={e => this.setState({ pw: e.target.value })}
  85. />
  86. <input
  87. type={this.state.showPassword ? 'text' : 'password'}
  88. placeholder={i18n.t(
  89. 'mist.popupWindows.requestAccount.repeatPassword'
  90. )}
  91. className="password-repeat"
  92. ref="password_repeat"
  93. value={this.state.pwRepeat}
  94. onChange={e => this.setState({ pwRepeat: e.target.value })}
  95. />
  96. </div>
  97. <div className="show-password-container">
  98. <input
  99. id="show-password-checkbox"
  100. type="checkbox"
  101. name="elements_input_bool"
  102. className="show-password abi-input"
  103. checked={this.state.showPassword}
  104. onChange={() =>
  105. this.setState({ showPassword: !this.state.showPassword })
  106. }
  107. />
  108. <label htmlFor="show-password-checkbox">
  109. {i18n.t('mist.popupWindows.importAccount.buttons.showPassword')}
  110. </label>
  111. </div>
  112. <div className="dapp-modal-buttons">
  113. <button
  114. className="cancel"
  115. type="button"
  116. onClick={e => this.handleCancel(e)}
  117. >
  118. {i18n.t('buttons.cancel')}
  119. </button>
  120. <button className="ok dapp-primary-button" type="submit">
  121. {i18n.t('buttons.ok')}
  122. </button>
  123. </div>
  124. </div>
  125. );
  126. }
  127. }
  128. render() {
  129. return (
  130. <div className="popup-windows request-account">
  131. <form onSubmit={e => this.handleSubmit(e)}>
  132. <h1>{i18n.t('mist.popupWindows.requestAccount.title')}</h1>
  133. {this.renderFormBody()}
  134. </form>
  135. </div>
  136. );
  137. }
  138. }
  139. export default RequestAccount;