Skip to content

Commit

Permalink
Edit user in the mailing list from admin panel
Browse files Browse the repository at this point in the history
  • Loading branch information
Cellule committed Dec 9, 2014
1 parent eca3bef commit f58eb42
Show file tree
Hide file tree
Showing 13 changed files with 311 additions and 234 deletions.
130 changes: 110 additions & 20 deletions components/MailingListEditPanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ var MKPermissionMixin = require("mykoop-user/components/PermissionMixin");
var MKListModButtons = require("mykoop-core/components/ListModButtons");
var MKAlert = require("mykoop-core/components/Alert");
var MKFeedbacki18nMixin = require("mykoop-core/components/Feedbacki18nMixin");
var MKAbstractModal = require("mykoop-core/components/AbstractModal");
var MKUserList = require("mykoop-core/components/UserList");
var MKUserListWrapper = require("mykoop-core/components/UserListWrapper");

var validatePermissions = MKPermissionMixin.statics.validateUserPermissions;
var __ = require("language").__;
var _ = require("lodash");
var actions = require("actions");
Expand Down Expand Up @@ -50,6 +54,47 @@ var MailingListEditPanel = React.createClass({
};
},

canEditList: false,
canViewUsers: false,
canAddUsers: false,
canDeleteUsers: false,
canDeleteList: false,
componentWillMount: function () {
this.canEditList = validatePermissions({
mailinglists: {
update: true
}
});

this.canViewUsers = this.canEditList && validatePermissions({
mailinglists: {
users: {
view: true
}
}
});
this.canAddUsers = this.canViewUsers && validatePermissions({
mailinglists: {
users: {
add: true,
}
}
});
this.canDeleteUsers = this.canViewUsers && validatePermissions({
mailinglists: {
users: {
delete: true,
}
}
});

this.canDeleteList = validatePermissions({
mailinglists: {
delete: true
}
});
},

componentWillReceiveProps: function (nextProps) {
// update state if a new mailing is taking this spot
// except this spot was for a new mailing list
Expand Down Expand Up @@ -146,27 +191,46 @@ var MailingListEditPanel = React.createClass({
});
},

