Guest User

Untitled

a guest
Apr 26th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { connect } from 'react-redux';
  3. import { login } from '../../redux/reducer';
  4. import './LoginForm.css';
  5. import { BrowserRouter as Router, Route } from "react-router-dom";
  6.  
  7. class LoginForm extends Component {
  8.  
  9. constructor(props) {
  10. super(props);
  11. this.state = {};
  12. this.onSubmit = this.onSubmit.bind(this);
  13. }
  14.  
  15. render() {
  16. let {username, password} = this.state;
  17. let {isLoginPending, isLoginSuccess, loginError} = this.props;
  18. return (
  19. <form name="loginForm" onSubmit={this.onSubmit}>
  20. <div className="form-group-collection">
  21. <div className="form-group">
  22. <label>Username/E-mail:</label>
  23. <input type="username" name="username" onChange={e => this.setState({username: e.target.value})} value={username}/>
  24. </div>
  25.  
  26. <div className="form-group">
  27. <label>Password:</label>
  28. <input type="password" name="password" onChange={e => this.setState({password: e.target.value})} value={password}/>
  29. </div>
  30. </div>
  31.  
  32. <input type="submit" value="Login" />
  33.  
  34. <div className="message">
  35. { isLoginPending && <div>Please wait...</div> }
  36. { isLoginSuccess && <div>Success.
  37. <Router><Route exact path="/" component={Home}/></Router></div> }
  38. { loginError && <div>{loginError.message}</div> }
  39. </div>
  40. </form>
  41. )
  42. }
  43.  
  44. onSubmit(e) {
  45. e.preventDefault();
  46. let { username, password } = this.state;
  47. this.props.login(username, password);
  48. this.setState({
  49. username: '',
  50. password: ''
  51. });
  52. }
  53. }
  54.  
  55. const Home = () => (
  56. <div>
  57. <h2>Welcome Admin </h2>
  58. </div>
  59. );
  60.  
  61. const mapStateToProps = (state) => {
  62. return {
  63. isLoginPending: state.isLoginPending,
  64. isLoginSuccess: state.isLoginSuccess,
  65. loginError: state.loginError
  66. };
  67. }
  68.  
  69. const mapDispatchToProps = (dispatch) => {
  70. return {
  71. login: (username, password) => dispatch(login(username, password))
  72. };
  73. }
  74.  
  75. export default connect(mapStateToProps, mapDispatchToProps)(LoginForm);
  76.  
  77. const SET_LOGIN_PENDING = 'SET_LOGIN_PENDING';
  78. const SET_LOGIN_SUCCESS = 'SET_LOGIN_SUCCESS';
  79. const SET_LOGIN_ERROR = 'SET_LOGIN_ERROR';
  80.  
  81. export function login(username, password) {
  82. return dispatch => {
  83. dispatch(setLoginPending(true));
  84. dispatch(setLoginSuccess(false));
  85. dispatch(setLoginError(null));
  86.  
  87. callLoginApi(username, password, error => {
  88. dispatch(setLoginPending(false));
  89. if (!error) {
  90. dispatch(setLoginSuccess(true));
  91. } else {
  92. dispatch(setLoginError(error));
  93. }
  94. });
  95. }
  96. }
  97.  
  98. function setLoginPending(isLoginPending) {
  99. return {
  100. type: SET_LOGIN_PENDING,
  101. isLoginPending
  102. };
  103. }
  104.  
  105. function setLoginSuccess(isLoginSuccess) {
  106. return {
  107. type: SET_LOGIN_SUCCESS,
  108. isLoginSuccess
  109. };
  110. }
  111.  
  112. function setLoginError(loginError) {
  113. return {
  114. type: SET_LOGIN_ERROR,
  115. loginError
  116. }
  117. }
  118.  
  119. function callLoginApi(username, password, callback) {
  120. setTimeout(() => {
  121. if (username === 'admin' && password === 'password') {
  122. return callback(null);
  123. } else {
  124. return callback(new Error('Invalid username and password'));
  125. }
  126. }, 1000);
  127. }
  128.  
  129. export default function reducer(state = {
  130. isLoginSuccess: false,
  131. isLoginPending: false,
  132. loginError: null
  133. }, action) {
  134. switch (action.type) {
  135. case SET_LOGIN_PENDING:
  136. return Object.assign({}, state, {
  137. isLoginPending: action.isLoginPending
  138. });
  139.  
  140. case SET_LOGIN_SUCCESS:
  141. return Object.assign({}, state, {
  142. isLoginSuccess: action.isLoginSuccess
  143. });
  144.  
  145. case SET_LOGIN_ERROR:
  146. return Object.assign({}, state, {
  147. loginError: action.loginError
  148. });
  149.  
  150. default:
  151. return state;
  152. }
  153. }
  154.  
  155. import { createStore, applyMiddleware } from 'redux';
  156. import thunk from 'redux-thunk';
  157. import logger from 'redux-logger';
  158. import reducer from './reducer';
  159.  
  160. const store = createStore(reducer, {}, applyMiddleware(thunk, logger));
  161. export default store;
Add Comment
Please, Sign In to add comment