2017-01-06 12:11:47 -05:00
'use strict' ;
angular . module ( 'copayApp.controllers' ) . controller ( 'preferencesBitpayServicesController' ,
function ( $rootScope , $scope , $state , $timeout , $ionicHistory , bitpayAccountService , bitpayCardService , popupService , gettextCatalog ) {
$scope . removeAccount = function ( account ) {
var title = gettextCatalog . getString ( 'Remove BitPay Account?' ) ;
2017-02-22 12:16:38 -05:00
var msg = gettextCatalog . getString ( 'Removing your BitPay account will remove all associated BitPay account data from this device. Are you sure you would like to remove your BitPay Account ({{email}}) from this device?' , {
2017-01-06 12:11:47 -05:00
email : account . email
} ) ;
popupService . showConfirm ( title , msg , null , null , function ( res ) {
if ( res ) {
removeAccount ( account ) ;
}
} ) ;
} ;
$scope . removeCard = function ( card ) {
var title = gettextCatalog . getString ( 'Remove BitPay Card?' ) ;
var msg = gettextCatalog . getString ( 'Are you sure you would like to remove your BitPay Card ({{lastFourDigits}}) from this device?' , {
lastFourDigits : card . lastFourDigits
} ) ;
popupService . showConfirm ( title , msg , null , null , function ( res ) {
if ( res ) {
removeCard ( card ) ;
}
} ) ;
} ;
var removeAccount = function ( account ) {
bitpayAccountService . removeAccount ( account , function ( err ) {
if ( err ) {
return popupService . showAlert ( gettextCatalog . getString ( 'Error' ) , gettextCatalog . getString ( 'Could not remove account' ) ) ;
}
setScope ( function ( ) {
// If there are no paired accounts then change views.
if ( $scope . bitpayAccounts . length == 0 ) {
$state . go ( 'tabs.settings' ) . then ( function ( ) {
$ionicHistory . clearHistory ( ) ;
$state . go ( 'tabs.home' ) ;
} ) ;
}
} ) ;
} ) ;
} ;
var removeCard = function ( card ) {
2017-02-09 14:58:47 -05:00
bitpayCardService . remove ( card . id , function ( err ) {
2017-01-06 12:11:47 -05:00
if ( err ) {
return popupService . showAlert ( gettextCatalog . getString ( 'Error' ) , gettextCatalog . getString ( 'Could not remove card' ) ) ;
}
setScope ( ) ;
} ) ;
} ;
var setScope = function ( cb ) {
2017-02-09 14:58:47 -05:00
bitpayAccountService . getAccounts ( function ( err , accounts ) {
2017-01-06 12:11:47 -05:00
if ( err ) return ;
2017-02-09 14:58:47 -05:00
$scope . bitpayAccounts = accounts ;
2017-01-06 12:11:47 -05:00
2017-02-09 14:58:47 -05:00
bitpayCardService . getCards ( function ( err , cards ) {
2017-01-06 12:11:47 -05:00
if ( err ) return ;
2017-02-09 14:58:47 -05:00
$scope . bitpayCards = cards ;
2017-01-06 12:11:47 -05:00
if ( cb ) {
cb ( ) ;
}
2017-02-22 12:16:38 -05:00
$timeout ( function ( ) {
$rootScope . $apply ( ) ;
} , 10 ) ;
2017-01-06 12:11:47 -05:00
} ) ;
} ) ;
} ;
$scope . $on ( "$ionicView.beforeEnter" , function ( event , data ) {
setScope ( ) ;
} ) ;
} ) ;