Vulcan/packages/telescope-invites/lib/client/templates/user_invites.js

63 lines
1.5 KiB
JavaScript
Raw Normal View History

Template.userInvites.created = function () {
2014-12-08 22:17:43 +09:00
var user = this.data;
var instance = this;
instance.invites = new ReactiveVar({});
Meteor.autorun(function () {
coreSubscriptions.subscribe('invites', user._id);
var invites = Invites.find({invitingUserId: user._id});
instance.invites.set(invites);
});
};
Template.userInvites.helpers({
2014-12-08 22:17:43 +09:00
canCurrentUserInvite: function () {
2014-07-31 01:18:17 +02:00
var currentUser = Meteor.user();
return currentUser && (currentUser.inviteCount > 0 && can.invite(currentUser));
2014-07-31 01:18:17 +02:00
},
2014-12-08 22:17:43 +09:00
invitesLeft: function () {
2014-07-31 01:18:17 +02:00
var currentUser = Meteor.user();
return currentUser ? currentUser.inviteCount : 0;
},
2014-12-08 22:17:43 +09:00
invitesSchema: function () {
// expose schema for Invites (used by AutoForm)
2014-07-31 01:18:17 +02:00
return InviteSchema;
2014-12-08 22:17:43 +09:00
},
invites: function () {
return Template.instance().invites.get();
2014-07-31 01:18:17 +02:00
}
});
var scrollUp = function(){
Deps.afterFlush(function() {
var element = $('.grid > .error');
$('html, body').animate({scrollTop: element.offset().top});
});
};
AutoForm.hooks({
inviteForm: {
onSuccess: function(operation, result, template) {
2015-03-27 16:24:21 +08:00
Messages.clearSeen();
if(result && result.newUser){
2015-03-27 16:24:21 +08:00
Messages.flash('An invite has been sent out. Thank you!', "success");
} else {
2015-03-27 16:24:21 +08:00
Messages.flash('Thank you!', "info");
}
scrollUp();
},
onError: function(operation, error, template) {
2015-03-27 16:24:21 +08:00
Messages.clearSeen();
if(error && error.reason){
2015-03-27 16:24:21 +08:00
Messages.flash(error.reason, "error");
scrollUp();
}
}
}
});