Commit 024ea9dddbb19f964209978d2b49f60c3e454d0c

Authored by themeteorchef
Exists in master

Merge branch 'refactor/beauremus_#201'

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