2016-03-30 19:08:06 +09:00
|
|
|
import React, { PropTypes, Component } from 'react';
|
|
|
|
import Formsy from 'formsy-react';
|
|
|
|
import { Button } from 'react-bootstrap';
|
|
|
|
|
2016-04-01 20:57:37 +09:00
|
|
|
import FormComponent from "./FormComponent.jsx";
|
2016-03-30 19:08:06 +09:00
|
|
|
import Utils from './utils.js';
|
|
|
|
|
2016-04-01 20:57:37 +09:00
|
|
|
class NewDocument extends Component {
|
2016-03-30 19:08:06 +09:00
|
|
|
|
2016-04-01 20:57:37 +09:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.submitForm = this.submitForm.bind(this);
|
|
|
|
this.state = {
|
|
|
|
disabled: false
|
|
|
|
};
|
|
|
|
}
|
2016-03-30 19:08:06 +09:00
|
|
|
|
|
|
|
submitForm(data) {
|
|
|
|
|
2016-04-01 20:57:37 +09:00
|
|
|
this.setState({disabled: true});
|
|
|
|
|
2016-04-04 09:31:48 +09:00
|
|
|
// if there's a submit callback, run it
|
|
|
|
if (this.props.submitCallback) {
|
|
|
|
this.props.submitCallback();
|
|
|
|
}
|
|
|
|
|
2016-03-30 19:08:06 +09:00
|
|
|
// remove any empty properties
|
2016-04-03 20:58:11 +09:00
|
|
|
let document = _.compactObject(Utils.flatten(data));
|
2016-03-30 19:08:06 +09:00
|
|
|
const collection = this.props.collection;
|
|
|
|
const methodName = this.props.methodName ? this.props.methodName : collection._name+'.create';
|
|
|
|
|
2016-04-03 20:58:11 +09:00
|
|
|
// add prefilled properties
|
|
|
|
if (this.props.prefilledProps) {
|
|
|
|
document = Object.assign(document, this.props.prefilledProps);
|
|
|
|
}
|
|
|
|
|
2016-03-30 19:08:06 +09:00
|
|
|
Meteor.call(methodName, document, (error, document) => {
|
2016-04-01 20:57:37 +09:00
|
|
|
|
|
|
|
this.setState({disabled: false});
|
|
|
|
|
2016-03-30 19:08:06 +09:00
|
|
|
if (error) {
|
|
|
|
console.log(error)
|
|
|
|
if (this.props.errorCallback) {
|
2016-03-31 09:36:25 +09:00
|
|
|
this.props.errorCallback(document, error);
|
2016-03-30 19:08:06 +09:00
|
|
|
}
|
|
|
|
} else {
|
2016-04-03 20:58:11 +09:00
|
|
|
this.refs.newDocumentForm.reset();
|
2016-03-30 19:08:06 +09:00
|
|
|
if (this.props.successCallback) {
|
|
|
|
this.props.successCallback(document);
|
|
|
|
}
|
|
|
|
if (this.context.closeCallback) {
|
|
|
|
this.context.closeCallback();
|
|
|
|
}
|
|
|
|
}
|
2016-04-01 20:57:37 +09:00
|
|
|
|
2016-03-30 19:08:06 +09:00
|
|
|
});
|
2016-04-01 20:57:37 +09:00
|
|
|
}
|
2016-03-30 19:08:06 +09:00
|
|
|
|
|
|
|
render() {
|
|
|
|
|
|
|
|
const collection = this.props.collection;
|
|
|
|
const fields = collection.getInsertableFields(this.props.currentUser);
|
|
|
|
|
|
|
|
const style = {
|
|
|
|
maxWidth: "800px",
|
|
|
|
width: "100%"
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="new-document" style={style}>
|
2016-04-03 20:58:11 +09:00
|
|
|
<Formsy.Form onSubmit={this.submitForm} disabled={this.state.disabled} ref="newDocumentForm">
|
2016-04-01 20:57:37 +09:00
|
|
|
{fields.map(fieldName => <FormComponent
|
|
|
|
key={fieldName}
|
|
|
|
className={"input-"+fieldName}
|
|
|
|
fieldName={fieldName}
|
|
|
|
field={collection.simpleSchema()._schema[fieldName]}
|
|
|
|
labelFunction={this.props.labelFunction}
|
|
|
|
/>)}
|
2016-03-30 19:08:06 +09:00
|
|
|
<Button type="submit" bsStyle="primary">Submit</Button>
|
|
|
|
</Formsy.Form>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2016-04-01 20:57:37 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
NewDocument.propTypes = {
|
|
|
|
collection: React.PropTypes.object.isRequired,
|
|
|
|
currentUser: React.PropTypes.object,
|
|
|
|
errorCallback: React.PropTypes.func,
|
|
|
|
successCallback: React.PropTypes.func,
|
|
|
|
methodName: React.PropTypes.string,
|
2016-04-03 20:58:11 +09:00
|
|
|
labelFunction: React.PropTypes.func,
|
|
|
|
prefilledProps: React.PropTypes.object
|
2016-04-01 20:57:37 +09:00
|
|
|
}
|
2016-03-30 19:08:06 +09:00
|
|
|
|
|
|
|
NewDocument.contextTypes = {
|
|
|
|
closeCallback: React.PropTypes.func
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = NewDocument;
|
|
|
|
export default NewDocument;
|