/* eslint-disable max-len, no-return-assign */ import React from 'react'; import PropTypes from 'prop-types'; import { Form, Button } from 'react-bootstrap'; import { Meteor } from 'meteor/meteor'; import { Bert } from 'meteor/themeteorchef:bert'; import validate from '../../../modules/validate'; class DocumentEditor extends React.Component { componentDidMount() { const component = this; validate(component.form, { rules: { title: { required: true, }, body: { required: true, }, }, messages: { title: { required: 'Need a title in here, Seuss.', }, body: { required: 'This thneeds a body, please.', }, }, submitHandler() { component.handleSubmit(); }, }); } handleSubmit() { const { history } = this.props; const existingDocument = this.props.doc && this.props.doc._id; const methodToCall = existingDocument ? 'documents.update' : 'documents.insert'; const doc = { title: this.title.value.trim(), body: this.body.value.trim(), }; if (existingDocument) doc._id = existingDocument; Meteor.call(methodToCall, doc, (error, documentId) => { if (error) { Bert.alert(error.reason, 'danger'); } else { const confirmation = existingDocument ? 'Document updated!' : 'Document added!'; this.form.reset(); Bert.alert(confirmation, 'success'); history.push(`/documents/${documentId}`); } }); } render() { const { doc } = this.props; return (
(this.form = form)} onSubmit={event => event.preventDefault()}> Title (this.title = title)} defaultValue={doc && doc.title} placeholder="Oh, The Places You'll Go!" /> Body