Blame view

imports/modules/document-editor.js 1.35 KB
4c9b3dfc1   themeteorchef   cleaning up
1
  /* eslint-disable no-undef */
c42d4eeac   themeteorchef   handful of changes
2
3
4
  import { browserHistory } from 'react-router';
  import { Bert } from 'meteor/themeteorchef:bert';
  import { upsertDocument } from '../api/documents/methods.js';
4c9b3dfc1   themeteorchef   cleaning up
5
  import './validation.js';
c42d4eeac   themeteorchef   handful of changes
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   themeteorchef   merge #201 by han...
18
    upsertDocument.call(upsert, (error, response) => {
c42d4eeac   themeteorchef   handful of changes
19
20
21
      if (error) {
        Bert.alert(error.reason, 'danger');
      } else {
618970298   themeteorchef   fix documentEdito...
22
        component.documentEditorForm.reset();
c42d4eeac   themeteorchef   handful of changes
23
        Bert.alert(confirmation, 'success');
bfc1101ee   themeteorchef   merge #201 by han...
24
        browserHistory.push(`/documents/${response.insertedId || doc._id}`);
c42d4eeac   themeteorchef   handful of changes
25
26
27
28
29
      }
    });
  };
  
  const validate = () => {
618970298   themeteorchef   fix documentEdito...
30
    $(component.documentEditorForm).validate({
c42d4eeac   themeteorchef   handful of changes
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   themeteorchef   fix documentEdito...
50
  export default function documentEditor(options) {
c42d4eeac   themeteorchef   handful of changes
51
52
53
    component = options.component;
    validate();
  }