Added edit user from admin panel

This commit is contained in:
Thuan Pham Quoc 2017-11-07 22:26:21 +07:00
parent 29d54f46aa
commit 1f6545e411
2 changed files with 155 additions and 22 deletions

View file

@ -19,18 +19,66 @@ template(name="people")
template(name="peopleGeneral")
table
tbody
tr
th {{_ 'username'}}
th {{_ 'fullname'}}
th {{_ 'isAdmin'}}
th {{_ 'email'}}
th {{_ 'verified'}}
th {{_ 'createdAt'}}
th {{_ 'active'}}
th
each user in peopleList
tr
th {{_ 'username'}}
th {{_ 'fullname'}}
th {{_ 'isAdmin'}}
th {{_ 'email'}}
th {{_ 'verified'}}
th {{_ 'createdAt'}}
tr
td {{ user.username }}
td {{ user.profile.fullname }}
td {{ user.isAdmin }}
td {{ user.emails.[0].address }}
td {{ user.emails.[0].verified }}
td {{ user.createdAt }}
+peopleRow(userId=user._id)
template(name="peopleRow")
tr
td.username {{ userData.username }}
td {{ userData.profile.fullname }}
td
if userData.isAdmin
| true
else
| false
td {{ userData.emails.[0].address }}
td
if userData.emails.[0].verified
| true
else
| false
td {{ userData.createdAt }}
td
if userData.active
| true
else
| false
td
a.edit-user
| edit
template(name="editUserPopup")
form
label.hide.userId(type="text" value=user._id)
label
| {{_ 'fullname'}}
input.js-profile-fullname(type="text" value=user.profile.fullname autofocus)
label
| {{_ 'username'}}
span.error.hide.username-taken
| {{_ 'error-username-taken'}}
input.js-profile-username(type="text" value=user.username)
label
| {{_ 'initials'}}
input.js-profile-initials(type="text" value=user.profile.initials)
label
| {{_ 'email'}}
span.error.hide.email-taken
| {{_ 'error-email-taken'}}
input.js-profile-email(type="email" value="{{user.emails.[0].address}}")
label
| {{_ 'isAdmin'}}
select.select-role.js-profile-isadmin
option(value="false") No
option(value="true" selected="{{user.isAdmin}}") Yes
input.primary.wide(type="submit" value="{{_ 'save'}}")

View file

@ -6,21 +6,106 @@ BlazeComponent.extendComponent({
this.loading = new ReactiveVar(false);
this.people = new ReactiveVar(true);
},
setError(error) {
this.error.set(error);
},
setLoading(w) {
this.loading.set(w);
},
peopleList() {
this.users = Users.find({});
this.users.forEach((user) => {
console.log(JSON.stringify(user));
return Users.find({}, {
fields: {_id: true},
});
return this.users;
},
}).register('people');
Template.peopleRow.helpers({
userData() {
const userCollection = this.esSearch ? ESSearchResults : Users;
return userCollection.findOne(this.userId);
},
});
Template.editUserPopup.helpers({
user() {
return Users.findOne(this.userId);
},
});
BlazeComponent.extendComponent({
onCreated() {
},
user() {
return Users.findOne(this.userId);
},
events() {
return [{
'click a.edit-user': Popup.open('editUser'),
}];
},
}).register('peopleRow');
Template.editUserPopup.events({
submit(evt, tpl) {
evt.preventDefault();
const user = Users.findOne(this.userId);
const fullname = tpl.find('.js-profile-fullname').value.trim();
const username = tpl.find('.js-profile-username').value.trim();
const initials = tpl.find('.js-profile-initials').value.trim();
const isAdmin = tpl.find('.js-profile-isadmin').value.trim();
const email = tpl.find('.js-profile-email').value.trim();
console.log('isAdmin', isAdmin);
let isChangeUserName = false;
let isChangeEmail = false;
Users.update(this.userId, {
$set: {
'profile.fullname': fullname,
'profile.initials': initials,
'isAdmin': true,
},
});
isChangeUserName = username !== user.username;
isChangeEmail = email.toLowerCase() !== user.emails[0].address.toLowerCase();
if (isChangeUserName && isChangeEmail) {
Meteor.call('setUsernameAndEmail', username, email.toLowerCase(), function (error) {
const usernameMessageElement = tpl.$('.username-taken');
const emailMessageElement = tpl.$('.email-taken');
if (error) {
const errorElement = error.error;
if (errorElement === 'username-already-taken') {
usernameMessageElement.show();
emailMessageElement.hide();
} else if (errorElement === 'email-already-taken') {
usernameMessageElement.hide();
emailMessageElement.show();
}
} else {
usernameMessageElement.hide();
emailMessageElement.hide();
Popup.back();
}
});
} else if (isChangeUserName) {
Meteor.call('setUsername', username, function (error) {
const messageElement = tpl.$('.username-taken');
if (error) {
messageElement.show();
} else {
messageElement.hide();
Popup.back();
}
});
} else if (isChangeEmail) {
Meteor.call('setEmail', email.toLowerCase(), function (error) {
const messageElement = tpl.$('.email-taken');
if (error) {
messageElement.show();
} else {
messageElement.hide();
Popup.back();
}
});
} else Popup.back();
},
});