1
0
mirror of https://github.com/ascribe/onion.git synced 2024-09-28 12:08:55 +02:00
onion/js/mixins/form_mixin.js

54 lines
1.3 KiB
JavaScript
Raw Normal View History

2015-06-01 17:43:38 +02:00
import fetch from '../utils/fetch';
import React from 'react';
import AppConstants from '../constants/application_constants'
import AlertDismissable from '../components/ascribe_forms/alert'
export const FormMixin = {
getInitialState() {
return {
submitted: false
, status: null
}
},
2015-06-01 18:00:34 +02:00
submit(e) {
e.preventDefault();
this.setState({submitted: true});
2015-06-01 17:43:38 +02:00
fetch
.post(this.url(), { body: this.getFormData() })
.then(response => { this.props.onRequestHide(); })
.catch(this.handleError);
},
2015-06-01 18:00:34 +02:00
2015-06-01 17:43:38 +02:00
handleError(err){
if (err.json) {
for (var input in errors){
if (this.refs && this.refs[input] && this.refs[input].state) {
this.refs[input].setAlerts(errors[input]);
}
}
2015-06-01 17:43:38 +02:00
this.setState({submitted: false});
} else {
this.setState({submitted: false, status: response.status});
}
},
2015-06-01 18:00:34 +02:00
render(){
let alert = null;
if (this.state.status >= 500){
alert = <AlertDismissable error="Something went wrong, please try again later"/>;
}
return (
<div>
{alert}
{this.renderForm()}
</div>
)
}
};
export default FormMixin;