Vulcan/client/views/users/user_edit.js

61 lines
2.1 KiB
JavaScript
Raw Normal View History

Template.user_edit.helpers({
profileIncomplete : function() {
return Meteor.user() && !this.loading && !userProfileComplete(this);
},
user : function(){
var currentUser=Meteor.user();
if(Session.get('selectedUserId') && !currentUser.loading && currentUser.isAdmin){
return Meteor.users.findOne(Session.get('selectedUserId'));
}else{
return currentUser;
}
},
hasNotificationsNone : function(){
return Meteor.user().profile && Meteor.user().profile.notificationsFrequency == 0 ? 'checked' : '';
},
hasNotificationsActivity : function(){
2013-01-29 10:51:44 +09:00
var u = Meteor.user();
return u.profile && (u.profile.notificationsFrequency == 1 || typeof u.profile.notificationsFrequency === 'undefined') ? 'checked' : '';
},
hasNotificationsAll : function(){
return Meteor.user().profile && Meteor.user().profile.notificationsFrequency == 2 ? 'checked' : '';
}
})
2012-09-19 09:03:25 +09:00
Template.user_edit.events = {
2012-10-01 13:08:46 +10:00
'submit form': function(e){
2012-09-19 09:03:25 +09:00
e.preventDefault();
2012-10-01 13:57:52 +09:00
if(!Meteor.user()) throwError('You must be logged in.');
2012-10-01 13:59:23 +09:00
var $target=$(e.target);
2012-10-11 08:23:52 +09:00
var user=Session.get('selectedUserId') ? Meteor.users.findOne(Session.get('selectedUserId')) : Meteor.user();
2012-10-01 13:08:46 +10:00
var update = {
2012-10-01 13:59:23 +09:00
"profile.name": $target.find('[name=name]').val(),
"profile.bio": $target.find('[name=bio]').val(),
"profile.email": $target.find('[name=email]').val(),
"profile.notificationsFrequency": parseInt($('input[name=notifications]:checked').val())
2012-10-01 13:08:46 +10:00
};
// TODO: enable change email
2012-10-01 13:59:23 +09:00
var email = $target.find('[name=email]').val();
2012-10-01 13:08:46 +10:00
2012-10-01 13:57:52 +09:00
var old_password = $target.find('[name=old_password]').val();
var new_password = $target.find('[name=new_password]').val();
2012-10-01 13:08:46 +10:00
2012-10-01 13:57:52 +09:00
// XXX we should do something if there is an error updating these things
2012-09-19 09:03:25 +09:00
if(old_password && new_password){
Meteor.changePassword(old_password, new_password);
}
2012-10-01 13:08:46 +10:00
Meteor.users.update(user._id, {
$set: update
}, function(error){
if(error){
throwError(error.reason);
} else {
throwError('Profile updated');
}
});
2012-09-19 09:03:25 +09:00
}
};