From 332b7077d53f7fa40d3bdfc4e1b4e07b44636fe6 Mon Sep 17 00:00:00 2001 From: Sigve Kvalsvik Date: Sat, 24 Mar 2018 10:54:03 +0100 Subject: [PATCH] #1338: Add ChainAccountName to resolve account names without using get_full_accounts --- app/components/Utility/BindToChainState.jsx | 43 ++++++++++++++++++++ app/components/Utility/ChainTypes.js | 37 +++++++++++++---- app/components/Utility/LinkToAccountById.jsx | 12 ++---- app/lib/common/utils.js | 3 ++ package-lock.json | 6 +-- package.json | 2 +- 6 files changed, 83 insertions(+), 20 deletions(-) diff --git a/app/components/Utility/BindToChainState.jsx b/app/components/Utility/BindToChainState.jsx index 6d48425d27..4341a51610 100644 --- a/app/components/Utility/BindToChainState.jsx +++ b/app/components/Utility/BindToChainState.jsx @@ -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) { @@ -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 = []; @@ -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); @@ -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, @@ -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] || @@ -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] || @@ -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] || @@ -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] || @@ -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] || @@ -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 = @@ -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 = @@ -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 = diff --git a/app/components/Utility/ChainTypes.js b/app/components/Utility/ChainTypes.js index 7fdcbc3f0a..289c905dbb 100644 --- a/app/components/Utility/ChainTypes.js +++ b/app/components/Utility/ChainTypes.js @@ -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) { @@ -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]; @@ -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]; @@ -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]; @@ -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]; @@ -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]; @@ -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]; @@ -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]; @@ -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); @@ -186,6 +206,7 @@ let ChainAssetsList = createChainableTypeChecker(assetsListChecker); export default { ChainObject, ChainAccount, + ChainAccountName, ChainKeyRefs, ChainAddressBalances, ChainAsset, diff --git a/app/components/Utility/LinkToAccountById.jsx b/app/components/Utility/LinkToAccountById.jsx index 9facc5254d..c007630b5d 100644 --- a/app/components/Utility/LinkToAccountById.jsx +++ b/app/components/Utility/LinkToAccountById.jsx @@ -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 }; @@ -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 {this.props.account.get("id")}; } @@ -44,4 +40,4 @@ class LinkToAccountById extends React.Component { } } -export default BindToChainState(LinkToAccountById); +export default BindToChainState(LinkToAccountById, {autosubscribe: false}); diff --git a/app/lib/common/utils.js b/app/lib/common/utils.js index b6a4e60056..7a8ebf6e0d 100644 --- a/app/lib/common/utils.js +++ b/app/lib/common/utils.js @@ -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; diff --git a/package-lock.json b/package-lock.json index b8debf9b1f..7bfabf7906 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2283,9 +2283,9 @@ "dev": true }, "bitsharesjs": { - "version": "1.5.3", - "resolved": "https://registry.npmjs.org/bitsharesjs/-/bitsharesjs-1.5.3.tgz", - "integrity": "sha512-3wbPMWtZO5pD9DsLxX2dH/TOm1S0izK2NLkAg6kdMZ/MFnG173fMkXVtn40c+kftkSfFtu80MtpxHi3zvVJV3w==", + "version": "1.5.6", + "resolved": "https://registry.npmjs.org/bitsharesjs/-/bitsharesjs-1.5.6.tgz", + "integrity": "sha512-2bX+UF2HplcK7B2SbUuaCEb7ZOXHMHPTVN9AvmAzO0RZpjOY3CRaaVc+BthVp9mnr036JCcwYrH4TIV0fBftbw==", "requires": { "babel-plugin-add-module-exports": "0.2.1", "babel-preset-es2015": "6.24.1", diff --git a/package.json b/package.json index def8317e4e..7835c7858f 100644 --- a/package.json +++ b/package.json @@ -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",