summaryrefslogtreecommitdiffstats
path: root/client/components/import/import.js
blob: 038f485c439ec3e902f25078bd850961a51b5727 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/// 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';
  },

  jsonText() {
    return Session.get('import.text');
  },

  membersMapping() {
    return Session.get('import.membersToMap');
  },

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

  onFinish() {
    Popup.close();
  },

  onShowMapping(evt) {
    this._storeText(evt);
    Popup.open('mapMembers')(evt);
  },

  _storeText() {
    const dataJson = this.$('.js-import-json').val();
    Session.set('import.text', dataJson);
    return dataJson;
  },

  onSubmit(evt){
    evt.preventDefault();
    const dataJson = this._storeText(evt);
    let dataObject;
    try {
      dataObject = JSON.parse(dataJson);
      this.setError('');
    } catch (e) {
      this.setError('error-json-malformed');
      return;
    }
    // if there are members listed in the import and we have no mapping for them...
    if(dataObject.members.length > 0 && !this.membersMapping()) {
      // we will work on the list itself (an ordered array of POJO)
      // when a mapping is done, we add a 'wekan' field to the POJO representing the imported member
      const membersToMap = dataObject.members;
      // auto-map based on username
      membersToMap.forEach((importedMember) => {
        const wekanUser = Users.findOne({username: importedMember.username});
        if(wekanUser) {
          importedMember.wekan = wekanUser;
        }
      });
      // store members data and mapping in Session
      // (we go deep and 2-way, so storing in data context is not a viable option)
      Session.set('import.membersToMap', membersToMap);
      Popup.open('mapMembers')(evt);
    } else {
      const additionalData = this.getAdditionalData();
      const membersMapping = this.membersMapping();
      if(membersMapping) {
        const mappingById = {};
        membersMapping.forEach((member) => {
          if (member.wekan) {
            mappingById[member.id] = member.wekan._id;
          }
        });
        additionalData.membersMapping = mappingById;
      }
      Session.set('import.membersToMap', null);
      Session.set('import.text', null);
      Meteor.call(this.getMethodName(), dataObject, additionalData,
        (error, response) => {
          if (error) {
            this.setError(error.error);
          } else {
            // ensure will display what we just imported
            Filter.addException(response);
            this.onFinish(response);
          }
        }
      );
    }
  },

  events() {
    return [{
      submit: this.onSubmit,
      'click .show-mapping': this.onShowMapping,
    }];
  },

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

});

ImportPopup.extendComponent({
  getAdditionalData() {
    const listId = this.currentData()._id;
    const selector = `#js-list-${this.currentData()._id} .js-minicard:first`;
    const firstCardDom = $(selector).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';
  },

  onFinish(response) {
    Utils.goBoardId(response);
  },
}).register('boardImportBoardPopup');

const ImportMapMembers = BlazeComponent.extendComponent({
  members() {
    return Session.get('import.membersToMap');
  },
  _refreshMembers(listOfMembers) {
    Session.set('import.membersToMap', listOfMembers);
  },
  /**
   * Will look into the list of members to import for the specified memberId,
   * then set its property to the supplied value.
   * If unset is true, it will remove the property from the rest of the list as well.
   *
   * use:
   * - memberId = null to use selected member
   * - value = null to unset a property
   * - unset = true to ensure property is only set on 1 member at a time
   */
  _setPropertyForMember(property, value, memberId, unset = false) {
    const listOfMembers = this.members();
    let finder = null;
    if(memberId) {
      finder = (member) => member.id === memberId;
    } else {
      finder = (member) => member.selected;
    }
    listOfMembers.forEach((member) => {
      if(finder(member)) {
        if(value !== null) {
          member[property] = value;
        } else {
          delete member[property];
        }
        if(!unset) {
          // we shortcut if we don't care about unsetting the others
          return false;
        }
      } else if(unset) {
        delete member[property];
      }
      return true;
    });
    // Session.get gives us a copy, we have to set it back so it sticks
    this._refreshMembers(listOfMembers);
  },
  setSelectedMember(memberId) {
    return this._setPropertyForMember('selected', true, memberId, true);
  },
  /**
   * returns the member with specified id,
   * or the selected member if memberId is not specified
   */
  getMember(memberId = null) {
    const allMembers = Session.get('import.membersToMap');
    let finder = null;
    if(memberId) {
      finder = (user) => user.id === memberId;
    } else {
      finder = (user) => user.selected;
    }
    return allMembers.find(finder);
  },
  mapSelectedMember(wekan) {
    return this._setPropertyForMember('wekan', wekan, null);
  },
  unmapMember(memberId){
    return this._setPropertyForMember('wekan', null, memberId);
  },
});

ImportMapMembers.extendComponent({
  onMapMember(evt) {
    const memberToMap = this.currentData();
    if(memberToMap.wekan) {
      // todo xxx ask for confirmation?
      this.unmapMember(memberToMap.id);
    } else {
      this.setSelectedMember(memberToMap.id);
      Popup.open('mapMembersAdd')(evt);
    }
  },
  onSubmit(evt) {
    evt.preventDefault();
    Popup.back();
  },
  events() {
    return [{
      'submit': this.onSubmit,
      'click .mapping': this.onMapMember,
    }];
  },
}).register('mapMembersPopup');

ImportMapMembers.extendComponent({
  onSelectUser(){
    this.mapSelectedMember(this.currentData());
    Popup.back();
  },
  events() {
    return [{
      'click .js-select-import': this.onSelectUser,
    }];
  },
  onRendered() {
    // todo XXX why do I not get the focus??
    this.find('.js-map-member input').focus();
  },
}).register('mapMembersAddPopup');