Skip to content

Instantly share code, notes, and snippets.

@aantipov
Created October 7, 2014 04:54
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/0492e88b5b8508b29d21 to your computer and use it in GitHub Desktop.
Save aantipov/0492e88b5b8508b29d21 to your computer and use it in GitHub Desktop.
'use strict';
angular.module('convertiser.components.manager')
.directive('changeStatus',
function ($state,
$modal,
gettextCatalog,
flash,
alertHelper,
managersService,
advertisersService,
publishersService) {
return {
require: '^profile',
scope: {
action: '@changeStatus'
},
link: function ($scope, element, attrs, profileController) {
element.on('click', function (event) {
event.preventDefault();
$scope.$apply(showModal);
});
function showModal(scope) {
var modal = $modal({
scope: scope,
show: true,
placement: 'center',
template: '/components/manager/accounts/changeStatus.html'
});
var profile = profileController.getProfile();
modal.$scope.remove = function () {
getAccountService().delete(profile).then(success, error);
};
modal.$scope.changeStatus = function (status) {
getAccountService()[status](profile).then(success, error);
};
function success() {
var state = 'manager.accounts.' + profileController.getProfileType() + 's.item';
// Flash success message and redirect to track domains list.
flash.setSuccessMsg(gettextCatalog.getString('Account status was successfully updated'));
// Hide modal.
setTimeout(modal.hide);
// Reload state.
$state.go(state, {id: profile.id}, {inherit: false, reload: true});
}
function error(response) {
// Hide modal.
setTimeout(modal.hide);
// Display alert with error msg.
alertHelper.error((response.data && response.data.detail) || gettextCatalog.getString('Some error occurred'));
}
}
function getAccountService() {
var accountType = profileController.getProfileType();
switch (accountType) {
case 'manager':
return managersService;
case 'advertiser':
return advertisersService;
case 'publisher':
return publishersService;
default:
throw 'profileController.getProfileType() return wrong acount type \'' + accountType + '\'';
}
}
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment