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.

138 lines
3.7 KiB

  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. {
  21. public function new(props:LoginBoxProps)
  22. {
  23. if (props.redirectUrl == null) props.redirectUrl = "/";
  24. if (props.message == "") 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 Cagette.net ?</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) el.classList.remove("disabled");
  105. var d = haxe.Json.parse(d);
  106. if (Reflect.hasField(d, "error")) setError(d.error.message);
  107. if (Reflect.hasField(d, "success")) js.Browser.window.location.href = props.redirectUrl;
  108. }
  109. req.request(true);
  110. }
  111. function onKeyPress(e:js.html.KeyboardEvent){
  112. if(e.key=="Enter") submit();
  113. }
  114. }