set the default account upon successful /sync/get call #38

Merged
akinwale merged 1 commit from sync-default-account into master 2019-05-14 16:57:23 +02:00
5 changed files with 118 additions and 4 deletions

44
dist/bundle.es.js vendored
View file

@ -78,6 +78,7 @@ const GET_SYNC_COMPLETED = 'GET_SYNC_COMPLETED';
const SET_SYNC_STARTED = 'SET_SYNC_STARTED';
const SET_SYNC_FAILED = 'SET_SYNC_FAILED';
const SET_SYNC_COMPLETED = 'SET_SYNC_COMPLETED';
const SET_DEFAULT_ACCOUNT = 'SET_DEFAULT_ACCOUNT';
var action_types = /*#__PURE__*/Object.freeze({
GENERATE_AUTH_TOKEN_FAILURE: GENERATE_AUTH_TOKEN_FAILURE,
@ -142,7 +143,8 @@ var action_types = /*#__PURE__*/Object.freeze({
GET_SYNC_COMPLETED: GET_SYNC_COMPLETED,
SET_SYNC_STARTED: SET_SYNC_STARTED,
SET_SYNC_FAILED: SET_SYNC_FAILED,
SET_SYNC_COMPLETED: SET_SYNC_COMPLETED
SET_SYNC_COMPLETED: SET_SYNC_COMPLETED,
SET_DEFAULT_ACCOUNT: SET_DEFAULT_ACCOUNT
});
const Lbryio = {
@ -2011,6 +2013,40 @@ function doSetSync(oldHash, newHash, data) {
});
};
}
function doSetDefaultAccount() {
return dispatch => {
dispatch({
type: SET_DEFAULT_ACCOUNT
});
lbryRedux.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;
} // Set the default account
if (defaultId) {
lbryRedux.Lbry.account_set({
account_id: defaultId,
default: true
});
}
});
};
}
function doGetSync(password) {
return dispatch => {
dispatch({
@ -2037,7 +2073,10 @@ function doGetSync(password) {
if (walletHash !== syncHash) {
// different local hash, need to synchronise
dispatch(doSetSync(syncHash, walletHash, walletData));
}
} // set the default account
dispatch(doSetDefaultAccount());
});
}
@ -2648,6 +2687,7 @@ exports.doInstallNew = doInstallNew;
exports.doRemoveUnreadSubscription = doRemoveUnreadSubscription;
exports.doRemoveUnreadSubscriptions = doRemoveUnreadSubscriptions;
exports.doRewardList = doRewardList;
exports.doSetDefaultAccount = doSetDefaultAccount;
exports.doSetSync = doSetSync;
exports.doSetViewMode = doSetViewMode;
exports.doShowSuggestedSubs = doShowSuggestedSubs;

42
dist/bundle.js vendored
View file

@ -212,6 +212,8 @@ __webpack_require__.r(__webpack_exports__);
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "doSetSync", function() { return redux_actions_sync__WEBPACK_IMPORTED_MODULE_12__["doSetSync"]; });
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "doSetDefaultAccount", function() { return redux_actions_sync__WEBPACK_IMPORTED_MODULE_12__["doSetDefaultAccount"]; });
/* harmony import */ var redux_reducers_auth__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__(26);
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "authReducer", function() { return redux_reducers_auth__WEBPACK_IMPORTED_MODULE_13__["authReducer"]; });
@ -507,6 +509,7 @@ __webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "SET_SYNC_STARTED", function() { return SET_SYNC_STARTED; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "SET_SYNC_FAILED", function() { return SET_SYNC_FAILED; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "SET_SYNC_COMPLETED", function() { return SET_SYNC_COMPLETED; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "SET_DEFAULT_ACCOUNT", function() { return SET_DEFAULT_ACCOUNT; });
// Auth Token
var GENERATE_AUTH_TOKEN_FAILURE = 'GENERATE_AUTH_TOKEN_FAILURE';
var GENERATE_AUTH_TOKEN_STARTED = 'GENERATE_AUTH_TOKEN_STARTED';
@ -577,6 +580,7 @@ var GET_SYNC_COMPLETED = 'GET_SYNC_COMPLETED';
var SET_SYNC_STARTED = 'SET_SYNC_STARTED';
var SET_SYNC_FAILED = 'SET_SYNC_FAILED';
var SET_SYNC_COMPLETED = 'SET_SYNC_COMPLETED';
var SET_DEFAULT_ACCOUNT = 'SET_DEFAULT_ACCOUNT';
/***/ }),
/* 2 */
@ -3395,6 +3399,7 @@ var doFetchViewCount = function doFetchViewCount(claimId) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "doSetSync", function() { return doSetSync; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "doSetDefaultAccount", function() { return doSetDefaultAccount; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "doGetSync", function() { return doGetSync; });
/* harmony import */ var constants_action_types__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
/* harmony import */ var lbryio__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(2);
@ -3438,6 +3443,38 @@ function doSetSync(oldHash, newHash, data) {
});
};
}
function doSetDefaultAccount() {
return function (dispatch) {
dispatch({
type: constants_action_types__WEBPACK_IMPORTED_MODULE_0__["SET_DEFAULT_ACCOUNT"]
});
lbry_redux__WEBPACK_IMPORTED_MODULE_2__["Lbry"].account_list().then(function (accountList) {
var accounts = accountList.lbc_mainnet;
var defaultId;
for (var 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;
} // Set the default account
if (defaultId) {
lbry_redux__WEBPACK_IMPORTED_MODULE_2__["Lbry"].account_set({
account_id: defaultId,
"default": true
});
}
});
};
}
function doGetSync(password) {
return function (dispatch) {
dispatch({
@ -3464,7 +3501,10 @@ function doGetSync(password) {
if (walletHash !== syncHash) {
// different local hash, need to synchronise
dispatch(doSetSync(syncHash, walletHash, walletData));
}
} // set the default account
dispatch(doSetDefaultAccount());
});
}

View file

@ -76,3 +76,4 @@ export const GET_SYNC_COMPLETED = 'GET_SYNC_COMPLETED';
export const SET_SYNC_STARTED = 'SET_SYNC_STARTED';
export const SET_SYNC_FAILED = 'SET_SYNC_FAILED';
export const SET_SYNC_COMPLETED = 'SET_SYNC_COMPLETED';
export const SET_DEFAULT_ACCOUNT = 'SET_DEFAULT_ACCOUNT';

View file

@ -59,7 +59,7 @@ export { doFetchCostInfoForUri } from 'redux/actions/cost_info';
export { doBlackListedOutpointsSubscribe } from 'redux/actions/blacklist';
export { doFetchFeaturedUris, doFetchTrendingUris } from 'redux/actions/homepage';
export { doFetchViewCount } from 'redux/actions/stats';
export { doGetSync, doSetSync } from 'redux/actions/sync';
export { doGetSync, doSetSync, doSetDefaultAccount } from 'redux/actions/sync';
// reducers
export { authReducer } from 'redux/reducers/auth';

View file

@ -31,6 +31,36 @@ export function doSetSync(oldHash, newHash, data) {
};
}
export function doSetDefaultAccount() {
return dispatch => {
dispatch({
type: ACTIONS.SET_DEFAULT_ACCOUNT,
neb-b commented 2019-05-13 21:51:51 +02:00 (Migrated from github.com)
Review

Nothing is listening for this? Do we need it?

Nothing is listening for this? Do we need it?
akinwale commented 2019-05-14 16:56:17 +02:00 (Migrated from github.com)
Review

Just added this in case we may have to use it in the future.

Just added this in case we may have to use it in the future.
});
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;
}
// Set the default account
if (defaultId) {
neb-b commented 2019-05-13 21:53:23 +02:00 (Migrated from github.com)
Review

If there is no defaultId, should we error to say that sync won't work for whatever reason?

If there is no `defaultId`, should we error to say that sync won't work for whatever reason?
akinwale commented 2019-05-14 16:57:08 +02:00 (Migrated from github.com)
Review

No need to throw an error here. If no default id is found, then the wallet will end up being set to the pre-existing default account when it was created.

No need to throw an error here. If no default id is found, then the wallet will end up being set to the pre-existing default account when it was created.
Lbry.account_set({ account_id: defaultId, default: true });
}
});
};
}
export function doGetSync(password) {
return dispatch => {
dispatch({
@ -50,6 +80,9 @@ export function doGetSync(password) {
// different local hash, need to synchronise
dispatch(doSetSync(syncHash, walletHash, walletData));
}
// set the default account
dispatch(doSetDefaultAccount());
}
);
}