Skip to content

Commit

Permalink
fix(accounts): references proper use of $state
Browse files Browse the repository at this point in the history
Corrects for the old method of using $stateParams to using the new
$transition$.params('to').  It also corrects the onEnter() state
definitions for the account_reference module.
  • Loading branch information
jniles committed Sep 22, 2020
1 parent 5334d0a commit a7ec4f8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<ol class="headercrumb">
<li class="static" translate>TREE.ACCOUNT_REFERENCE_MANAGEMENT</li>

<li ng-if="AccountReferenceModalCtrl.isCreating" class="title">
<li ng-if="AccountReferenceModalCtrl.isCreateState" class="title">
<span translate>ACCOUNT.REFERENCE.ADD_REFERENCE</span>
<label class="badge badge-warning" translate>FORM.LABELS.CREATE</label>
</li>
<li ng-if="!AccountReferenceModalCtrl.isCreating" class="title">
<li ng-if="!AccountReferenceModalCtrl.isCreateState" class="title">
<span translate>ACCOUNT.REFERENCE.UPDATE_REFERENCE</span>
<label class="badge badge-warning" translate>FORM.LABELS.UPDATE</label>
</li>
Expand Down Expand Up @@ -129,7 +129,7 @@
</div>

<div class="modal-footer">
<p id="account-reference-same" ng-if="AccountReferenceForm.$submitted && AccountReferenceForm.$pristine && !AccountReferenceModalCtrl.isCreating" class="text-warning">
<p id="account-reference-same" ng-if="AccountReferenceForm.$submitted && AccountReferenceForm.$pristine && !AccountReferenceModalCtrl.isCreateState" class="text-warning">
<i class="fa fa-warning"></i> <span translate>ACCOUNT.RECORD_SAME</span>
</p>

Expand Down
25 changes: 13 additions & 12 deletions client/src/modules/account_reference/account_reference.modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,33 @@ angular.module('bhima.controllers')

AccountReferenceModalController.$inject = [
'$state', 'AccountService', 'AccountReferenceService',
'NotifyService', 'appcache', 'FormatTreeDataService',
'NotifyService', 'appcache', 'FormatTreeDataService', 'params',
];

function AccountReferenceModalController($state, Accounts, AccountReferences, Notify, AppCache, FormatTreeData) {
function AccountReferenceModalController($state, Accounts, AccountReferences, Notify, AppCache, FormatTreeData, params) {
const vm = this;
const cache = AppCache('AccountReferenceModal');

vm.accountReference = {};
vm.stateParams = {};

// check if we are in the create state
vm.isCreateState = params.isCreateState;

// exposed methods
vm.submit = submit;
vm.closeModal = closeModal;
vm.clear = clear;
vm.onSelectAccountReferenceType = onSelectAccountReferenceType;

if ($state.params.creating || $state.params.id) {
cache.stateParams = $state.params;
vm.stateParams = cache.stateParams;
if (vm.isCreateState || params.id) {
cache.stateParams = params;
vm.params = cache.stateParams;
} else {
vm.stateParams = cache.stateParams;
vm.params = cache.stateParams;
}
vm.isCreating = vm.stateParams.creating;

if (!vm.isCreating) {
AccountReferences.read(vm.stateParams.id)
if (!vm.isCreateState) {
AccountReferences.read(vm.params.id)
.then(reference => {
vm.accountReference = reference;
})
Expand Down Expand Up @@ -65,13 +66,13 @@ function AccountReferenceModalController($state, Accounts, AccountReferences, No

if (accountReferenceForm.$pristine) { return null; }

const promise = (vm.isCreating)
const promise = (vm.isCreateState)
? AccountReferences.create(vm.accountReference)
: AccountReferences.update(vm.accountReference.id, vm.accountReference);

return promise
.then(() => {
const translateKey = (vm.isCreating) ? 'ACCOUNT.REFERENCE.CREATED' : 'ACCOUNT.REFERENCE.UPDATED';
const translateKey = (vm.isCreateState) ? 'ACCOUNT.REFERENCE.CREATED' : 'ACCOUNT.REFERENCE.UPDATED';
Notify.success(translateKey);
$state.go('account_reference.list', null, { reload : true });
})
Expand Down
14 changes: 6 additions & 8 deletions client/src/modules/account_reference/account_reference.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ angular.module('bhima.routes')
filters : [],
},
})

.state('account_reference.create', {
url : '/create',
params : {
creating : { value : true },
isCreateState : { value : true },
},
onEnter : ['$uibModal', accountReferenceModal],
onEnter : ['$uibModal', '$transition$', accountReferenceModal],
onExit : ['$uibModalStack', closeModal],
})
.state('account_reference.list', {
Expand All @@ -31,18 +30,17 @@ angular.module('bhima.routes')
params : {
id : null,
},
onEnter : ['$uibModal', accountReferenceModal],
onEnter : ['$uibModal', '$transition$', accountReferenceModal],
onExit : ['$uibModalStack', closeModal],
});
}]);

function accountReferenceModal($modal) {
function accountReferenceModal($modal, $transition) {
$modal.open({
keyboard : false,
backdrop : 'static',
templateUrl : 'modules/account_reference/account_reference.modal.html',
controller : 'AccountReferenceModalController as AccountReferenceModalCtrl',
});
resolve : { params : () => $transition.params('to') },
}).result.catch(angular.noop);
}

function closeModal(ModalStack) {
Expand Down
2 changes: 1 addition & 1 deletion client/src/modules/account_reference/search.modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
</ui-select-choices>
</ui-select>
</div>

<div class="form-group">
<div class="checkbox">
<label class="control-label">
Expand Down

0 comments on commit a7ec4f8

Please sign in to comment.