Blame view

imports/modules/document-editor.js 1.32 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
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
  
  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.form.reset();
        Bert.alert(confirmation, 'success');
        browserHistory.push(`/documents/${insertedId || doc._id}`);
      }
    });
  };
  
  const validate = () => {
    $(component.form).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 handleLogin(options) {
    component = options.component;
    validate();
  }