import SignUpFormView from './SignUpFormView.coffee'

export default withSignUpForm = (defaultValue) =>
  (WrappedComponent) =>
    observer class WithSignUpForm extends Component
      constructor: (props) ->
        super props

        @state = observable
          signUpFormValue:
            email: defaultValue.email or ''
            password: defaultValue.password or ''
            passwordConfirm: defaultValue.passwordConfirm or ''
          signUpFormError:
            email: if defaultValue.email then '' else null
            password: if defaultValue.password then '' else null
            passwordConfirm: if defaultValue.passwordConfirm then '' else null

      @propTypes:
        defaultValue: PropTypes.object

      @defaultProps:
        defaultValue: {}

      onChangeSignUpForm: (value, error) =>
        @state.signUpFormValue = value

        @state.signUpFormError = error

      render: =>
        <div>
          <SignUpFormView onChange={@onChangeSignUpForm} defaultValue={defaultValue} />
          <div style={{ marginTop: 20 }}>
            <WrappedComponent {...@props} signUpFormValue={toJS @state.signUpFormValue} signUpFormError={toJS @state.signUpFormError} />
          </div>
        </div>
