You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package react.user;
  2. import react.ReactComponent;
  3. import react.ReactMacro.jsx;
  4. import Common;
  5. typedef LoginBoxProps = {
  6. redirectUrl:String,
  7. message:String,
  8. ?phoneRequired:Bool
  9. }
  10. typedef LoginBoxState = {
  11. email:String,
  12. password:String,
  13. error:String
  14. }
  15. /**
  16. * Login Box
  17. * @author fbarbut
  18. */
  19. class LoginBox extends react.ReactComponentOfPropsAndState<LoginBoxProps, LoginBoxState> {
  20. public function new(props:LoginBoxProps) {
  21. if (props.redirectUrl == null)
  22. props.redirectUrl = "/";
  23. if (props.message == "")
  24. props.message = null;
  25. super(props);
  26. this.state = {email: "", password: "", error: null};
  27. }
  28. function setError(err:String) {
  29. this.setState(cast {error: err});
  30. }
  31. override public function render() {
  32. return jsx('<div onKeyPress=$onKeyPress>
  33. <$Error error="${state.error}" />
  34. <$Message message="${props.message}" />
  35. <form action="" method="post" className="form-horizontal">
  36. <div className="form-group">
  37. <label htmlFor="email" className="col-sm-4 control-label">Email : </label>
  38. <div className="col-sm-8">
  39. <input id="email" className="form-control" type="text" name="email" value="${state.email}" required="1" onChange={onChange}/>
  40. </div>
  41. </div>
  42. <div className="form-group">
  43. <label htmlFor="password" className="col-sm-4 control-label">Mot de passe : </label>
  44. <div className="col-sm-8">
  45. <input id="password" type="password" name="password" value="${state.password}" className="form-control" required="1" onChange={onChange}/>
  46. </div>
  47. </div>
  48. <p className="text-center">
  49. <a onClick={submit} className="btn btn-primary btn-lg" ><span className="glyphicon glyphicon-user"></span> S\'identifier</a>
  50. <br/>
  51. <br/>
  52. <a href="/user/forgottenPassword">Mot de passe oublié ?</a>
  53. </p>
  54. </form>
  55. <!--
  56. <hr/>
  57. <p className="text-center">
  58. <b>C\'est votre première visite sur CagettePéi ?</b>&nbsp;&nbsp;
  59. <a onClick={registerBox} className="btn btn-default"><span className="glyphicon glyphicon-chevron-right"></span> S\'inscrire</a>
  60. </p>
  61. -->
  62. </div>');
  63. }
  64. /**
  65. * @doc https://facebook.github.io/react/docs/forms.html
  66. */
  67. function onChange(e:js.html.Event) {
  68. e.preventDefault();
  69. var name:String = untyped e.target.name;
  70. var value:String = untyped /*(e.target.value == "") ? null :*/ e.target.value;
  71. Reflect.setField(state, name, value);
  72. this.setState(this.state);
  73. }
  74. /**
  75. * displays a registerBox
  76. */
  77. public function registerBox() {
  78. var body = js.Browser.document.querySelector('#myModal .modal-body');
  79. ReactDOM.unmountComponentAtNode(body);
  80. js.Browser.document.querySelector("#myModal .modal-title").innerHTML = "Inscription";
  81. ReactDOM.render(jsx('<$RegisterBox redirectUrl="${props.redirectUrl}" phoneRequired="${props.phoneRequired}"/>'), body);
  82. }
  83. public function submit(?e:js.html.Event) {
  84. if (state.email == "") {
  85. setError("Veuillez saisir votre email");
  86. return;
  87. }
  88. if (state.password == "") {
  89. setError("Veuillez saisir votre mot de passe");
  90. return;
  91. }
  92. // lock button
  93. var el:js.html.Element = null;
  94. if (e != null) {
  95. el = cast e.target;
  96. el.classList.add("disabled");
  97. }
  98. var req = new haxe.Http("/api/user/login");
  99. req.addParameter("email", state.email);
  100. req.addParameter("password", state.password);
  101. req.addParameter("redirecturl", props.redirectUrl);
  102. req.onData = req.onError = function(d) {
  103. var d = req.responseData;
  104. if (e != null)
  105. el.classList.remove("disabled");
  106. var d = haxe.Json.parse(d);
  107. if (Reflect.hasField(d, "error"))
  108. setError(d.error.message);
  109. if (Reflect.hasField(d, "success"))
  110. js.Browser.window.location.href = props.redirectUrl;
  111. }
  112. req.request(true);
  113. }
  114. function onKeyPress(e:js.html.KeyboardEvent) {
  115. if (e.key == "Enter")
  116. submit();
  117. }
  118. }