Skip to content

Commit

Permalink
bitshares#1338: Add ChainAccountName to resolve account names without…
Browse files Browse the repository at this point in the history
… using get_full_accounts
  • Loading branch information
svk31 committed Mar 24, 2018
1 parent f5d8e94 commit 332b707
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 20 deletions.
43 changes: 43 additions & 0 deletions app/components/Utility/BindToChainState.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const isAssetType = checkChainType(ChainTypes.ChainAsset);
const isObjectsListType = checkChainType(ChainTypes.ChainObjectsList);
const isAccountsListType = checkChainType(ChainTypes.ChainAccountsList);
const isAssetsListType = checkChainType(ChainTypes.ChainAssetsList);
const isAccountNameType = checkChainType(ChainTypes.ChainAccountName);

function checkIfRequired(t) {
for (let k in ChainTypes) {
Expand All @@ -69,6 +70,7 @@ function BindToChainState(Component, options = {}) {
e === "show_loader"
);
this.chain_accounts = [];
this.chain_account_names = [];
this.chain_key_refs = [];
this.chain_address_balances = [];
this.chain_assets = [];
Expand All @@ -84,6 +86,9 @@ function BindToChainState(Component, options = {}) {
this.chain_accounts = prop_types_array
.filter(flow(secondEl, isAccountType))
.map(firstEl);
this.chain_account_names = prop_types_array
.filter(flow(secondEl, isAccountNameType))
.map(firstEl);
this.chain_key_refs = prop_types_array
.filter(flow(secondEl, isKeyRefsType))
.map(firstEl);
Expand All @@ -108,6 +113,7 @@ function BindToChainState(Component, options = {}) {
this.all_chain_props = [
...this.chain_objects,
...this.chain_accounts,
...this.chain_account_names,
...this.chain_key_refs,
...this.chain_address_balances,
...this.chain_assets,
Expand Down Expand Up @@ -188,6 +194,8 @@ function BindToChainState(Component, options = {}) {
let new_state = {};
let all_objects_counter = 0;
let resolved_objects_counter = 0;

/* Resolve pure objects*/
for (let key of this.chain_objects) {
let prop =
props[key] ||
Expand All @@ -213,6 +221,8 @@ function BindToChainState(Component, options = {}) {
if (this.state[key]) new_state[key] = null;
}
}

/* Resolve accounts */
for (let key of this.chain_accounts) {
let prop =
props[key] ||
Expand All @@ -239,6 +249,31 @@ function BindToChainState(Component, options = {}) {
if (this.state[key]) new_state[key] = null;
}
}

/* Resolve account names */
for (let key of this.chain_account_names) {
let prop =
props[key] ||
this.dynamic_props[key] ||
this.default_props[key];
if (prop) {
let name = ChainStore.getAccountName(prop);
if (
name === undefined &&
this.required_props.indexOf(key) === -1 &&
name !== this.state[key]
)
new_state[key] = name;
else if (name && name !== this.state[key])
new_state[key] = name;
++all_objects_counter;
if (name !== undefined) ++resolved_objects_counter;
} else {
if (this.state[key]) new_state[key] = null;
}
}

/* Resolve account key references */
for (let key of this.chain_key_refs) {
let prop =
props[key] ||
Expand All @@ -260,6 +295,8 @@ function BindToChainState(Component, options = {}) {
if (this.state[key]) new_state[key] = null;
}
}

/* Resolve balance objects */
for (let key of this.chain_address_balances) {
let prop =
props[key] ||
Expand All @@ -281,6 +318,8 @@ function BindToChainState(Component, options = {}) {
if (this.state[key]) new_state[key] = null;
}
}

/* Resolve assets */
for (let key of this.chain_assets) {
let prop =
props[key] ||
Expand All @@ -302,6 +341,8 @@ function BindToChainState(Component, options = {}) {
if (this.state[key]) new_state[key] = null;
}
}

/* Resolve lists of pure objects */
for (let key of this.chain_objects_list) {
//console.log("-- Wrapper.update -->", this.chain_objects_list);
let prop =
Expand Down Expand Up @@ -346,6 +387,7 @@ function BindToChainState(Component, options = {}) {
}
}

/* Resolve lists of accounts */
for (let key of this.chain_accounts_list) {
//console.log("-- Wrapper.update -->", this.chain_accounts_list);
let prop =
Expand Down Expand Up @@ -389,6 +431,7 @@ function BindToChainState(Component, options = {}) {
}
}

/* Resolve lists of assets */
for (let key of this.chain_assets_list) {
//console.log("-- Wrapper.update -->", this.chain_assets_list);
let prop =
Expand Down
37 changes: 29 additions & 8 deletions app/components/Utility/ChainTypes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import utils from "common/utils";
import Immutable from "immutable";
import {ChainTypes as grapheneChainTypes} from "bitsharesjs/es";
import {
ChainTypes as grapheneChainTypes,
ChainValidation
} from "bitsharesjs/es";
const {object_type} = grapheneChainTypes;

function createChainableTypeChecker(validate) {
Expand Down Expand Up @@ -29,7 +32,7 @@ function createChainableTypeChecker(validate) {
return chainedCheckType;
}

function objectIdChecker(props, propName, componentName, location) {
function objectIdChecker(props, propName, componentName) {
componentName = componentName || "ANONYMOUS";
if (props[propName]) {
let value = props[propName];
Expand All @@ -51,7 +54,7 @@ function objectIdChecker(props, propName, componentName, location) {
return null;
}

function keyChecker(props, propName, componentName, location) {
function keyChecker(props, propName, componentName) {
componentName = componentName || "ANONYMOUS";
if (props[propName]) {
let value = props[propName];
Expand All @@ -69,7 +72,7 @@ function keyChecker(props, propName, componentName, location) {
return null;
}

function assetChecker(props, propName, componentName, location) {
function assetChecker(props, propName, componentName) {
componentName = componentName || "ANONYMOUS";
if (props[propName]) {
let value = props[propName];
Expand All @@ -87,7 +90,7 @@ function assetChecker(props, propName, componentName, location) {
return null;
}

function accountChecker(props, propName, componentName, location) {
function accountChecker(props, propName, componentName) {
componentName = componentName || "ANONYMOUS";
if (props[propName]) {
let value = props[propName];
Expand All @@ -114,7 +117,7 @@ function accountChecker(props, propName, componentName, location) {
return null;
}

function objectsListChecker(props, propName, componentName, location) {
function objectsListChecker(props, propName, componentName) {
componentName = componentName || "ANONYMOUS";
if (props[propName]) {
let value = props[propName];
Expand All @@ -134,7 +137,7 @@ function objectsListChecker(props, propName, componentName, location) {
return null;
}

function assetsListChecker(props, propName, componentName, location) {
function assetsListChecker(props, propName, componentName) {
componentName = componentName || "ANONYMOUS";
if (props[propName]) {
let value = props[propName];
Expand All @@ -154,7 +157,7 @@ function assetsListChecker(props, propName, componentName, location) {
return null;
}

function accountsListChecker(props, propName, componentName, location) {
function accountsListChecker(props, propName, componentName) {
componentName = componentName || "ANONYMOUS";
if (props[propName]) {
let value = props[propName];
Expand All @@ -174,8 +177,25 @@ function accountsListChecker(props, propName, componentName, location) {
return null;
}

function accountNameChecker(props, propName, componentName) {
componentName = componentName || "ANONYMOUS";
if (props[propName]) {
let value = props[propName];
if (ChainValidation.is_account_name(value)) {
return null;
} else {
return new Error(
`${propName} in ${componentName} is not a valid account name`
);
}
}
// assume all ok
return null;
}

let ChainObject = createChainableTypeChecker(objectIdChecker);
let ChainAccount = createChainableTypeChecker(accountChecker);
let ChainAccountName = createChainableTypeChecker(accountNameChecker);
let ChainKeyRefs = createChainableTypeChecker(keyChecker);
let ChainAddressBalances = createChainableTypeChecker(keyChecker);
let ChainAsset = createChainableTypeChecker(assetChecker);
Expand All @@ -186,6 +206,7 @@ let ChainAssetsList = createChainableTypeChecker(assetsListChecker);
export default {
ChainObject,
ChainAccount,
ChainAccountName,
ChainKeyRefs,
ChainAddressBalances,
ChainAsset,
Expand Down
12 changes: 4 additions & 8 deletions app/components/Utility/LinkToAccountById.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import BindToChainState from "./BindToChainState";

class LinkToAccountById extends React.Component {
static propTypes = {
account: ChainTypes.ChainObject.isRequired,
account: ChainTypes.ChainAccountName.isRequired,
subpage: React.PropTypes.string.isRequired
};

Expand All @@ -15,18 +15,14 @@ class LinkToAccountById extends React.Component {
};

shouldComponentUpdate(nextProps) {
if (
nextProps.account.get("name") &&
this.props.account.get("name") &&
nextProps.account.get("name") === this.props.account.get("name")
) {
if (nextProps.account === this.props.account) {
return false;
}
return true;
}

render() {
let account_name = this.props.account.get("name");
let account_name = this.props.account;
if (!account_name) {
return <span>{this.props.account.get("id")}</span>;
}
Expand All @@ -44,4 +40,4 @@ class LinkToAccountById extends React.Component {
}
}

export default BindToChainState(LinkToAccountById);
export default BindToChainState(LinkToAccountById, {autosubscribe: false});
3 changes: 3 additions & 0 deletions app/lib/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ var Utils = {
return false;
}
}
if (typeof a === "string" && typeof b === "string") {
return a !== b;
}
for (var key in a) {
if (!(key in b) || a[key] !== b[key]) {
return false;
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
"alt-container": "^1.0.0",
"alt-react": "0.0.1",
"bignumber.js": "^4.0.0",
"bitsharesjs": "^1.5.3",
"bitsharesjs": "^1.5.6",
"browser-locale": "^1.0.3",
"classnames": "^2.2.1",
"cookies-js": "^1.2.1",
Expand Down

0 comments on commit 332b707

Please sign in to comment.