Advertisement
Guest User

Untitled

a guest
Oct 4th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as React from '../lib/reactjs/react';
  2. import {Spinner} from './spinner';
  3.  
  4. export var LoginForm = React.createClass({
  5.     getDefaultProps: function() {
  6.         return {
  7.             authApiUrl: 'api/user/login'
  8.         };
  9.     },
  10.  
  11.     getInitialState: function() {
  12.         return {
  13.             username: '',
  14.             password: '',
  15.             isLoading: false
  16.         };
  17.     },
  18.  
  19.     setIsLoading: function(isLoading) {
  20.         this.setState({ isLoading: isLoading });
  21.     },
  22.  
  23.     handleChange: function(e) {
  24.         switch (e.target.id) {
  25.             case 'loginForm_username':
  26.                 this.setState({ username: e.target.value })
  27.                 break;
  28.             case 'loginForm_password':
  29.                 this.setState({ password: e.target.value });
  30.                 break;
  31.         }
  32.     },
  33.  
  34.     authApi: function(success, error) {
  35.         $.ajax({
  36.             method: 'POST',
  37.             url: this.props.authApiUrl,
  38.             data: {
  39.                 username: this.state.username,
  40.                 password: this.state.password
  41.             },
  42.             success: success,
  43.             error: error
  44.         });
  45.     },
  46.  
  47.     handleSubmit: function(e) {
  48.         e.preventDefault();
  49.  
  50.         var errors = (this.state.username == '') ? "Zadejte uživatelské jméno!\n" : '';
  51.         errors += (this.state.password == '') ? "Zadejte heslo!\n" : '';
  52.  
  53.         if (errors != '') {
  54.             alert(errors);
  55.         }
  56.         else {
  57.             this.setIsLoading(true);
  58.             this.authApi(
  59.                 function(res) {
  60.                     window.location = '/';
  61.                 }.bind(this),
  62.                 function(xhr, status, err) {
  63.                     console.log(status);
  64.                     console.log(err);
  65.                     this.setIsLoading(false);
  66.                 }.bind(this)
  67.             );
  68.         }
  69.     },
  70.  
  71.     render: function() {
  72.         return (
  73.             <form onSubmit={this.handleSubmit}>
  74.                 <div className="form-group form-group-lg">
  75.                     <input
  76.                         type="text"
  77.                         id="loginForm_username"
  78.                         className="form-control"
  79.                         onChange={this.handleChange}
  80.                     />
  81.                 </div>
  82.                 <div className="form-group form-group-lg">
  83.                     <input
  84.                         type="password"
  85.                         id="loginForm_password"
  86.                         className="form-control"
  87.                         onChange={this.handleChange}
  88.                     />
  89.                 </div>
  90.                 <div className="form-group">
  91.                     <input
  92.                         type="submit"
  93.                         id="loginForm_submit"
  94.                         className="btn btn-primary btn-lg btn-block"
  95.                         value={(this.state.isLoading) ? <Spinner /> : 'Přihlásit'}
  96.                     />
  97.                 </div>
  98.             </form>
  99.         );
  100.     }
  101. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement