Wallet/src/js/services/sendFlowService.js

95 lines
2 KiB
JavaScript
Raw Normal View History

2018-08-08 16:35:49 +02:00
'use strict';
(function(){
2018-08-08 16:35:49 +02:00
angular
.module('copayApp.services')
.factory('sendFlowService', sendFlowService);
function sendFlowService($log) {
2018-08-08 17:27:15 +02:00
var service = {
amount: '',
fromWalletId: '',
sendMax: false,
thirdParty: null,
toAddress: '',
toWalletId: '',
previousStates: [],
2018-08-08 17:27:15 +02:00
// Functions
clear: clear,
getState: getState,
map: map,
popState: popState,
pushState: pushState,
startSend: startSend
};
return service;
function clear() {
console.log("sendFlow clear()");
2018-08-09 13:06:50 +12:00
clearCurrent();
service.previousStates = [];
}
function clearCurrent() {
console.log("sendFlow clearCurrent()");
service.amount = '';
service.fromWalletId = '';
service.sendMax = false;
service.thirdParty = null;
service.toAddress = '';
service.toWalletId = '';
}
2018-08-08 17:27:15 +02:00
/**
* Handy for debugging
*/
function getState() {
var currentState = {};
Object.keys(service).forEach(function forCurrentParam(key) {
if (typeof service[key] !== 'function' && key !== 'previousStates') {
currentState[key] = service[key];
}
});
return currentState;
}
/**
* Clears all previous state
*/
function startSend(params) {
console.log('startSend()');
clear();
map(params);
}
function map(params) {
Object.keys(params).forEach(function forNewParam(key) {
service[key] = params[key];
});
};
function popState() {
console.log('sendFlow pop');
if (service.previousStates.length) {
var params = service.previousStates.pop();
2018-08-09 13:06:50 +12:00
clearCurrent();
map(params);
} else {
clear();
}
};
function pushState(params) {
console.log('sendFlow push');
var currentParams = getState();
service.previousStates.push(currentParams);
2018-08-09 13:06:50 +12:00
clearCurrent();
map(params);
};
2018-08-08 17:10:47 +02:00
};
})();