Advertisement
Guest User

Untitled

a guest
Sep 5th, 2017
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { withStateHandlers } from 'recompose'
  2.  
  3. import actions from '../../actions'
  4. const { logIn, logOut } = actions
  5.  
  6. const mapDispatchToProps = {
  7.     logIn,
  8.     logOut,
  9. }
  10.  
  11. const onFieldChangedFactory = field => state => event => ({
  12.     ...state,
  13.     [field]: event.target.value,
  14. })
  15.  
  16. const enhance = compose(
  17.     connect(null, mapDispatchToProps),
  18.     withStateHandlers(
  19.         { email: '', password: '' },
  20.         {
  21.             onEmailChange: onFieldChangedFactory('email'),
  22.             onPasswordChange: onFieldChangedFactory('password'),
  23.             onSubmit: (state, props) => event => {
  24.                 event.preventDefault()
  25.                 props.logIn(state.email, state.password)
  26.             },
  27.             onClickLogOut: (state, { logOut }) => () => logOut(),
  28.         }
  29.     )
  30. )
  31.  
  32. const LoginForm = ({ user, onEmailChange, onPasswordChange, onSubmit, onClickLogOut }) =>
  33.     <form onSubmit={onSubmit}>
  34.         <input type="text" value={user.email} onChange={onEmailChange} />
  35.         <input type="password" value={user.password} onChange={onPasswordChange} />
  36.         <button>Log in</button>
  37.         <button onClick={onClickLogOut}>Log out</button>
  38.     </form>
  39.  
  40. LoginForm.propTypes = {
  41.     user: PropTypes.shape({
  42.         email: PropTypes.string.isRequired,
  43.         password: PropTypes.string.isRequired,
  44.     }),
  45.     onEmailChange: PropTypes.func.isRequired,
  46.     onPasswordChange: PropTypes.func.isRequired,
  47.     onSubmit: PropTypes.func.isRequired,
  48.     onClickLogOut: PropTypes.func.isRequired,
  49. }
  50.  
  51. export default enhance(LoginForm)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement