check amount with decimals
This commit is contained in:
parent
d8c3f9451d
commit
259ce7c798
3 changed files with 39 additions and 36 deletions
|
|
@ -48,32 +48,37 @@ angular.module('copayApp.directives')
|
|||
};
|
||||
}
|
||||
])
|
||||
.directive('enoughAmount', ['$rootScope',
|
||||
function($rootScope) {
|
||||
.directive('validAmount', ['$rootScope', '$locale',
|
||||
function($rootScope, locale) {
|
||||
var w = $rootScope.wallet;
|
||||
preconditions.checkState(w);
|
||||
preconditions.checkState(w.settings.unitToSatoshi);
|
||||
var formats = locale.NUMBER_FORMATS;
|
||||
|
||||
var feeSat = Number(bitcore.TransactionBuilder.FEE_PER_1000B_SAT);
|
||||
return {
|
||||
require: 'ngModel',
|
||||
link: function(scope, element, attrs, ctrl) {
|
||||
var val = function(value) {
|
||||
var availableBalanceNum = Number(($rootScope.availableBalance * w.settings.unitToSatoshi).toFixed(0));
|
||||
var vNum = Number((value * w.settings.unitToSatoshi).toFixed(0));
|
||||
|
||||
if (typeof value == 'undefined') {
|
||||
ctrl.$pristine = true;
|
||||
}
|
||||
|
||||
if (typeof vNum == "number" && vNum > 0) {
|
||||
vNum = vNum + feeSat;
|
||||
if (availableBalanceNum < vNum || isNaN(availableBalanceNum)) {
|
||||
ctrl.$setValidity('enoughAmount', false);
|
||||
scope.notEnoughAmount = true;
|
||||
var decimals = Number(w.settings.unitDecimals);
|
||||
var sep_index = ('' + value).indexOf(formats.DECIMAL_SEP);
|
||||
var str_value = ('' + value).substring(sep_index+1);
|
||||
if (sep_index > 0 && str_value.length > decimals) {
|
||||
ctrl.$setValidity('validAmount', false);
|
||||
scope.notValidAmount = true;
|
||||
} else {
|
||||
ctrl.$setValidity('enoughAmount', true);
|
||||
scope.notEnoughAmount = null;
|
||||
ctrl.$setValidity('validAmount', true);
|
||||
scope.notValidAmount = null;
|
||||
}
|
||||
} else {
|
||||
ctrl.$setValidity('enoughAmount', false);
|
||||
scope.notEnoughAmount = null;
|
||||
ctrl.$setValidity('validAmount', false);
|
||||
scope.notValidAmount = null;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,10 +62,9 @@ describe("Unit: Testing Directives", function() {
|
|||
describe('Unit: bits', function() {
|
||||
beforeEach(inject(function($compile, $rootScope) {
|
||||
$scope = $rootScope;
|
||||
$rootScope.availableBalance = 1000;
|
||||
var element = angular.element(
|
||||
'<form name="form">' +
|
||||
'<input type="number" id="amount" name="amount" placeholder="Amount" ng-model="amount" min="0.0001" max="10000000" enough-amount required>' +
|
||||
'<input type="number" id="amount" name="amount" placeholder="Amount" ng-model="amount" min="0.00000001" max="10000000000" valid-amount required>' +
|
||||
'</form>'
|
||||
);
|
||||
$scope.model = {
|
||||
|
|
@ -81,20 +80,19 @@ describe("Unit: Testing Directives", function() {
|
|||
form.amount.$setViewValue(800);
|
||||
expect(form.amount.$invalid).to.equal(false);
|
||||
form.amount.$setViewValue(900);
|
||||
expect($scope.notEnoughAmount).to.equal(null);
|
||||
expect($scope.notValidAmount).to.equal(null);
|
||||
form.amount.$setViewValue(0.44);
|
||||
expect($scope.notValidAmount).to.equal(null);
|
||||
|
||||
});
|
||||
|
||||
it('should not validate', function() {
|
||||
form.amount.$setViewValue(0);
|
||||
expect(form.amount.$invalid).to.equal(true);
|
||||
form.amount.$setViewValue(9999999999);
|
||||
form.amount.$setViewValue(999999999999);
|
||||
expect(form.amount.$invalid).to.equal(true);
|
||||
form.amount.$setViewValue(901);
|
||||
expect(form.amount.$invalid).to.equal(true);
|
||||
form.amount.$setViewValue(1000);
|
||||
expect(form.amount.$invalid).to.equal(true);
|
||||
form.amount.$setViewValue(901);
|
||||
expect($scope.notEnoughAmount).to.equal(true);
|
||||
form.amount.$setViewValue(0.333);
|
||||
expect($scope.notValidAmount).to.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -104,12 +102,13 @@ describe("Unit: Testing Directives", function() {
|
|||
var w = new FakeWallet(walletConfig);
|
||||
w.settings.unitToSatoshi = 100000000;
|
||||
w.settings.unitName = 'BTC';
|
||||
w.settings.unitDecimals = 8;
|
||||
$rootScope.wallet = w;
|
||||
|
||||
$rootScope.availableBalance = 0.04;
|
||||
var element = angular.element(
|
||||
'<form name="form">' +
|
||||
'<input type="number" id="amount" name="amount" placeholder="Amount" ng-model="amount" min="0.0001" max="10000000" enough-amount required>' +
|
||||
'<input type="number" id="amount" name="amount" placeholder="Amount" ng-model="amount" min="0.00000001" max="10000000000" valid-amount required>' +
|
||||
'</form>'
|
||||
);
|
||||
$scope.model = {
|
||||
|
|
@ -122,25 +121,24 @@ describe("Unit: Testing Directives", function() {
|
|||
|
||||
it('should validate', function() {
|
||||
form.amount.$setViewValue(0.01);
|
||||
expect($scope.notEnoughAmount).to.equal(null);
|
||||
expect($scope.notValidAmount).to.equal(null);
|
||||
expect(form.amount.$invalid).to.equal(false);
|
||||
form.amount.$setViewValue(0.039);
|
||||
expect($scope.notEnoughAmount).to.equal(null);
|
||||
expect($scope.notValidAmount).to.equal(null);
|
||||
expect(form.amount.$invalid).to.equal(false);
|
||||
form.amount.$setViewValue(100292.039);
|
||||
expect($scope.notValidAmount).to.equal(null);
|
||||
expect(form.amount.$invalid).to.equal(false);
|
||||
});
|
||||
|
||||
it('should not validate', function() {
|
||||
form.amount.$setViewValue(0.03999);
|
||||
expect($scope.notEnoughAmount).to.equal(true);
|
||||
form.amount.$setViewValue(0.039998888888888);
|
||||
expect($scope.notValidAmount).to.equal(true);
|
||||
expect(form.amount.$invalid).to.equal(true);
|
||||
form.amount.$setViewValue(0);
|
||||
expect(form.amount.$invalid).to.equal(true);
|
||||
form.amount.$setViewValue(0.0);
|
||||
expect(form.amount.$invalid).to.equal(true);
|
||||
form.amount.$setViewValue(0.05);
|
||||
expect($scope.notEnoughAmount).to.equal(true);
|
||||
expect(form.amount.$invalid).to.equal(true);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
|||
|
|
@ -61,19 +61,19 @@
|
|||
ng-show="!sendForm.amount.$invalid &&
|
||||
!sendForm.amount.$pristine">valid!</small>
|
||||
<small translate class="has-error"
|
||||
ng-show="sendForm.amount.$invalid &&
|
||||
!sendForm.amount.$pristine && !notEnoughAmount">not valid
|
||||
ng-show="(sendForm.amount.$invalid || notValidAmount) &&
|
||||
!sendForm.amount.$pristine">not valid
|
||||
</small>
|
||||
<small translate ng-show="notEnoughAmount" class="has-error">Insufficient funds</small>
|
||||
</label>
|
||||
<div class="small-9 columns">
|
||||
<input type="number" id="amount"
|
||||
ng-disabled="loading || ($root.merchant && +$root.merchant.total > 0) || $root.merchantError"
|
||||
name="amount" placeholder="{{'Amount'|translate}}" ng-model="amount"
|
||||
min="0.00000001" max="10000000000" enough-amount required
|
||||
min="0.00000001" max="10000000000" valid-amount required
|
||||
autocomplete="off">
|
||||
<small class="icon-input" ng-show="!sendForm.amount.$invalid && amount"><i class="fi-check"></i></small>
|
||||
<small class="icon-input" ng-show="sendForm.amount.$invalid && !sendForm.amount.$pristine && !notEnoughAmount"><i class="fi-x"></i></small>
|
||||
<small class="icon-input" ng-show="sendForm.amount.$invalid &&
|
||||
!sendForm.amount.$pristine && !notValidAmount"><i class="fi-x"></i></small>
|
||||
<a class="small input-note" title="{{'Send all funds'|translate}}"
|
||||
ng-show="$root.availableBalance > 0 && (!$root.merchant || +$root.merchant.total === 0)"
|
||||
ng-click="topAmount(sendForm)">
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue