Advertisement
Guest User

Untitled

a guest
Mar 13th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. App.Users.Add = React.createClass({
  2. getInitialState: function () {
  3. return {username: "", email: "", password: "", loading: false, errors: {}}
  4. },
  5. _create: function () {
  6. return $.ajax({
  7. url: '/api/users',
  8. type: 'POST',
  9. data: {
  10. username: this.state.username,
  11. password: this.state.password,
  12. email: this.state.email
  13. },
  14. beforeSend: function () {
  15. this.setState({loading: true});
  16. }.bind(this)
  17. })
  18. },
  19. _onSubmit: function (e) {
  20. e.preventDefault();
  21. var errors = this._validate();
  22. if(Object.keys(errors).length != 0) {
  23. this.setState({
  24. errors: errors
  25. });
  26. return;
  27. }
  28. var xhr = this._create();
  29. xhr.done(this._onSuccess)
  30. .fail(this._onError)
  31. .always(this.hideLoading)
  32. },
  33. hideLoading: function () {
  34. this.setState({loading: false});
  35. },
  36. _onSuccess: function (data) {
  37. this.refs.user_form.getDOMNode().reset();
  38. this.setState(this.getInitialState());
  39. // show success message
  40. },
  41. _onError: function (data) {
  42. var message = "Failed to create the user";
  43. var res = data.responseJSON;
  44. if(res.message) {
  45. message = data.responseJSON.message;
  46. }
  47. if(res.errors) {
  48. this.setState({
  49. errors: res.errors
  50. });
  51. }
  52. },
  53. _onChange: function (e) {
  54. var state = {};
  55. state[e.target.name] = $.trim(e.target.value);
  56. this.setState(state);
  57. },
  58. _validate: function () {
  59. var errors = {}
  60. if(this.state.username == "") {
  61. errors.username = "Username is required";
  62. }
  63. if(this.state.email == "") {
  64. errors.email = "Email is required";
  65. }
  66. if(this.state.password == "") {
  67. errors.password = "Password is required";
  68. }
  69. return errors;
  70. },
  71. _formGroupClass: function (field) {
  72. var className = "form-group ";
  73. if(field) {
  74. className += " has-error"
  75. }
  76. return className;
  77. },
  78. render: function() {
  79. return (
  80. <div className="form-container">
  81. <form ref='user_form' onSubmit={this._onSubmit}>
  82. <div className={this._formGroupClass(this.state.errors.username)}>
  83. <label className="control-label" for="username">Username </label>
  84. <input name="username" ref="username" type="text" className="form-control" id="username" placeholder="Username" onChange={this._onChange} />
  85. <span className="help-block">{this.state.errors.username}</span>
  86. </div>
  87. <div className={this._formGroupClass(this.state.errors.email)}>
  88. <label className="control-label" for="email">Email address</label>
  89. <input name="email" ref="email" type="email" className="form-control" id="email" placeholder="Email" onChange={this._onChange} />
  90. <span className="help-block">{this.state.errors.email}</span>
  91. </div>
  92. <div className={this._formGroupClass(this.state.errors.password)}>
  93. <label className="control-label" for="password">Password</label>
  94. <input name="password" ref="password" type="password" className="form-control" id="password" placeholder="Password" onChange={this._onChange} />
  95. <span className="help-block">{this.state.errors.password}</span>
  96. </div>
  97. <button type="submit" className="btn btn-default" disabled={this.state.loading}>
  98. Create
  99. <App.Loading loading={this.state.loading} />
  100. </button>
  101. </form>
  102. </div>
  103. );
  104. }
  105. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement