Merge pull request #55 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

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 => {
dispatch({
type: SET_DEFAULT_ACCOUNT
@ -2105,7 +2105,24 @@ function doSetDefaultAccount() {
lbryRedux.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);
}
});
};

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) {
dispatch({
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({
account_id: defaultId,
"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 => {
dispatch({
type: ACTIONS.SET_DEFAULT_ACCOUNT,
});
Lbry.account_list().then(accountList => {
const { lbc_mainnet: accounts } = accountList;
let defaultId;
for (let i = 0; i < accounts.length; ++i) {
if (accounts[i].satoshis > 0) {
defaultId = accounts[i].id;
break;
Lbry.account_list()
.then(accountList => {
const { lbc_mainnet: accounts } = accountList;
let defaultId;
for (let i = 0; i < accounts.length; ++i) {
if (accounts[i].satoshis > 0) {
defaultId = accounts[i].id;
break;
}
}
}
// In a case where there's no balance on either account
// assume the second (which is created after sync) as default
if (!defaultId && accounts.length > 1) {
defaultId = accounts[1].id;
}
// In a case where there's no balance on either account
// assume the second (which is created after sync) as default
if (!defaultId && accounts.length > 1) {
defaultId = accounts[1].id;
}
// Set the default account
if (defaultId) {
Lbry.account_set({ account_id: defaultId, default: true });
}
});
// Set the default account
if (defaultId) {
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);
}
});
};
}