Skip to content

Instantly share code, notes, and snippets.

@aantipov
Created July 4, 2015 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aantipov/8797ebe8ab75dfb1d30c to your computer and use it in GitHub Desktop.
Save aantipov/8797ebe8ab75dfb1d30c to your computer and use it in GitHub Desktop.
An example of angular.js controller written in ES6
const _deps = [
'$scope',
'$log',
'$state',
'notify',
'GuinnessApi',
'campaign',
'campaignUsers',
'teamMembers',
'user'
];
class ManageTeamCtrl {
constructor(...args) {
// Publish each service on 'this' with '_' prefix.
_deps.map((val) => '_' + val).forEach((val, key) => this[val] = args[key]);
this._$log.debug('CampaignManageTeamCtrl initialization');
this.actionPromise = false;
this.sortFields = [
{title: 'First name', value: 'extended_user.user.first_name'},
{title: 'Last name', value: 'extended_user.user.last_name'},
];
this.campaignUsers = this._campaignUsers;
this.availableSort = this.sortFields[0];
this.availableSortDir = true;
this.addedSort = this.sortFields[0];
this.addedSortDir = true;
this._currentUserId = this._user.getUserInfo().id;
}
/**
* Check if a user is current user.
*
* @param campaingUser
* @return {boolean}
*/
isItMe(campaingUser) {
return campaingUser.extended_user.user.id === this._currentUserId;
}
/**
* Remove current user from the campaign.
*
*/
removeMyself() {
// Check if there are other users.
if (this._campaignUsers.length < 2) {
this._notify.warning('You can\'t remove yourself because you are the only user assigned to the company');
return;
}
// Get campaign user id.
const campaignUser = _.find(this._campaignUsers, this.isItMe.bind(this));
let successMsg = `You have been successfully removed from campaign <strong>${this._campaign.name}</strong>`;
// Remove it from the campaign and redirect to my campaigns list.
this.actionPromise = this._GuinnessApi.removeCampaignUser({id: campaignUser.id})
.then(() => {
this._$scope.$close(true);
return this._$state.go('my_campaign');
})
.then(() => this._notify.success(successMsg));
}
/**
* Add new team member to the campaign.
*
* @param teamMember
*/
addTeamMember(teamMember) {
this.actionPromise = this._GuinnessApi.addCampaignUser({
campaign: this._campaign.resource_uri,
extended_user: teamMember.extended_user.resource_uri,
})
.then((campaignMember) => {
this._campaignUsers.push(campaignMember);
});
}
/**
* Get available team members list.
*
* Those, that are not included to the campaign.
*/
getAvailableTeamMembers() {
return _.filter(this._teamMembers, (teamMember) => {
return !this._isUserInCampaign(teamMember);
});
}
/**
* Check if a user is already in the campaign.
*
* @param teamMember
* @return {boolean}
* @private
*/
_isUserInCampaign(teamMember) {
return !!_.find(this.campaignUsers, (user) => {
return user.extended_user.user.id === teamMember.extended_user.user.id;
});
}
}
ManageTeamCtrl.$inject = _deps;
export default ManageTeamCtrl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment