2016-03-30 19:08:06 +09:00
|
|
|
import React, { PropTypes, Component } from 'react';
|
|
|
|
import Formsy from 'formsy-react';
|
|
|
|
import { Button } from 'react-bootstrap';
|
|
|
|
|
|
|
|
import SmartForms from "./smart-forms.jsx";
|
|
|
|
import Utils from './utils.js';
|
|
|
|
|
|
|
|
const EditDocument = React.createClass({
|
|
|
|
|
|
|
|
propTypes: {
|
|
|
|
document: React.PropTypes.object.isRequired,
|
|
|
|
collection: React.PropTypes.object.isRequired,
|
|
|
|
currentUser: React.PropTypes.object,
|
|
|
|
successCallback: React.PropTypes.func,
|
|
|
|
errorCallback: React.PropTypes.func,
|
|
|
|
methodName: React.PropTypes.string,
|
|
|
|
labelFunction: React.PropTypes.func
|
|
|
|
},
|
|
|
|
|
2016-03-31 09:11:47 +09:00
|
|
|
getFields() {
|
|
|
|
const collection = this.props.collection;
|
|
|
|
const fields = collection.getInsertableFields(this.props.currentUser);
|
|
|
|
return fields;
|
|
|
|
},
|
|
|
|
|
2016-03-30 19:08:06 +09:00
|
|
|
submitForm(data) {
|
|
|
|
|
2016-03-31 09:11:47 +09:00
|
|
|
const fields = this.getFields();
|
2016-03-30 19:08:06 +09:00
|
|
|
const document = this.props.document;
|
2016-03-31 09:11:47 +09:00
|
|
|
// put all keys with data on $set
|
|
|
|
const set = _.compactObject(Utils.flatten(data));
|
|
|
|
// put all keys without data on $unset
|
|
|
|
const unsetKeys = _.difference(fields, _.keys(set));
|
|
|
|
const unset = _.object(unsetKeys, unsetKeys.map(()=>true));
|
|
|
|
const modifier = {$set: set, $unset: unset};
|
2016-03-30 19:08:06 +09:00
|
|
|
const collection = this.props.collection;
|
|
|
|
const methodName = this.props.methodName ? this.props.methodName : collection._name+'.edit';
|
|
|
|
|
|
|
|
Meteor.call(methodName, document._id, modifier, (error, document) => {
|
|
|
|
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 {
|
|
|
|
if (this.props.successCallback) {
|
|
|
|
this.props.successCallback(document);
|
|
|
|
}
|
|
|
|
if (this.context.closeCallback) {
|
|
|
|
this.context.closeCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
|
|
|
const document = this.props.document;
|
|
|
|
const collection = this.props.collection;
|
2016-03-31 09:11:47 +09:00
|
|
|
const fields = this.getFields();
|
2016-03-30 19:08:06 +09:00
|
|
|
|
2016-03-31 13:24:19 -06:00
|
|
|
console.log('called editdocument render')
|
|
|
|
|
2016-03-30 19:08:06 +09:00
|
|
|
const style = {
|
|
|
|
maxWidth: "800px",
|
|
|
|
width: "100%"
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="edit-document" style={style}>
|
|
|
|
<Formsy.Form onSubmit={this.submitForm}>
|
2016-03-31 09:11:47 +09:00
|
|
|
{fields.map(fieldName =>
|
|
|
|
<div key={fieldName} className={"input-"+fieldName}>
|
|
|
|
{SmartForms.getComponent(fieldName, collection.simpleSchema()._schema[fieldName], this.props.labelFunction, document)}
|
|
|
|
</div>
|
|
|
|
)}
|
2016-03-30 19:08:06 +09:00
|
|
|
<Button type="submit" bsStyle="primary">Submit</Button>
|
|
|
|
</Formsy.Form>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
EditDocument.contextTypes = {
|
|
|
|
closeCallback: React.PropTypes.func
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = EditDocument;
|
|
|
|
export default EditDocument;
|