Skip to content

Commit

Permalink
changes record editor.editItemModal to return a promise even if its t…
Browse files Browse the repository at this point in the history
…he output of the delete item confirmation model
  • Loading branch information
fredkingham committed Dec 19, 2018
1 parent b2ba01c commit 2ee320e
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 93 deletions.
4 changes: 4 additions & 0 deletions changelog.md
@@ -1,3 +1,7 @@
### 0.12.1 (Minor Release)
* If an item is deleted from the edit item modal, RecordEditor.openEditItemModal will now resolve after the delete item modal is closed with 'deleted'


### 0.12.0 (Major Release)

#### Misc Changes
Expand Down
14 changes: 9 additions & 5 deletions opal/static/js/opal/controllers/edit_item.js
Expand Up @@ -77,17 +77,21 @@ angular.module('opal.controllers').controller(
});
};

$scope.delete = function(result){
$modalInstance.close(result);
var modal = $modal.open({
templateUrl: '/templates/modals/delete_item_confirmation.html/',
$scope.delete = function(){
var deferred = $q.defer();
$modalInstance.close(deferred.promise);
var deleteModal = $modal.open({
templateUrl: '/templates/delete_item_confirmation_modal.html',
controller: 'DeleteItemConfirmationCtrl',
resolve: {
item: function() {
return item;
}
}
});
deleteModal.result.then(function(result){
deferred.resolve(result)
});
};

//
Expand Down Expand Up @@ -122,7 +126,7 @@ angular.module('opal.controllers').controller(

$scope.undischarge = function() {
undischargeMoadal = $modal.open({
templateUrl: '/templates/modals/undischarge.html/',
templateUrl: '/templates/undischarge_modal.html',
controller: 'UndischargeCtrl',
resolve: {episode: function(){ return episode } }
}
Expand Down
68 changes: 32 additions & 36 deletions opal/static/js/opal/services/record_editor.js
Expand Up @@ -5,40 +5,6 @@ angular.module('opal.services').factory('RecordEditor', function(
var RecordEditor = function(episode){
var self = this;

self.deleteItem = function(name, iix){
var item = self.getItem(name, iix);
var deferred = $q.defer();

if (!angular.isDefined(item) || item.isReadOnly() || item.isSingleton()) {
// Cannot delete 'Add'
deferred.resolve();
return deferred.promise;
}

$rootScope.state = 'modal';

UserProfile.load().then(function(profile){
if(profile.readonly){
deferred.resolve();
return deferred.promise;
}

var modal = $modal.open({
templateUrl: '/templates/modals/delete_item_confirmation.html/',
controller: 'DeleteItemConfirmationCtrl',
resolve: {
item: function() { return item; },
profile: function(){ return profile; }
}
}).result.then(function(result) {
$rootScope.state = 'normal';
deferred.resolve(result);
});
});

return deferred.promise;
};

self.getItem = function(name, iix){
if (episode[name] && episode[name][iix] && episode[name][iix].columnName) {
return episode[name][iix];
Expand Down Expand Up @@ -78,14 +44,44 @@ angular.module('opal.services').factory('RecordEditor', function(
var modal = $modal.open(modal_opts);

modal.result.then(function(result) {
$rootScope.state = 'normal';
deferred.resolve(result);
$q.when(result).then(function(x){
$rootScope.state = 'normal';
deferred.resolve(result);
});
});
}
});

return deferred.promise;
};
self.deleteItem = function(name, iix){
var item = self.getItem(name, iix);
var deferred = $q.defer();
if (!angular.isDefined(item) || item.isReadOnly() || item.isSingleton()) {
// Cannot delete 'Add'
deferred.resolve();
return deferred.promise;
}
$rootScope.state = 'modal';
UserProfile.load().then(function(profile){
if(profile.readonly){
deferred.resolve();
return deferred.promise;
}
var modal = $modal.open({
templateUrl: '/templates/modals/delete_item_confirmation.html/',
controller: 'DeleteItemConfirmationCtrl',
resolve: {
item: function() { return item; },
profile: function(){ return profile; }
}
}).result.then(function(result) {
$rootScope.state = 'normal';
deferred.resolve(result);
});
});
return deferred.promise;
}

self.editItem = function(name, iix){
var item = self.getItem(name, iix);
Expand Down
28 changes: 27 additions & 1 deletion opal/static/js/test/edit_item.controller.test.js
Expand Up @@ -159,14 +159,40 @@ describe('EditItemCtrl', function (){

it('should open the delete modal', function() {
spyOn($modal, 'open');
$modal.open.and.returnValue({
result: {
then: function(x){
x("cancelled");
}
}
});
$scope.delete();
expect($modal.open).toHaveBeenCalled()
var args = $modal.open.calls.mostRecent().args[0];
expect(args.templateUrl).toEqual('/templates/modals/delete_item_confirmation.html/');
expect(args.templateUrl).toEqual('/templates/delete_item_confirmation_modal.html');
expect(args.controller).toEqual('DeleteItemConfirmationCtrl');
expect(args.resolve.item()).toEqual(item)
});

it('should return the output of the delete modal', function(){
spyOn($modal, 'open');
var promiseResolved = false;
$modal.open.and.returnValue({
result: {
then: function(x){
x("cancelled");
}
}
});
spyOn($q, "defer").and.returnValue({
resolve: function(result){
promiseResolved = result;
}
});

$scope.delete("delete");
expect(promiseResolved).toBe("cancelled");
});
});

describe('cancel()', function(){
Expand Down
83 changes: 32 additions & 51 deletions opal/static/js/test/record_editor_test.js
Expand Up @@ -203,63 +203,44 @@ describe('RecordEditor', function(){
expect($rootScope.state).toBe('normal');
});

});

describe('delete item', function(){
it('should open the DeleteItemConfirmationCtrl', function(){
var deferred, callArgs;
deferred = $q.defer();
spyOn($modal, 'open').and.returnValue({result: deferred.promise});
episode.recordEditor.deleteItem('diagnosis', 0);
$scope.$digest();
callArgs = $modal.open.calls.mostRecent().args;
expect(callArgs.length).toBe(1);
expect(callArgs[0].controller).toBe('DeleteItemConfirmationCtrl');
expect(callArgs[0].templateUrl).toBe(
'/templates/modals/delete_item_confirmation.html/'
);
var resolves = callArgs[0].resolve;
expect(resolves.item()).toEqual(episode.recordEditor.getItem('diagnosis', 0));
expect(resolves.profile(null)).toEqual(profile);
it("should handle the result of it is not a promise", function(){
var deferred;
var called = false

deferred = $q.defer();
spyOn($modal, 'open').and.returnValue({result: deferred.promise});
episode.recordEditor.editItem('demographics', 0).then(function(modalResult){
called = modalResult == "save";
});

describe('for a readonly user', function(){
beforeEach(function(){
profile.readonly = true;
});

it('should return just return an empty promise', function(){
var deferred, callArgs;
deferred = $q.defer();
spyOn($modal, 'open').and.returnValue({result: deferred.promise});
episode.recordEditor.deleteItem('diagnosis', 0);
$scope.$digest();
expect($modal.open.calls.count()).toBe(0);
});

afterEach(function(){
profile.readonly = false;
});
});
deferred.resolve('save');
$rootScope.$apply();

it('should change state to "normal" when the modal is closed', function() {
var deferred;
deferred = $q.defer();
spyOn($modal, 'open').and.returnValue({result: deferred.promise});
episode.recordEditor.deleteItem('diagnosis', 0);
deferred.resolve('save');
$scope.$digest();
});
expect($rootScope.state).toBe('normal');
expect(called).toBe(true);
});

it('should not delete singletons', function(){
var deferred, callArgs;
deferred = $q.defer();
spyOn($modal, 'open').and.returnValue({result: deferred.promise});
episode.recordEditor.deleteItem('demographics', 0);
$scope.$digest();
expect($modal.open.calls.count()).toBe(0);
it("should handle the result if it is a promise", function(){
var deferred, nestedDeferred;
var called = false

deferred = $q.defer();
nestedDeferred = $q.defer()

spyOn($modal, 'open').and.returnValue({result: deferred.promise});
episode.recordEditor.editItem('demographics', 0).then(function(modalResult){
called = modalResult == "delete";
});

nestedDeferred.resolve("delete")
deferred.resolve(nestedDeferred.promise);
$rootScope.$apply();

expect($rootScope.state).toBe('normal');
expect(called).toBe(true);
});
});

});

describe("get item", function(){
Expand Down

0 comments on commit 2ee320e

Please sign in to comment.