Blame view
imports/modules/document-editor.js
1.35 KB
4c9b3dfc1
|
1 |
/* eslint-disable no-undef */ |
c42d4eeac
|
2 3 4 |
import { browserHistory } from 'react-router'; import { Bert } from 'meteor/themeteorchef:bert'; import { upsertDocument } from '../api/documents/methods.js'; |
4c9b3dfc1
|
5 |
import './validation.js'; |
c42d4eeac
|
6 7 8 9 10 11 12 13 14 15 16 17 |
let component; const handleUpsert = () => { const { doc } = component.props; const confirmation = doc && doc._id ? 'Document updated!' : 'Document added!'; const upsert = { title: document.querySelector('[name="title"]').value.trim(), body: document.querySelector('[name="body"]').value.trim(), }; if (doc && doc._id) upsert._id = doc._id; |
bfc1101ee
|
18 |
upsertDocument.call(upsert, (error, response) => { |
c42d4eeac
|
19 20 21 |
if (error) { Bert.alert(error.reason, 'danger'); } else { |
618970298
|
22 |
component.documentEditorForm.reset(); |
c42d4eeac
|
23 |
Bert.alert(confirmation, 'success'); |
bfc1101ee
|
24 |
browserHistory.push(`/documents/${response.insertedId || doc._id}`); |
c42d4eeac
|
25 26 27 28 29 |
} }); }; const validate = () => { |
618970298
|
30 |
$(component.documentEditorForm).validate({ |
c42d4eeac
|
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
rules: { title: { required: true, }, body: { required: true, }, }, messages: { title: { required: 'Need a title in here, Seuss.', }, body: { required: 'This thneeds a body, please.', }, }, submitHandler() { handleUpsert(); }, }); }; |
618970298
|
50 |
export default function documentEditor(options) { |
c42d4eeac
|
51 52 53 |
component = options.component; validate(); } |