Skip to content

Commit

Permalink
wip(stock): exit form validation works
Browse files Browse the repository at this point in the history
This commit fixes the exit form line-by-line validation so it works
as expected.  Each line self-validates with each condition when a change
is made.  We are also able to add/remove lines as needed.
  • Loading branch information
jniles committed Mar 4, 2022
1 parent 8886e52 commit 1e0e325
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
5 changes: 3 additions & 2 deletions client/src/modules/stock/LotItem.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ function LotItemService(uuid, $translate) {
const hasLot = this.hasLotInformation();
const isPositive = this.hasPositiveQuantity();
const isUsed = !this.isUnused();
const hasEnough = this.hasEnoughQuantityAvailable();
const isNotExpired = !this.isExpired(date);

// add in the tooltip
Expand All @@ -107,6 +108,7 @@ function LotItemService(uuid, $translate) {
&& hasLot
&& isPositive
&& isUsed
&& hasEnough
&& isNotExpired;
};

Expand Down Expand Up @@ -248,9 +250,8 @@ function LotItemService(uuid, $translate) {
Lot.prototype.configure = function configure(item) {
this._initialised = true;

// console.log('#configure()', item);

const clone = { ...item };
delete clone.uuid;

if (clone.tracking_consumption !== undefined) {
this.setTrackingConsumption(clone.tracking_consumption);
Expand Down
25 changes: 16 additions & 9 deletions client/src/modules/stock/StockExitForm.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,22 +336,26 @@ function StockExitFormService(Store, AppCache, Session, $timeout, bhConstants, D

row._quantity_available = item._quantity_available;

this.validate();

// since we automatically select the first lot, we use set it as "used"
this._pool.use(row.lot_uuid);

this.validate();
};

/**
* @method removeItem
*
* @description
* This method removes an item from the ui-grid by its index.
* This method removes an item from the ui-grid by its uuid.
*/
StockExitForm.prototype.removeItem = function removeItem(index) {
const item = this.store.remove(index);
StockExitForm.prototype.removeItem = function removeItem(uuid) {
const lot = this.store.get(uuid);
this.store.remove(uuid);

// return the lot to the pool
this._pool.release(lot.lot_uuid);

this.validate();
return item;
};

/**
Expand All @@ -378,7 +382,7 @@ function StockExitFormService(Store, AppCache, Session, $timeout, bhConstants, D
// validate() is only set up to test on submission as it checks the validity
// of individual items which will not have been configured, manually
// reset error state
this._errors.clear();
delete this._errors;
});
};

Expand Down Expand Up @@ -440,7 +444,7 @@ function StockExitFormService(Store, AppCache, Session, $timeout, bhConstants, D
* @description
* Check if the form is valid or contains errors. This is done by:
*
* 1. TODO(@jniles) - checking if all data is finished loading
* 1. TODO(@jniles) - check if all data is finished loading
* 2. Running the validate function on each stock item.
* 3. Checking if globally required items (date, etc) are set.
*
Expand All @@ -459,7 +463,10 @@ function StockExitFormService(Store, AppCache, Session, $timeout, bhConstants, D

// gather errors into a flat array
this._errors = this.store.data
.flatMap(row => row.errors())
.flatMap(row => {
row.validate();
return row.errors();
})
.filter(err => err);

const hasDestination = !!this.details.entity_uuid;
Expand Down
27 changes: 10 additions & 17 deletions client/src/modules/stock/exit/exit.html
Original file line number Diff line number Diff line change
Expand Up @@ -165,25 +165,18 @@ <h4 translate>STOCK.PRODUCT_NOT_AVAILABLE</h4>

<!-- footer -->
<div class="row" style="margin-top: 5px;">
<div class="col-xs-12">
<div class="col-md-6">
<div class="col-xs-12">
<div class="text-info" ng-show="!StockCtrl.stockForm.details.exit_type">
<span class="fa fa-info-circle"></span>
<span translate>FORM.INFO.NO_DESTINATION</span>
</div>
</div>
</div>
<div class="col-md-6">
<div uib-alert ng-repeat="error in StockCtrl.stockForm.errors" class="alert-danger">{{error}}</div>
</div>

<div class="col-md-6 text-right">
<button class="btn btn-default" ng-click="StockCtrl.stockForm.clear()" type="button" translate>
FORM.BUTTONS.CLEAR
</button>
<div class="col-md-6 text-right">
<button class="btn btn-default" ng-click="StockCtrl.stockForm.clear()" type="button" translate>
FORM.BUTTONS.CLEAR
</button>

<bh-loading-button loading-state="StockExitForm.$loading" disabled="!StockCtrl.stockForm.validate()">
<span translate>FORM.BUTTONS.SUBMIT</span>
</bh-loading-button>
</div>
<bh-loading-button loading-state="StockExitForm.$loading" disabled="!StockCtrl.stockForm.validate()">
<span translate>FORM.BUTTONS.SUBMIT</span>
</bh-loading-button>
</div>
</div>
</form>
Expand Down
2 changes: 1 addition & 1 deletion client/src/modules/stock/exit/templates/actions.tmpl.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div class="ui-grid-cell-contents text-center">
<a class="text-action" ng-click="grid.appScope.removeItem(row.entity)" tabindex="-1"><i class="fa fa-trash-o text-danger"></i></a>
<a class="text-action" ng-click="grid.appScope.stockForm.removeItem(row.entity.uuid)" tabindex="-1"><i class="fa fa-trash-o text-danger"></i></a>
</div>

0 comments on commit 1e0e325

Please sign in to comment.