Merge pull request from lbryio/set-default-callback

add callbacks to setDefaultAccount method
This commit is contained in:
Akinwale Ariwodola 2019-09-06 16:21:42 +01:00 committed by GitHub
commit c580288e35
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 21 deletions
dist
src/redux/actions

19
dist/bundle.es.js vendored
View file

@ -2076,7 +2076,7 @@ function doSetSync(oldHash, newHash, data) {
}); });
}; };
} }
function doSetDefaultAccount() { function doSetDefaultAccount(success, failure) {
return dispatch => { return dispatch => {
dispatch({ dispatch({
type: SET_DEFAULT_ACCOUNT type: SET_DEFAULT_ACCOUNT
@ -2105,7 +2105,24 @@ function doSetDefaultAccount() {
lbryRedux.Lbry.account_set({ lbryRedux.Lbry.account_set({
account_id: defaultId, account_id: defaultId,
default: true default: true
}).then(() => {
if (success) {
success();
}
}).catch(err => {
if (failure) {
failure(err);
}
}); });
} else {
// no default account to set
if (failure) {
failure('Could not set a default account'); // fail
}
}
}).catch(err => {
if (failure) {
failure(err);
} }
}); });
}; };

19
dist/bundle.js vendored
View file

@ -3556,7 +3556,7 @@ function doSetSync(oldHash, newHash, data) {
}); });
}; };
} }
function doSetDefaultAccount() { function doSetDefaultAccount(success, failure) {
return function (dispatch) { return function (dispatch) {
dispatch({ dispatch({
type: constants_action_types__WEBPACK_IMPORTED_MODULE_0__["SET_DEFAULT_ACCOUNT"] type: constants_action_types__WEBPACK_IMPORTED_MODULE_0__["SET_DEFAULT_ACCOUNT"]
@ -3583,7 +3583,24 @@ function doSetDefaultAccount() {
lbry_redux__WEBPACK_IMPORTED_MODULE_2__["Lbry"].account_set({ lbry_redux__WEBPACK_IMPORTED_MODULE_2__["Lbry"].account_set({
account_id: defaultId, account_id: defaultId,
"default": true "default": true
}).then(function () {
if (success) {
success();
}
})["catch"](function (err) {
if (failure) {
failure(err);
}
}); });
} else {
// no default account to set
if (failure) {
failure('Could not set a default account'); // fail
}
}
})["catch"](function (err) {
if (failure) {
failure(err);
} }
}); });
}; };

View file

@ -31,33 +31,54 @@ export function doSetSync(oldHash, newHash, data) {
}; };
} }
export function doSetDefaultAccount() { export function doSetDefaultAccount(success, failure) {
return dispatch => { return dispatch => {
dispatch({ dispatch({
type: ACTIONS.SET_DEFAULT_ACCOUNT, type: ACTIONS.SET_DEFAULT_ACCOUNT,
}); });
Lbry.account_list().then(accountList => { Lbry.account_list()
const { lbc_mainnet: accounts } = accountList; .then(accountList => {
let defaultId; const { lbc_mainnet: accounts } = accountList;
for (let i = 0; i < accounts.length; ++i) { let defaultId;
if (accounts[i].satoshis > 0) { for (let i = 0; i < accounts.length; ++i) {
defaultId = accounts[i].id; if (accounts[i].satoshis > 0) {
break; defaultId = accounts[i].id;
break;
}
} }
}
// In a case where there's no balance on either account // In a case where there's no balance on either account
// assume the second (which is created after sync) as default // assume the second (which is created after sync) as default
if (!defaultId && accounts.length > 1) { if (!defaultId && accounts.length > 1) {
defaultId = accounts[1].id; defaultId = accounts[1].id;
} }
// Set the default account // Set the default account
if (defaultId) { if (defaultId) {
Lbry.account_set({ account_id: defaultId, default: true }); Lbry.account_set({ account_id: defaultId, default: true })
} .then(() => {
}); if (success) {
success();
}
})
.catch(err => {
if (failure) {
failure(err);
}
});
} else {
// no default account to set
if (failure) {
failure('Could not set a default account'); // fail
}
}
})
.catch(err => {
if (failure) {
failure(err);
}
});
}; };
} }