document-editor.js
1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* eslint-disable no-undef */
import { browserHistory } from 'react-router';
import { Bert } from 'meteor/themeteorchef:bert';
import { upsertDocument } from '../api/documents/methods.js';
import './validation.js';
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;
upsertDocument.call(upsert, (error, { insertedId }) => {
if (error) {
Bert.alert(error.reason, 'danger');
} else {
component.documentEditorForm.reset();
Bert.alert(confirmation, 'success');
browserHistory.push(`/documents/${insertedId || doc._id}`);
}
});
};
const validate = () => {
$(component.documentEditorForm).validate({
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(); },
});
};
export default function documentEditor(options) {
component = options.component;
validate();
}