From ad9cdbd622ee296578179742330d616c5727366a Mon Sep 17 00:00:00 2001 From: Ivan Tivonenko Date: Thu, 9 Apr 2015 19:44:12 +0300 Subject: [PATCH] [FIX] Trade: fix trade pairs order edit (RT-3343) Fix editing trade pairs order. Clean trade pairs list - remove $$hashKey, remove duplicates. Check trade pair before adding. --- src/js/services/id.service.js | 21 +++++++++++++++++ src/js/tabs/settingstrade.controller.js | 31 +++++-------------------- src/js/tabs/trade.controller.js | 10 +++++--- src/templates/tabs/settingstrade.jade | 2 +- 4 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/js/services/id.service.js b/src/js/services/id.service.js index efe94d7c4..4176e35d7 100644 --- a/src/js/services/id.service.js +++ b/src/js/services/id.service.js @@ -198,6 +198,27 @@ module.factory('rpId', ['$rootScope', '$location', '$route', '$routeParams', '$t $scope.userBlob.unset('/trade_currency_pairs'); } + var tradeCurrencyPairs = settings.getSetting($scope.userBlob, 'trade_currency_pairs', []); + if (_.isArray(tradeCurrencyPairs) && tradeCurrencyPairs.length > 0) { + var changed = false; + if (_.find(tradeCurrencyPairs, _.partial(_.has, _, '$$hashKey'))) { + // clear $$hashKey + tradeCurrencyPairs = angular.fromJson(angular.toJson(tradeCurrencyPairs)); + changed = true; + } + var tradeCurrencyPairsUniq = _.uniq(tradeCurrencyPairs, false, function(o) { + return o.name; + }); + if (tradeCurrencyPairsUniq.length !== tradeCurrencyPairs.length) { + tradeCurrencyPairs = tradeCurrencyPairsUniq; + changed = true; + } + + if (changed) { + $scope.userBlob.set('/clients/rippletradecom/trade_currency_pairs', tradeCurrencyPairs); + } + } + if (_.has(d, 'txQueue')) { $scope.userBlob.set('/clients/rippletradecom/txQueue', d.txQueue); $scope.userBlob.unset('/txQueue'); diff --git a/src/js/tabs/settingstrade.controller.js b/src/js/tabs/settingstrade.controller.js index 6ab493756..97b0b04e2 100644 --- a/src/js/tabs/settingstrade.controller.js +++ b/src/js/tabs/settingstrade.controller.js @@ -8,8 +8,7 @@ var util = require('util'), settings = require('../util/settings'), Tab = require('../client/tab').Tab; -var SettingsTradeTab = function () -{ +var SettingsTradeTab = function() { Tab.call(this); }; @@ -24,9 +23,6 @@ SettingsTradeTab.prototype.angular = function(module) { SettingsTradeCtrl.$inject = ['$scope', '$timeout']; function SettingsTradeCtrl($scope, $timeout) { - var dirty = false, - saveTimeout; - if ($scope.userBlob.data && $scope.userCredentials.username) { $scope.pairs = settings.getSetting($scope.userBlob, 'trade_currency_pairs'); } else { @@ -40,8 +36,7 @@ SettingsTradeTab.prototype.angular = function(module) { $scope.deletePair = function(index){ for (var i = 0; i < $scope.pairs.length; i++) { - if ($scope.pairs[i].$$hashKey === this.entry.$$hashKey) { - saveTradePairs(); + if ($scope.pairs[i].name === this.entry.name) { $scope.userBlob.unset('/clients/rippletradecom/trade_currency_pairs/' + index); return; } @@ -50,26 +45,12 @@ SettingsTradeTab.prototype.angular = function(module) { $scope.dragControlListeners = { orderChanged: function(event) { - dirty = true; - if (saveTimeout) { - $timeout.cancel(saveTimeout); - } - saveTimeout = $timeout(saveTradePairs, 2000); + var sourceObj = _.clone($scope.pairs[event.source.index]); + var destObj = _.clone($scope.pairs[event.dest.index]); + $scope.userBlob.set('/clients/rippletradecom/trade_currency_pairs/' + event.source.index, sourceObj); + $scope.userBlob.set('/clients/rippletradecom/trade_currency_pairs/' + event.dest.index, destObj); } }; - - $scope.$on('$destroy', saveTradePairs); - - function saveTradePairs() { - if (dirty) { - $timeout.cancel(saveTimeout); - saveTimeout = null; - // clear $$hashKey - var pairs = angular.fromJson(angular.toJson($scope.pairs)); - $scope.userBlob.set('/clients/rippletradecom/trade_currency_pairs', pairs); - dirty = false; - } - } } }; diff --git a/src/js/tabs/trade.controller.js b/src/js/tabs/trade.controller.js index 1bd064b28..a8a279dbb 100644 --- a/src/js/tabs/trade.controller.js +++ b/src/js/tabs/trade.controller.js @@ -927,7 +927,7 @@ TradeTab.prototype.angular = function(module) } }); - $scope.open_custom_currency_selector = function () { + $scope.open_custom_currency_selector = function() { $scope.first_currency_selected = ''; $scope.first_issuer_selected = ''; $scope.second_currency_selected = ''; @@ -935,14 +935,18 @@ TradeTab.prototype.angular = function(module) $scope.adding_pair = true; } - $scope.add_pair = function () { + $scope.add_pair = function() { var formattedIssuerFirst = $scope.first_currency_selected === 'XRP' ? '' : '.' + $scope.first_issuer_selected; var formattedIssuerSecond = $scope.second_currency_selected === 'XRP' ? '' : '.' + $scope.second_issuer_selected; + if (($scope.second_currency_selected !== 'XRP' && ($scope.second_issuer_selected == null || $scope.second_issuer_selected === '')) || + ($scope.first_currency_selected !== 'XRP' && ($scope.first_issuer_selected == null || $scope.first_issuer_selected === ''))) { + // this could happen if gate is not validated + return; + } $scope.order.currency_pair = $scope.first_currency_selected + formattedIssuerFirst + '/' + $scope.second_currency_selected + formattedIssuerSecond; $scope.userBlob.unshift('/clients/rippletradecom/trade_currency_pairs', { name: $scope.order.currency_pair }); - $scope.userBlob.set('/clients/rippletradecom/trade_currency_pairs', $scope.pairs_query); $scope.adding_pair = false; }; diff --git a/src/templates/tabs/settingstrade.jade b/src/templates/tabs/settingstrade.jade index d79032396..5df2de27e 100644 --- a/src/templates/tabs/settingstrade.jade +++ b/src/templates/tabs/settingstrade.jade @@ -18,7 +18,7 @@ section.col-xs-12.content(ng-controller="SettingsTradeCtrl") | section. div(ng-if="pairs") div(as-sortable="dragControlListeners", ng-model="pairs") - div.pair(ng-repeat="entry in pairs", as-sortable-item) + div.pair(ng-repeat="entry in pairs track by entry.name", as-sortable-item) .col-xs-12(as-sortable-item-handle) i.grip span.description(ng-bind="entry.name")