Vulcan/packages/telescope-users/lib/client/templates/account/user_account.js

101 lines
3.3 KiB
JavaScript
Raw Normal View History

Template.userAccount.helpers({
user: function () {
return Meteor.user();
},
2014-12-08 20:36:46 +09:00
profileIncomplete : function() {
return this && !this.loading && !Users.userProfileComplete(this);
2014-12-08 20:36:46 +09:00
},
userName: function(){
return Users.getUserName(this);
2014-12-08 20:36:46 +09:00
},
userEmail : function(){
return Users.getEmail(this);
2014-12-08 20:36:46 +09:00
},
getTwitter: function(){
return Users.getTwitterName(this) || "";
2014-12-08 20:36:46 +09:00
},
getGitHub: function(){
return Users.getGitHubName(this) || "";
2014-12-08 20:36:46 +09:00
},
profileUrl: function(){
return Users.getProfileUrlBySlugOrId(this.slug);
2014-12-08 20:36:46 +09:00
},
hasNotificationsUsers : function(){
return Users.getUserSetting('notifications.users', '', this) ? 'checked' : '';
2014-12-08 20:36:46 +09:00
},
hasNotificationsPosts : function(){
return Users.getUserSetting('notifications.posts', '', this) ? 'checked' : '';
2014-12-08 20:36:46 +09:00
},
hasNotificationsComments : function(){
return Users.getUserSetting('notifications.comments', '', this) ? 'checked' : '';
2014-12-08 20:36:46 +09:00
},
hasNotificationsReplies : function(){
return Users.getUserSetting('notifications.replies', '', this) ? 'checked' : '';
2014-12-08 20:36:46 +09:00
},
hasPassword: function () {
return Users.hasPassword(Meteor.user());
2014-12-08 20:36:46 +09:00
}
});
Template.userAccount.events({
2014-12-08 20:36:46 +09:00
'submit #account-form': function(e){
e.preventDefault();
2015-03-27 16:24:21 +08:00
Messages.clearSeen();
2014-12-08 20:36:46 +09:00
if(!Meteor.user())
2015-03-27 16:24:21 +08:00
Messages.flash(i18n.t('you_must_be_logged_in'), 'error');
2014-12-08 20:36:46 +09:00
var $target=$(e.target);
var name = $target.find('[name=name]').val();
var email = $target.find('[name=email]').val();
var user = this;
var update = {
2015-03-12 10:12:47 +01:00
"profile.username": name,
"profile.slug": Telescope.utils.slugify(name),
2014-12-08 20:36:46 +09:00
"profile.bio": $target.find('[name=bio]').val(),
2015-04-03 18:52:44 +09:00
"profile.city": $target.find('[name=city]').val(),
2014-12-08 20:36:46 +09:00
"profile.email": email,
"profile.twitter": $target.find('[name=twitter]').val(),
"profile.github": $target.find('[name=github]').val(),
"profile.site": $target.find('[name=site]').val(),
"profile.notifications.users": $('input[name=notifications_users]:checked').length, // only actually used for admins
"profile.notifications.posts": $('input[name=notifications_posts]:checked').length,
"profile.notifications.comments": $('input[name=notifications_comments]:checked').length,
"profile.notifications.replies": $('input[name=notifications_replies]:checked').length
};
var old_password = $target.find('[name=old_password]').val();
var new_password = $target.find('[name=new_password]').val();
if(old_password && new_password){
Accounts.changePassword(old_password, new_password, function(error){
// TODO: interrupt update if there's an error at this point
if(error)
2015-03-27 16:24:21 +08:00
Messages.flash(error.reason, "error");
2014-12-08 20:36:46 +09:00
});
}
update = Users.hooks.userEditClientCallbacks.reduce(function(result, currentFunction) {
2014-12-08 20:36:46 +09:00
return currentFunction(user, result);
}, update);
Meteor.users.update(user._id, {
$set: update
}, function(error){
if(error){
2015-03-27 16:24:21 +08:00
Messages.flash(error.reason, "error");
2014-12-08 20:36:46 +09:00
} else {
2015-03-27 16:24:21 +08:00
Messages.flash(i18n.t('profile_updated'), 'success');
2014-12-08 20:36:46 +09:00
}
Deps.afterFlush(function() {
var element = $('.grid > .error');
$('html, body').animate({scrollTop: element.offset().top});
});
});
Meteor.call('changeEmail', user._id, email);
2014-12-08 20:36:46 +09:00
}
});