'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 + '\''; } } } }; });