Guest User

Untitled

a guest
Dec 18th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. import React, {Component} from 'react';
  2. import ErrorMessage from "../ErrorMessage";
  3. import {Field, reduxForm} from 'redux-form';
  4. import {connect} from 'react-redux';
  5. import {validate} from '../validate';
  6. import {SUBMIT_USERNAME_PASSWORD} from "../../constants";
  7.  
  8. class Login extends Component {
  9.  
  10. constructor(props) {
  11. //Login is stateful component, but finally action will change
  12. //reducer state
  13. super(props);
  14. const token = localStorage.getItem('token');
  15. const isAuthenticated = !((token === undefined) | (token === null));
  16. this.state = {
  17. token,
  18. isAuthenticated,
  19. message: null,
  20. statusCode: null
  21. };
  22. }
  23.  
  24. onSubmit(values) {
  25. const {userid, password} = values;
  26. const data = {
  27. username: userid,
  28. password
  29. };
  30. this.props.onSubmitClick(data);
  31. }
  32.  
  33. componentWillUpdate(){
  34. console.log('componentWillUpdate');
  35. if(this.props.isAuthenticated){
  36. this.props.history.push('/companies');
  37. }
  38. }
  39.  
  40. renderField(field) {
  41. const {meta: {touched, error}} = field;
  42. const className = `'form-group' ${touched && error ? 'has-danger' : ''}`;
  43.  
  44. console.log('renderField');
  45.  
  46. return (
  47. <div className={className}>
  48. <label>{field.label}</label>
  49. <input
  50. className="form-control"
  51. type={field.type}
  52. placeholder={field.placeholder}
  53. {...field.input}
  54. />
  55. <div className="text-help">
  56. {touched ? error : ''}
  57. </div>
  58. </div>
  59. );
  60. }
  61.  
  62. render() {
  63. const {handleSubmit} = this.props;
  64.  
  65. return (
  66. <div>
  67. <ErrorMessage
  68. isAuthenticated={this.props.isAuthenticated}
  69. message={this.props.message}
  70. />
  71.  
  72. <form onSubmit={handleSubmit(this.onSubmit.bind(this))}>
  73. <Field
  74. name="userid"
  75. component={this.renderField}
  76. placeholder="User ID"
  77. type="text"
  78. />
  79. <Field
  80. name="password"
  81. component={this.renderField}
  82. placeholder="Password"
  83. type="password"
  84. />
  85. <button type="submit" className="btn btn-primary">Submit</button>
  86. </form>
  87. <a className='btn btn-primary' href="https://www.magicboxasia.com/">Sign up</a>
  88. </div>
  89. );
  90. }
  91. }
  92.  
  93.  
  94. const onSubmitClick = ({username, password}) => {
  95. return {
  96. type: SUBMIT_USERNAME_PASSWORD,
  97. payload: {username, password}
  98. };
  99. };
  100.  
  101. const mapStateToProps = (state, ownProps) => {
  102. return {
  103. ...state.login
  104. }
  105. };
  106.  
  107. export default reduxForm({
  108. validate,
  109. form: 'LoginForm'
  110. })(
  111. connect(mapStateToProps, {onSubmitClick})(Login)
  112. );
  113.  
  114. const shootApiTokenAuth = (values) =>{
  115. const {username, password} = values;
  116. return axios.post(`${ROOT_URL}/api-token-auth/`,
  117. {username, password});
  118. };
  119.  
  120. function* shootAPI(action){
  121. try{
  122. const res = yield call(shootApiTokenAuth, action.payload);
  123. yield put({
  124. type: REQUEST_SUCCESS,
  125. payload: res
  126. });
  127. }catch(err){
  128. yield put({
  129. type: REQUEST_FAILED,
  130. payload: err
  131. });
  132. }
  133. }
  134.  
  135. function * watchSubmitBtn(){
  136. yield takeEvery(SUBMIT_USERNAME_PASSWORD, shootAPI);
  137. }
  138.  
  139. // single entry point to start all Sagas at once
  140. export default function* rootSaga() {
  141. yield all([
  142. watchSubmitBtn()
  143. ])
  144. }
Add Comment
Please, Sign In to add comment