Skip to content
This repository has been archived by the owner on Oct 20, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(plan): better http redirections after API plan operations
  • Loading branch information
tcompiegne committed Jan 9, 2017
1 parent d4094d3 commit 90e2ae8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
33 changes: 29 additions & 4 deletions src/app/api/admin/plans/apiPlans.controller.js
Expand Up @@ -14,13 +14,14 @@
* limitations under the License.
*/
class ApiPlansController {
constructor(resolvedPlans, $mdSidenav, $mdDialog, $scope, ApiService, $stateParams, NotificationService, dragularService) {
constructor(resolvedPlans, $mdSidenav, $mdDialog, $scope, ApiService, $state, $stateParams, NotificationService, dragularService) {
'ngInject';
this.plans = resolvedPlans.data;
this.$mdSidenav = $mdSidenav;
this.$mdDialog = $mdDialog;
this.$scope = $scope;
this.ApiService = ApiService;
this.$state = $state;
this.$stateParams = $stateParams;
this.NotificationService = NotificationService;
this.DragularService = dragularService;
Expand All @@ -40,7 +41,15 @@ class ApiPlansController {

this.countByStatus = {};
this.resetPlan();
this.applyFilters();
if ($stateParams.state) {
if (_.includes(this.statusFilters, $stateParams.state)) {
this.changeFilter($stateParams.state);
} else {
this.applyFilters();
}
} else {
this.applyFilters();
}

var that = this;
$scope.configure = function (plan) {
Expand Down Expand Up @@ -122,6 +131,7 @@ class ApiPlansController {
} else {
this.selectedStatus.push(statusFilter);
}
this.$state.transitionTo('apis.admin.plans', { 'apiId': this.$stateParams.apiId, 'state': statusFilter }, {notify: false});
this.applyFilters();
}

Expand Down Expand Up @@ -198,18 +208,29 @@ class ApiPlansController {
});
}

that.ApiService.savePlan(that.$stateParams.apiId, that.$scope.plan).then(function () {
that.ApiService.savePlan(that.$stateParams.apiId, that.$scope.plan).then(function (response) {
var createMode = !that.planAlreadyCreated(response.data.id);
that.$scope.$parent.apiCtrl.checkAPISynchronization({id: that.$stateParams.apiId});
that.NotificationService.show('The plan ' + that.$scope.plan.name + ' has been saved with success');
that.ApiService.getApiPlans(that.$stateParams.apiId).then(function (response) {
that.plans = response.data;
that.$mdSidenav('plan-edit').toggle();
that.$mdSidenav('live-preview').toggle();
that.applyFilters();
if (createMode) {
that.changeFilter('staging');
} else {
that.applyFilters();
}
});
});
}

planAlreadyCreated(planId) {
return _.some(this.plans, function (plan) {
return plan.id === planId;
});
}

close(plan, ev) {
var _this = this;

Expand All @@ -226,6 +247,9 @@ class ApiPlansController {
}).then(function (plan) {
if (plan) {
_this.list();
if (plan.isClosed) {
_this.changeFilter('closed');
}
}
}, function() {
// You cancelled the dialog
Expand All @@ -247,6 +271,7 @@ class ApiPlansController {
}).then(function (plan) {
if (plan) {
_this.list();
_this.changeFilter('published');
}
}, function() {
// You cancelled the dialog
Expand Down
4 changes: 4 additions & 0 deletions src/app/api/admin/plans/closePlanDialog.controller.js
Expand Up @@ -26,13 +26,17 @@ function DialogClosePlanController($scope, $mdDialog, ApiService, NotificationSe

$scope.close = function () {
if ($scope.plan.security === 'api_key' && $scope.subscriptions === 0) {
$scope.plan.isDeleted = true;
$scope.plan.isClosed = false;
ApiService.deletePlan($scope.apiId, $scope.plan.id).then(function() {
NotificationService.show('Plan ' + plan.name + ' has been deleted');
}).catch(function (error) {
NotificationService.show('Error while deleting plan ' + plan.name);
$scope.error = error;
});
} else {
$scope.plan.isDeleted = false;
$scope.plan.isClosed = true;
ApiService.closePlan($scope.apiId, $scope.plan.id).then(function() {
NotificationService.show('Plan ' + plan.name + ' has been closed');
}).catch(function (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/index.route.js
Expand Up @@ -153,7 +153,7 @@ function routerConfig($stateProvider, $urlRouterProvider) {
controllerAs: 'endpointCtrl'
})
.state('apis.admin.plans', {
url: '/plans',
url: '/plans?state',
templateUrl: 'app/api/admin/plans/apiPlans.html',
controller: 'ApiPlansController',
controllerAs: 'apiPlansCtrl',
Expand Down

0 comments on commit 90e2ae8

Please sign in to comment.