Import single card: proper error handling

This commit is contained in:
Xavier Priour 2015-10-14 22:09:32 +02:00 committed by Maxime Quandalle
parent 7d57ce896b
commit b670a1ab36
4 changed files with 71 additions and 45 deletions

View file

@ -32,6 +32,8 @@ template(name="listMoveCardsPopup")
+boardLists +boardLists
template(name="listImportCardPopup") template(name="listImportCardPopup")
if error.get
.warning {{_ error.get}}
form form
label label
| {{_ 'card-json'}} | {{_ 'card-json'}}

View file

@ -49,30 +49,46 @@ Template.listActionPopup.events({
}, },
}); });
Template.listImportCardPopup.events({
submit(evt) { BlazeComponent.extendComponent({
// 1. get the json data out of the form and parse it events() {
evt.preventDefault(); return [{
const jsonData = $(evt.currentTarget).find('textarea').val(); 'submit': (evt) => {
const firstCardDom = $(`#js-list-${this._id} .js-minicard:first`).get(0); evt.preventDefault();
const sortIndex = Utils.calculateIndex(null, firstCardDom).base; const jsonData = $(evt.currentTarget).find('textarea').val();
try { const firstCardDom = $(`#js-list-${this.currentData()._id} .js-minicard:first`).get(0);
const trelloCard = JSON.parse(jsonData); const sortIndex = Utils.calculateIndex(null, firstCardDom).base;
const cardId = Meteor.call('importTrelloCard', trelloCard, this._id, sortIndex); let trelloCard;
// In case the filter is active we need to add the newly inserted card in try {
// the list of exceptions -- cards that are not filtered. Otherwise the trelloCard = JSON.parse(jsonData);
// card will disappear instantly. } catch (e) {
// See https://github.com/wekan/wekan/issues/80 console.log(e);
Filter.addException(cardId); this.setError('error-json-malformed');
Popup.close(); return;
} catch(e) { }
// XXX handle error Meteor.call('importTrelloCard', trelloCard, this.currentData()._id, sortIndex,
// this.error.set('avatar-too-big'); (error, response) => {
console.log('Invalid JSON'); if (error) {
return; console.log(error);
} this.setError(error.error);
} else {
Filter.addException(response);
Popup.close();
}
}
);
}
},];
}, },
});
onCreated() {
this.error = new ReactiveVar('');
},
setError(error) {
this.error.set(error);
},
}).register('listImportCardPopup');
Template.listMoveCardsPopup.events({ Template.listMoveCardsPopup.events({
'click .js-select-list'() { 'click .js-select-list'() {

View file

@ -112,6 +112,10 @@
"editLabelPopup-title": "Change Label", "editLabelPopup-title": "Change Label",
"editProfilePopup-title": "Edit Profile", "editProfilePopup-title": "Edit Profile",
"email": "Email", "email": "Email",
"error-board-notAMember": "You need to be a member of this board to do that",
"error-json-malformed": "Your text is not valid JSON",
"error-json-schema": "Your JSON data does not include the proper information in the correct format",
"error-list-doesNotExist": "This list does not exist",
"filter": "Filter", "filter": "Filter",
"filter-cards": "Filter Cards", "filter-cards": "Filter Cards",
"filter-clear": "Clear filter", "filter-clear": "Clear filter",

View file

@ -1,40 +1,44 @@
Meteor.methods({ Meteor.methods({
/**
*
*/
importTrelloCard(trelloCard, listId, sortIndex) { importTrelloCard(trelloCard, listId, sortIndex) {
// 1. check parameters are ok from a syntax point of view // 1. check parameters are ok from a syntax point of view
DateString = Match.Where(function (dateAsString) { DateString = Match.Where(function (dateAsString) {
check(dateAsString, String); check(dateAsString, String);
return moment(dateAsString, moment.ISO_8601).isValid(); return moment(dateAsString, moment.ISO_8601).isValid();
}); });
check(trelloCard, Match.ObjectIncluding({ try {
name: String, check(trelloCard, Match.ObjectIncluding({
desc: String,
closed: Boolean,
dateLastActivity: DateString,
labels: [Match.ObjectIncluding({
name: String, name: String,
color: String, desc: String,
})], closed: Boolean,
actions: [Match.ObjectIncluding({ dateLastActivity: DateString,
type: String, labels: [Match.ObjectIncluding({
date: DateString, name: String,
data: Object, color: String,
})], })],
members: [Object], actions: [Match.ObjectIncluding({
})); type: String,
check(listId, String); date: DateString,
check(sortIndex, Number); data: Object,
})],
members: [Object],
}));
check(listId, String);
check(sortIndex, Number);
} catch(e) {
if(Meteor.isServer) {
console.log(e);
}
throw new Meteor.Error('error-json-schema');
}
// 2. check parameters are ok from a business point of view (exist & authorized) // 2. check parameters are ok from a business point of view (exist & authorized)
const list = Lists.findOne(listId); const list = Lists.findOne(listId);
if(!list) { if(!list) {
throw 'exception-list-doesNotExist'; throw new Meteor.Error('error-list-doesNotExist');
} }
if(Meteor.isServer) { if(Meteor.isServer) {
if (!allowIsBoardMember(Meteor.userId(), Boards.findOne(list.boardId))) { if (!allowIsBoardMember(Meteor.userId(), Boards.findOne(list.boardId))) {
throw 'exception-board-notAMember'; throw new Meteor.Error('error-board-notAMember');
} }
} }