render: function() {
var self = this;

var validatePermissions = this.constructor.validateUserPermissions;

var canEditList = validatePermissions({
mailinglists: {
update: true
retrieveUsers: function(callback) {
var id = this.getField("id");
actions.mailinglist.listUsers({
i18nErrors: {},
data:{
id: id
}
}, function(err, res) {
callback(err, res && res.users);
});

var canDeleteList = validatePermissions({
mailinglists: {
delete: true
},
onAddUser: function(user, callback) {
var id = this.getField("id");
actions.user.mailinglist.register({
i18nErrors: {},
data: {
id: user.id,
idMailingLists: [id]
}
});
}, callback);
},
onDeleteUser: function(user, callback) {
var id = this.getField("id");
actions.user.mailinglist.unregister({
i18nErrors: {},
data: {
id: user.id,
idMailingLists: [id]
}
}, callback);
},

render: function() {
var self = this;


var showAtRegistration = !!this.props.showAtRegistrationLink.value;
var hasChanges = this.hasChanges();
var isNew = this.isNewMailingList();
var saveButton = canEditList && {
var saveButton = this.canEditList && {
icon: "save",
tooltip: {
text: __("save"),
Expand All @@ -179,7 +243,7 @@ var MailingListEditPanel = React.createClass({
},
callback: _.bind(this.saveChanges, this)
};
var editPermissionsButton = !showAtRegistration && canEditList && {
var editPermissionsButton = !showAtRegistration && this.canEditList && {
icon: "shield",
tooltip: {
text: __("mailinglist::editPermissionsTooltip"),
Expand All @@ -196,7 +260,7 @@ var MailingListEditPanel = React.createClass({
warningMessage: __("areYouSure"),
callback: this.resetMailingList
};
var deleteButton = canDeleteList && !hasChanges && {
var deleteButton = this.canDeleteList && !hasChanges && {
icon: isNew ? "remove" : "trash",
tooltip: {
text: __("remove"),
Expand All @@ -205,14 +269,37 @@ var MailingListEditPanel = React.createClass({
warningMessage: isNew ? __("areYouSure") : __("mailinglist::deleteMailingListWarning"),
callback: this.deleteMailingList
};
var userList = this.canViewUsers && (
<MKUserListWrapper
noAdd={!this.canAddUsers}
noDelete={!this.canDeleteUsers}
retrieveUsers={this.retrieveUsers}
onAddUser={this.onAddUser}
onDeleteUser={this.onDeleteUser}
/>
);
var editUsersButton = this.canViewUsers && {
icon: "users",
tooltip: {
text: __("mailinglist::editUsersInMailingList"),
overlayProps: {placement: "top"}
},
modalTrigger: (
<MKAbstractModal
title={__("mailinglist::userList")}
modalBody={userList}
/>
)
};
var buttonsConfig = _.compact([
saveButton,
editUsersButton,
editPermissionsButton,
deleteButton,
cancelButton
]);

var registrationButton = canEditList || isNew ? [{
var registrationButton = this.canEditList || isNew ? [{
icon:"check",
tooltip: {
text: __("mailinglist::showAtRegistrationTooltip"),
Expand All @@ -231,12 +318,15 @@ var MailingListEditPanel = React.createClass({
}
}] : [];

var minWidthClass = "list-mod-min-width-" +
(buttonsConfig.length + registrationButton.length + 2);

return (
<BSPanel className="mailingList-edit-min-height">
{this.renderFeedback()}
<BSGrid className="mailingListPanel" fluid>
<BSRow>
<BSCol md={4} className="pull-right mailingList-actions-buttons">
<BSCol md={4} className={"pull-right mailingList-actions-buttons " + minWidthClass}>
<MKListModButtons
className="pull-right"
buttons={buttonsConfig}
Expand All @@ -247,7 +337,7 @@ var MailingListEditPanel = React.createClass({
/>
</BSCol>
<BSCol xs={8} className="mailingList-name-form">
{canEditList ?
{this.canEditList ?
<BSInput
type="text"
label={__("name")}
Expand All @@ -260,7 +350,7 @@ var MailingListEditPanel = React.createClass({
</BSRow>
<BSRow>
<BSCol md={12}>
{canEditList ?
{this.canEditList ?
<BSInput
type="textarea"
className="resize-vertical"
Expand Down
2 changes: 1 addition & 1 deletion lib/classes/MailingList.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var MailingList = (function () {
this.id = dbResult.id;
this.name = dbResult.name;
this.description = dbResult.description;
this.permissions = module.deserializePermissions(dbResult.permissions);
this.permissions = module.deserializePermissions(dbResult.permissions || "{}");
this.showAtRegistration = dbResult.showAtRegistration;
}
MailingList.queryMailingListInfo = "SELECT\
Expand Down
4 changes: 2 additions & 2 deletions lib/classes/MailingList.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class MailingList implements MailingList.MailingList {
class MailingList implements mkmailinglist.MailingList {
static queryMailingListInfo = "SELECT\
idMailingList AS id,\
name,\
Expand All @@ -16,7 +16,7 @@ class MailingList implements MailingList.MailingList {
this.id = dbResult.id;
this.name = dbResult.name;
this.description = dbResult.description;
this.permissions = module.deserializePermissions(dbResult.permissions);
this.permissions = module.deserializePermissions(dbResult.permissions || "{}");
this.showAtRegistration = dbResult.showAtRegistration;
}
}
Expand Down
16 changes: 16 additions & 0 deletions lib/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ function attachControllers(binder) {
inRegistration: true
};
}));
// Get mailing for registration only
binder.attach({
endPoint: endpoints.mailinglist.listUsers,
permissions: {
mailinglists: {
read: true,
users: {
view: true,
}
}
}
}, binder.makeSimpleController(mailingList.getMailingListUsers, function (req) {
return {
id: parseInt(req.param("id")) || 0
};
}));
// Register a user to multiple mailing lists
binder.attach({
endPoint: endpoints.user.mailinglist.register,
Expand Down
39 changes: 31 additions & 8 deletions lib/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function attachControllers(
}
},
binder.makeSimpleController(mailingList.addMailingList, function (req: Express.Request) {
var params: MailingList.AddMailingList.Params = {
var params: mkmailinglist.AddMailingList.Params = {
showAtRegistration: !!req.param("showAtRegistration", false),
name: req.param("name"),
description: req.param("description"),
Expand All @@ -47,7 +47,7 @@ export function attachControllers(
}
},
binder.makeSimpleController(mailingList.updateMailingList, function (req: Express.Request) {
var params: MailingList.UpdateMailingList.Params = {
var params: mkmailinglist.UpdateMailingList.Params = {
id: parseInt(req.param("id")),
name: req.param("name"),
description: req.param("description"),
Expand All @@ -70,7 +70,7 @@ export function attachControllers(
}
},
binder.makeSimpleController(mailingList.deleteMailingList, function (req: Express.Request) {
var params: MailingList.DeleteMailingList.Params = {
var params: mkmailinglist.DeleteMailingList.Params = {
id: parseInt(req.param("id"))
};
return params;
Expand Down Expand Up @@ -125,7 +125,7 @@ export function attachControllers(
// Get mailing for registration only
binder.attach(
{endPoint: endpoints.mailinglist.registration},
binder.makeSimpleController<MailingList.GetMailingLists.Params>(
binder.makeSimpleController<mkmailinglist.GetMailingLists.Params>(
mailingList.getMailingLists,
function() {
return {
Expand All @@ -135,6 +135,29 @@ export function attachControllers(
)
);

// Get mailing for registration only
binder.attach(
{
endPoint: endpoints.mailinglist.listUsers,
permissions: {
mailinglists: {
read: true,
users: {
view: true,
}
}
}
},
binder.makeSimpleController<mkmailinglist.GetMailingListUsers.Params>(
mailingList.getMailingListUsers,
function(req) {
return {
id: parseInt(req.param("id")) || 0
};
}
)
);

// Register a user to multiple mailing lists
binder.attach(
{
Expand All @@ -149,7 +172,7 @@ export function attachControllers(
customPermissionDenied: validateCurrentUser
},
binder.makeSimpleController(mailingList.registerToMailingLists, function (req: Express.Request) {
var params: MailingList.RegisterToMailingLists.Params = {
var params: mkmailinglist.RegisterToMailingLists.Params = {
idMailingLists: _.map(req.param("idMailingLists"), function(id: string) {
return parseInt(id);
}),
Expand All @@ -173,7 +196,7 @@ export function attachControllers(
customPermissionDenied: validateCurrentUser
},
binder.makeSimpleController(mailingList.unregisterToMailingLists, function (req: Express.Request) {
var params: MailingList.RegisterToMailingLists.Params = {
var params: mkmailinglist.RegisterToMailingLists.Params = {
idMailingLists: _.map(req.param("idMailingLists"), function(id: string) {
return parseInt(id);
}),
Expand All @@ -198,7 +221,7 @@ export function attachControllers(
customPermissionDenied: validateCurrentUser
},
binder.makeSimpleController(mailingList.getUserMailingLists, function (req: Express.Request) {
var params: MailingList.GetUserMailingLists.Params = {
var params: mkmailinglist.GetUserMailingLists.Params = {
id: parseInt(req.param("id"))
};
return params;
Expand All @@ -215,7 +238,7 @@ export function attachControllers(
}
}
},
binder.makeSimpleController<MailingList.SendEmail.Params>(
binder.makeSimpleController<mkmailinglist.SendEmail.Params>(
mailingList.sendEmail,
function(req) {
return {
Expand Down
Loading

0 comments on commit f58eb42

Please sign in to comment.