Skip to content

Instantly share code, notes, and snippets.

@helloncanella
Created March 19, 2017 21:26
Show Gist options
  • Save helloncanella/115e8ba601626463921ccdcaabf7d263 to your computer and use it in GitHub Desktop.
Save helloncanella/115e8ba601626463921ccdcaabf7d263 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react'
import Form from './Form.jsx'
import fieldsets from './fieldsets.js'
import FieldsetsValidator from './fieldsets-validator.js'
import './style.scss'
class FormWrapper extends Component {
constructor() {
super()
this.state = {
dataWasSent: false,
validationError: null
}
this.onSubmit = this.onSubmit.bind(this)
}
setError(error) {
this.setState({ validationError: error })
}
validateFields(formData) {
const validator = new FieldsetsValidator()
try {
validator.validate(formData)
} catch (error) {
this.setError(error)
return false
}
return true;
}
sendData() {
/***
SENDING ALGORITHM!!!!!!!!!
**/
this.setState({ dataWasSent: true })
}
onSubmit() {
const formData = this.form.getData();
this.validateFields(formData) && this.sendData(formData)
}
formProps() {
return {
onSubmit: this.onSubmit,
validationError: this.state.validationError,
fieldsets,
ref: e => this.form = e
}
}
render() {
const SentMessage = () => <h1 className="sent">Sent</h1>;
return this.state.dataWasSent ? <SentMessage /> : <Form {...this.formProps() } />
}
}
export default FormWrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment