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.

164 lines
5.0 KiB

  1. package react.user;
  2. import react.ReactDOM;
  3. import react.ReactComponent;
  4. import react.ReactMacro.jsx;
  5. typedef RegisterBoxState = {firstName:String, lastName:String, email:String, password:String, error:String, phone:String};
  6. typedef RegisterBoxProps = {redirectUrl:String,message:String,phoneRequired:Bool};
  7. /**
  8. * Registration box ( sign up )
  9. * @author fbarbut
  10. */
  11. class RegisterBox extends react.ReactComponentOfPropsAndState<RegisterBoxProps,RegisterBoxState>
  12. {
  13. public function new(props:RegisterBoxProps)
  14. {
  15. if (props.redirectUrl == null) props.redirectUrl = "/";
  16. super(props);
  17. this.state = {firstName:"",lastName:"",email:"",password:"",error:null,phone:""};
  18. }
  19. override public function render(){
  20. //tips for conditionnal rendering : https://github.com/massiveinteractive/haxe-react#gotchas
  21. var phone = null;
  22. if (props.phoneRequired){
  23. phone = jsx('<div className="form-group">
  24. <label htmlFor="phone" className="col-sm-4 control-label">Téléphone : </label>
  25. <div className="col-sm-8">
  26. <input id="phone" type="text" className="form-control" name="phone" value="${state.phone}" onChange={onChange} />
  27. </div>
  28. </div>');
  29. }
  30. return jsx('
  31. <div>
  32. <$Error error="${state.error}" />
  33. <$Message message="${props.message}" />
  34. <form action="" method="post" className="form-horizontal">
  35. <div className="form-group">
  36. <label htmlFor="firstName" className="col-sm-4 control-label">Prénom : </label>
  37. <div className="col-sm-8">
  38. <input id="firstName" type="text" name="firstName" value="${state.firstName}" className="form-control" onChange={onChange}/>
  39. </div>
  40. </div>
  41. <div className="form-group">
  42. <label htmlFor="lastName" className="col-sm-4 control-label">Nom : </label>
  43. <div className="col-sm-8">
  44. <input id="lastName" type="text" name="lastName" value="${state.lastName}" className="form-control" onChange={onChange}/>
  45. </div>
  46. </div>
  47. <div className="form-group">
  48. <label htmlFor="email" className="col-sm-4 control-label">Email : </label>
  49. <div className="col-sm-8">
  50. <input id="email" type="text" className="form-control" name="email" value="${state.email}" onChange={onChange} />
  51. </div>
  52. </div>
  53. ${phone}
  54. <div className="form-group">
  55. <label htmlFor="password" className="col-sm-4 control-label">Mot de passe : </label>
  56. <div className="col-sm-8">
  57. <input id="password" type="password" name="password" value="${state.password}" className="form-control" onChange={onChange}/>
  58. </div>
  59. </div>
  60. <p className="text-center">
  61. <a onClick={submit} className="btn btn-primary btn-lg" ><span className="glyphicon glyphicon-chevron-right"></span> Inscription</a>
  62. </p>
  63. </form>
  64. <hr/>
  65. <p className="text-center">
  66. <b>Déjà inscrit ? </b>
  67. <a onClick={loginBox} className="btn btn-default"><span className="glyphicon glyphicon-user"></span> Connectez-vous ici</a>
  68. </p>
  69. </div>
  70. ');
  71. }
  72. /**
  73. * @doc https://facebook.github.io/react/docs/forms.html
  74. */
  75. function onChange(e:js.html.Event){
  76. e.preventDefault();
  77. var name :String = untyped e.target.name;
  78. var value :String = untyped e.target.value;
  79. //trace('onChange : $name = $value');
  80. Reflect.setField(state, name, value);
  81. this.setState(this.state);
  82. }
  83. /**
  84. * displays a login box
  85. */
  86. public function loginBox(){
  87. var body = js.Browser.document.querySelector('#myModal .modal-body');
  88. ReactDOM.unmountComponentAtNode( body );
  89. js.Browser.document.querySelector("#myModal .modal-title").innerHTML = "Connexion";
  90. ReactDOM.render(jsx('<$LoginBox redirectUrl="${props.redirectUrl}" />'), body );
  91. }
  92. public function submit(e:js.html.Event ){
  93. if (state.email == ""){
  94. setError("Veuillez saisir votre email");
  95. return;
  96. }
  97. if (state.password == ""){
  98. setError("Veuillez saisir un mot de passe");
  99. return;
  100. }
  101. if (state.firstName == ""){
  102. setError("Veuillez saisir votre prénom");
  103. return;
  104. }
  105. if (state.lastName == ""){
  106. setError("Veuillez saisir votre nom de famille");
  107. return;
  108. }
  109. if (state.phone == "" && props.phoneRequired){
  110. setError("Veuillez saisir votre numéro de téléphone");
  111. return;
  112. }
  113. //lock button
  114. var el : js.html.Element = cast e.target;
  115. el.classList.add("disabled");
  116. var req = new haxe.Http("/api/user/register");
  117. req.addParameter("firstName", state.firstName);
  118. req.addParameter("lastName", state.lastName);
  119. req.addParameter("email", state.email);
  120. req.addParameter("password", state.password);
  121. req.addParameter("redirecturl", props.redirectUrl);
  122. if(props.phoneRequired) req.addParameter("phone", state.phone);
  123. req.onData = req.onError = function(d){
  124. var d = req.responseData;
  125. el.classList.remove("disabled");
  126. var d = haxe.Json.parse(d);
  127. if (Reflect.hasField(d, "error")) setError(d.error.message);
  128. if (Reflect.hasField(d, "success")) js.Browser.window.location.href = props.redirectUrl;
  129. }
  130. req.request(true);
  131. }
  132. function setError(err:String){
  133. this.setState(cast {error:err});
  134. }
  135. }