summaryrefslogtreecommitdiffstats
path: root/client/components/import/import.js
blob: f15185edb3e17e47fcc88aad72eddf820d128ae3 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
 * Abstract root for all import popup screens.
 * Descendants must define:
 * - getMethodName(): return the Meteor method to call for import, passing json data decoded as object
 * and additional data (see below)
 * - getAdditionalData(): return object containing additional data passed to Meteor method
 * (like list ID and position for a card import)
 * - getLabel(): i18n key for the text displayed in the popup, usually to explain how to get the data out of the
 * source system.
 */
const ImportPopup = BlazeComponent.extendComponent({
  template() {return 'importPopup';},
  events() {
    return [{
      'submit': (evt) => {
        evt.preventDefault();
        const dataJson = $(evt.currentTarget).find('textarea').val();
        let dataObject;
        try {
          dataObject = JSON.parse(dataJson);
        } catch (e) {
          this.setError('error-json-malformed');
          return;
        }
        Meteor.call(this.getMethodName(), dataObject, this.getAdditionalData(),
          (error, response) => {
            if (error) {
              this.setError(error.error);
            } else {
              Filter.addException(response);
              Popup.close();
            }
          }
        );
      },
    }];
  },

  onCreated() {
    this.error = new ReactiveVar('');
  },

  setError(error) {
    this.error.set(error);
  },
});

ImportPopup.extendComponent({
  getAdditionalData() {
    const listId = this.data()._id;
    const firstCardDom = $(`#js-list-${this.currentData()._id} .js-minicard:first`).get(0);
    const sortIndex = Utils.calculateIndex(null, firstCardDom).base;
    const result = {listId, sortIndex};
    return result;
  },

  getMethodName() {
    return 'importTrelloCard';
  },

  getLabel() {
    return 'import-card-trello-instruction';
  },
}).register('listImportCardPopup');

ImportPopup.extendComponent({
  getAdditionalData() {
    const result = {};
    return result;
  },

  getMethodName() {
    return 'importTrelloBoard';
  },

  getLabel() {
    return 'import-board-trello-instruction';
  },
}).register('boardImportBoardPopup');