import { connect } from 'react-redux'
import React from 'react'
import { bindActionCreators } from 'redux'
import { reduxForm, Field } from 'redux-form'

import { Input, FormError, Fields, Button, Anchor, Controls } from '../components'
import { onSubmit, onSubmitSuccess, onSubmitFail } from './actions'
import { validate } from './helpers'

const Page = ({ handleSubmit, onSubmit, submitting, signinError }) => {
  return (
    <form noValidate onSubmit={ handleSubmit }>
      <Fields>
        <FormError error={ signinError } submitting={ submitting }/>
        <Field
          autoFocus
          name='username'
          type='text'
          placeholder='Username or Email'
          component={ Input }/>
        <Field
          name='password'
          type='password'
          placeholder='Password'
          component={ Input }/>
      </Fields>
      <Controls>
        <Anchor prefetch href='/register'>need an account?</Anchor>
        <Button type='submit' disabled={ submitting }>Sign In</Button>
      </Controls>
    </form>
  )
}

export const mapState = ({ signin: { signinError } }) => ({
  signinError
})

export const mapDispatch = dispatch => bindActionCreators({
  onSubmit,
  onSubmitFail,
  onSubmitSuccess
}, dispatch)

export const Signin = reduxForm({
  form: 'signin',
  validate,
  touchOnBlur: false
})(Page)

export default connect(mapState, mapDispatch)(Signin)

@@ -342,20 +342,20 @@
 %0A%0Aconst 
-Page
+Form
  = (%7B ha
@@ -365,26 +365,16 @@
 eSubmit,
- onSubmit,
  submitt
@@ -399,24 +399,9 @@
 ) =%3E
- %7B
 %0A
-  return (%0A  
   %3Cf
@@ -442,26 +442,24 @@
 bmit %7D%3E%0A    
-  
 %3CFields%3E%0A   
@@ -451,26 +451,24 @@
    %3CFields%3E%0A
-  
       %3CFormE
@@ -518,26 +518,24 @@
 mitting %7D/%3E%0A
-  
       %3CField
@@ -543,18 +543,16 @@
         
-  
 autoFocu
@@ -553,18 +553,16 @@
 toFocus%0A
-  
         
@@ -581,26 +581,24 @@
 me'%0A        
- 
- 
 type='text'%0A
@@ -597,18 +597,16 @@
 ='text'%0A
-  
         
@@ -637,18 +637,16 @@
  Email'%0A
-  
         
@@ -669,35 +669,31 @@
 t %7D/%3E%0A      
-  
 %3CField%0A
-  
         name
@@ -708,26 +708,24 @@
 rd'%0A        
- 
- 
 type='passwo
@@ -720,34 +720,32 @@
 type='password'%0A
-  
         placehol
@@ -763,26 +763,24 @@
 rd'%0A        
- 
- 
 component=%7B 
@@ -793,18 +793,16 @@
 %7D/%3E%0A    
-  
 %3C/Fields
@@ -799,26 +799,24 @@
   %3C/Fields%3E%0A
-  
     %3CControl
@@ -824,18 +824,16 @@
 %3E%0A      
-  
 %3CAnchor 
@@ -884,18 +884,16 @@
 Anchor%3E%0A
-  
       %3CB
@@ -957,18 +957,16 @@
 on%3E%0A    
- 
- 
 %3C/Contro
@@ -969,18 +969,16 @@
 ntrols%3E%0A
-  
   %3C/form
@@ -982,14 +982,8 @@
 orm%3E
-%0A  )%0A%7D
 %0A%0Aex
@@ -1270,12 +1270,12 @@
 %0A%7D)(
-Page
+Form
 )%0A%0Ae

Refactor signin form. #35