Skip to content

Commit

Permalink
fix(depots): fix required error message
Browse files Browse the repository at this point in the history
Adds in a required error message when a user has left the depots to
transfer to blank when they are required by the system.

Closes #5623.
  • Loading branch information
jniles committed May 10, 2021
1 parent e6d9651 commit ae32665
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
33 changes: 12 additions & 21 deletions client/src/js/components/bhDepotSelectSearch/bhDepotSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ angular.module('bhima.components')
controller : DepotSearchSelectController,
bindings : {
depotsUuids : '<?',
required : '<',
required : '<?',
label : '@?',
id : '@?',
onChange : '&',
formName : '@?',
},
});

Expand All @@ -26,10 +25,8 @@ function DepotSearchSelectController(Depots, uuidService) {
$ctrl.$onInit = () => {
// label to display
$ctrl.label = $ctrl.label || 'STOCK.DEPOT';
// default for form name
$ctrl.formName = $ctrl.formName || 'DepotSelectForm';
//
$ctrl.depotsSected = $ctrl.depotsSected || [];
$ctrl.depotsSelected = $ctrl.depotsSelected || [];
// init the model
$ctrl.depotsUuids = $ctrl.depotsUuids || [];
$ctrl.componentId = $ctrl.id || uuidService().replace('-', '');
Expand All @@ -45,42 +42,36 @@ function DepotSearchSelectController(Depots, uuidService) {
limit : 10,
text,
}).then(depots => {
const selectedUuids = $ctrl.depotsSected.map(d => {
return d.uuid;
});
return depots.filter(d => {
return !selectedUuids.includes(d.uuid);
});
const selectedUuids = $ctrl.depotsSelected.map(d => d.uuid);
return depots.filter(d => !selectedUuids.includes(d.uuid));
});
};

// on select a depot from the typehead
$ctrl.onSelect = (item) => {
$ctrl.depotsSected.push(angular.copy(item));
$ctrl.depotsSelected.push(angular.copy(item));
$ctrl.handleChange();
delete $ctrl.depotSelected;
};

// fires the onChange bound to the component boundary
$ctrl.handleChange = () => {
const depots = $ctrl.depotsSected.map(d => {
return d.uuid;
});
const depots = $ctrl.depotsSelected.map(d => d.uuid);
$ctrl.onChange({ depots });
};

// remove a selected depot
$ctrl.remove = (uuid) => {
$ctrl.depotsSected = $ctrl.depotsSected.filter(depot => {
return depot.uuid !== uuid;
});
$ctrl.depotsSelected = $ctrl.depotsSelected.filter(depot => depot.uuid !== uuid);
$ctrl.handleChange();
};

function loadSelected(depotsUuids) {
if (!depotsUuids || !(depotsUuids.length > 0)) return;
Depots.read(null, { uuids : depotsUuids }).then(depots => {
$ctrl.depotsSected = depots;
});

Depots.read(null, { uuids : depotsUuids })
.then(depots => {
$ctrl.depotsSelected = depots;
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div id="selectForm" ng-form="$ctrl.formName" bh-multiple-depot-search-select>
<div id="selectForm" ng-form="DepotSelectForm" bh-multiple-depot-search-select>

<div class="form-group">
<span ng-repeat = "depot in $ctrl.depotsSected">
Expand All @@ -9,9 +9,12 @@
</span>
</div>

<div class="form-group">
<label for="depots" translate>FORM.BUTTONS.ADD</label>
<input name="depots" id="{{$ctrl.componentId}}" type="text"
<div class="form-group" ng-class="{'has-error' : DepotSelectForm.depots.$invalid && DepotSelectForm.$submitted}">
<label class="control-label" for="depots" translate>FORM.BUTTONS.ADD</label>
<input
name="depots"
id="{{$ctrl.componentId}}"
type="text"
translate-attr ="{ 'placeholder' : 'DEPOT.MODAL.TYPE_DEPOT_NAME'}"
uib-typeahead="depot as depot.text for depot in $ctrl.loadDepots($viewValue) | limitTo:8"
typeahead-on-select="$ctrl.onSelect($item)"
Expand All @@ -21,5 +24,8 @@
ng-required="$ctrl.required"
ng-model="$ctrl.depotSelected"
class="form-control"/>
<div class="help-block" ng-show="DepotSelectForm.$submitted" ng-messages="DepotSelectForm.depots.$error">
<div ng-messages-include="modules/templates/messages.tmpl.html"></div>
</div>
<div>
</div>
5 changes: 3 additions & 2 deletions client/src/modules/depots/modals/depot.modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@
<p ng-if="!DepotModalCtrl.depot.allowed_distribution_depots.length"><i translate>DEPOT.NO_DEPOT</i></p>
<bh-depot-search-select label="STOCK.DEPOT" id="distribution_depots"
depots-uuids="DepotModalCtrl.depot.allowed_distribution_depots"
on-change="DepotModalCtrl.onDistributionDepotChange(depots)" form-name="DepotForm">
on-change="DepotModalCtrl.onDistributionDepotChange(depots)"
required="DepotModalCtrl.enable_strict_depot_distribution">
</bh-depot-search-select>
</div>

Expand Down Expand Up @@ -190,4 +191,4 @@
<span translate>FORM.BUTTONS.SUBMIT</span>
</bh-loading-button>
</div>
</form>
</form>

0 comments on commit ae32665

Please sign in to comment.