request-activation-link.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import React from 'react'; // jshint ignore:line
  2. import misago from 'misago/index';
  3. import Button from 'misago/components/button'; // jshint ignore:line
  4. import Form from 'misago/components/form';
  5. import ajax from 'misago/services/ajax';
  6. import snackbar from 'misago/services/snackbar';
  7. import * as validators from 'misago/utils/validators';
  8. import showBannedPage from 'misago/utils/banned-page';
  9. export class RequestLinkForm extends Form {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. 'isLoading': false,
  14. 'email': '',
  15. 'validators': {
  16. 'email': [
  17. validators.email()
  18. ]
  19. }
  20. };
  21. }
  22. clean() {
  23. if (this.isValid()) {
  24. return true;
  25. } else {
  26. snackbar.error(gettext("Enter a valid email address."));
  27. return false;
  28. }
  29. }
  30. send() {
  31. return ajax.post(misago.get('SEND_ACTIVATION_API'), {
  32. 'email': this.state.email
  33. });
  34. }
  35. handleSuccess(apiResponse) {
  36. this.props.callback(apiResponse);
  37. }
  38. handleError(rejection) {
  39. if (['already_active', 'inactive_admin'].indexOf(rejection.code) > -1) {
  40. snackbar.info(rejection.detail);
  41. } else if (rejection.status === 403 && rejection.ban) {
  42. showBannedPage(rejection.ban);
  43. } else {
  44. snackbar.apiError(rejection);
  45. }
  46. }
  47. render() {
  48. /* jshint ignore:start */
  49. return <div className="well well-form well-form-request-activation-link">
  50. <form onSubmit={this.handleSubmit}>
  51. <div className="form-group">
  52. <div className="control-input">
  53. <input type="text" className="form-control"
  54. placeholder={gettext("Your e-mail address")}
  55. disabled={this.state.isLoading}
  56. onChange={this.bindInput('email')}
  57. value={this.state.email} />
  58. </div>
  59. </div>
  60. <Button className="btn-primary btn-block"
  61. loading={this.state.isLoading}>
  62. {gettext("Send link")}
  63. </Button>
  64. </form>
  65. </div>;
  66. /* jshint ignore:end */
  67. }
  68. }
  69. export class LinkSent extends React.Component {
  70. getMessage() {
  71. return interpolate(gettext("Activation link was sent to %(email)s"), {
  72. email: this.props.user.email
  73. }, true);
  74. }
  75. render() {
  76. /* jshint ignore:start */
  77. return <div className="well well-form well-form-request-activation-link well-done">
  78. <div className="done-message">
  79. <div className="message-icon">
  80. <span className="material-icon">
  81. check
  82. </span>
  83. </div>
  84. <div className="message-body">
  85. <p>
  86. {this.getMessage()}
  87. </p>
  88. </div>
  89. <button type="button" className="btn btn-primary btn-block"
  90. onClick={this.props.callback}>
  91. {gettext("Request another link")}
  92. </button>
  93. </div>
  94. </div>;
  95. /* jshint ignore:end */
  96. }
  97. }
  98. export default class extends React.Component {
  99. constructor(props) {
  100. super(props);
  101. this.state = {
  102. complete: false
  103. };
  104. }
  105. /* jshint ignore:start */
  106. complete = (apiResponse) => {
  107. this.setState({
  108. complete: apiResponse
  109. });
  110. };
  111. reset = () => {
  112. this.setState({
  113. complete: false
  114. });
  115. };
  116. /* jshint ignore:end */
  117. render() {
  118. /* jshint ignore:start */
  119. if (this.state.complete) {
  120. return <LinkSent user={this.state.complete} callback={this.reset} />;
  121. } else {
  122. return <RequestLinkForm callback={this.complete} />;
  123. };
  124. /* jshint ignore:end */
  125. }
  126. }