register-button.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import React from "react"
  2. import Loader from "misago/components/loader"
  3. import RegisterForm from "misago/components/register.js"
  4. import ajax from "misago/services/ajax"
  5. import captcha from "misago/services/captcha"
  6. import modal from "misago/services/modal"
  7. import snackbar from "misago/services/snackbar"
  8. export default class extends React.Component {
  9. constructor(props) {
  10. super(props)
  11. this.state = {
  12. isLoading: false,
  13. isLoaded: false,
  14. criteria: null
  15. }
  16. }
  17. showRegisterForm = () => {
  18. if (misago.get("SETTINGS").account_activation === "closed") {
  19. snackbar.info(gettext("New registrations are currently disabled."))
  20. } else if (this.state.isLoaded) {
  21. modal.show(<RegisterForm criteria={this.state.criteria} />)
  22. } else {
  23. this.setState({ isLoading: true })
  24. Promise.all([
  25. captcha.load(),
  26. ajax.get(misago.get("AUTH_CRITERIA_API"))
  27. ]).then(
  28. result => {
  29. this.setState({
  30. isLoading: false,
  31. isLoaded: true,
  32. criteria: result[1]
  33. })
  34. modal.show(<RegisterForm criteria={result[1]} />)
  35. },
  36. () => {
  37. this.setState({ isLoading: false })
  38. snackbar.error(
  39. gettext("Registration is currently unavailable due to an error.")
  40. )
  41. }
  42. )
  43. }
  44. }
  45. getClassName() {
  46. return this.props.className + (this.state.isLoading ? " btn-loading" : "")
  47. }
  48. render() {
  49. return (
  50. <button
  51. className={"btn " + this.getClassName()}
  52. disabled={this.state.isLoading}
  53. onClick={this.showRegisterForm}
  54. type="button"
  55. >
  56. {gettext("Register")}
  57. {this.state.isLoading ? <Loader /> : null}
  58. </button>
  59. )
  60. }
  61. }