From cdf1d88de56c6c09c1ba8fda61a0fd618d81e913 Mon Sep 17 00:00:00 2001 From: kwingram25 Date: Sun, 1 Sep 2019 14:23:42 +0200 Subject: [PATCH] Functional components (apps, react-components) (#1564) * Functional components (apps, react-components) * move consts --- packages/apps/src/SideBar/NodeInfo.tsx | 43 +- packages/apps/src/TopBar.tsx | 28 +- packages/apps/src/overlays/Connecting.tsx | 91 ++-- packages/react-components/src/ActionItem.tsx | 48 +-- packages/react-components/src/AddressCard.tsx | 41 +- packages/react-components/src/AddressInfo.tsx | 406 +++++++++--------- packages/react-components/src/AddressMini.tsx | 160 ++++--- packages/react-components/src/AddressRow.tsx | 2 +- packages/react-components/src/Available.tsx | 28 +- packages/react-components/src/Balance.tsx | 77 ++-- packages/react-components/src/Bonded.tsx | 77 ++-- packages/react-components/src/Bubble.tsx | 34 +- packages/react-components/src/Button/Or.tsx | 28 +- packages/react-components/src/Call.tsx | 125 +++--- packages/react-components/src/Card.tsx | 16 +- packages/react-components/src/CardSummary.tsx | 89 ++-- packages/react-components/src/ChainImg.tsx | 23 +- packages/react-components/src/Chart/Base.tsx | 22 +- .../react-components/src/Chart/Doughnut.tsx | 77 ++-- packages/react-components/src/Columar.tsx | 16 +- packages/react-components/src/CopyButton.tsx | 82 ++-- packages/react-components/src/CryptoType.tsx | 39 +- packages/react-components/src/Event.tsx | 45 +- .../react-components/src/FilterOverlay.tsx | 16 +- packages/react-components/src/Forget.tsx | 226 +++++----- .../react-components/src/InfoForInput.tsx | 16 +- .../react-components/src/InputBalance.tsx | 50 +-- .../src/InputConsts/SelectKey.tsx | 47 +- .../src/InputConsts/SelectSection.tsx | 30 +- packages/react-components/src/InputError.tsx | 22 +- .../src/InputExtrinsic/SelectMethod.tsx | 49 +-- .../src/InputExtrinsic/SelectSection.tsx | 30 +- .../src/InputRpc/SelectMethod.tsx | 50 ++- .../src/InputRpc/SelectSection.tsx | 30 +- .../src/InputStorage/SelectKey.tsx | 49 +-- .../src/InputStorage/SelectSection.tsx | 30 +- packages/react-components/src/InputTags.tsx | 2 +- packages/react-components/src/Label.tsx | 24 +- packages/react-components/src/Labelled.tsx | 52 ++- .../react-components/src/LinkPolkascan.tsx | 37 +- packages/react-components/src/Messages.tsx | 100 +++-- packages/react-components/src/Nonce.tsx | 28 +- packages/react-components/src/Output.tsx | 56 ++- packages/react-components/src/Params/Call.tsx | 47 +- .../react-components/src/Params/Extrinsic.tsx | 46 +- .../react-components/src/Params/Proposal.tsx | 45 +- packages/react-components/src/Progress.tsx | 75 ++-- .../react-components/src/ProposedAction.tsx | 143 +++--- packages/react-components/src/Static.tsx | 36 +- .../react-components/src/Status/index.tsx | 257 ++++++----- packages/react-components/src/SummaryBox.tsx | 16 +- packages/react-components/src/Tabs.tsx | 39 +- packages/react-components/src/Toggle.tsx | 42 +- packages/react-components/src/TxButton.tsx | 2 +- 54 files changed, 1551 insertions(+), 1738 deletions(-) diff --git a/packages/apps/src/SideBar/NodeInfo.tsx b/packages/apps/src/SideBar/NodeInfo.tsx index 914bfc2077..2cce89069e 100644 --- a/packages/apps/src/SideBar/NodeInfo.tsx +++ b/packages/apps/src/SideBar/NodeInfo.tsx @@ -15,34 +15,31 @@ interface Props extends ApiProps, BareProps {} // eslint-disable-next-line @typescript-eslint/no-var-requires const pkgJson = require('../../package.json'); -class NodeInfo extends React.PureComponent { - public render (): React.ReactNode { - const { api, className } = this.props; - const uiInfo = `apps v${pkgJson.version}`; +const uiInfo = `apps v${pkgJson.version}`; - return ( -
- {this.renderNode()} -
{api.libraryInfo.replace('@polkadot/', '')}
-
{uiInfo}
-
- ); +function renderNode ({ isApiReady }: Props): React.ReactNode { + if (!isApiReady) { + return null; } - private renderNode (): React.ReactNode { - const { isApiReady } = this.props; + return ( +
+   + +
+ ); +} - if (!isApiReady) { - return null; - } +function NodeInfo (props: Props): React.ReactElement { + const { api, className } = props; - return ( -
-   - -
- ); - } + return ( +
+ {renderNode(props)} +
{api.libraryInfo.replace('@polkadot/', '')}
+
{uiInfo}
+
+ ); } export default withApi( diff --git a/packages/apps/src/TopBar.tsx b/packages/apps/src/TopBar.tsx index 8dfefd879a..df277eafef 100644 --- a/packages/apps/src/TopBar.tsx +++ b/packages/apps/src/TopBar.tsx @@ -12,23 +12,19 @@ interface Props { className?: string; } -class TopBar extends React.PureComponent { - public render (): React.ReactNode { - const { className } = this.props; - - return ( -
-
-   - -
-
-   - -
+function TopBar ({ className }: Props): React.ReactElement { + return ( +
+
+   +
- ); - } +
+   + +
+
+ ); } export default styled(TopBar)` diff --git a/packages/apps/src/overlays/Connecting.tsx b/packages/apps/src/overlays/Connecting.tsx index 741ff1af89..0c3a2ac7ef 100644 --- a/packages/apps/src/overlays/Connecting.tsx +++ b/packages/apps/src/overlays/Connecting.tsx @@ -16,60 +16,57 @@ import BaseOverlay from './Base'; type Props = I18nProps & ApiProps; const isFirefox = typeof (window as any).InstallTrigger !== 'undefined'; +const wsUrl = settings.apiUrl; +const isWs = wsUrl.startsWith('ws://'); +const isWsRemote = wsUrl.includes('127.0.0.1'); +const isHttps = window.location.protocol.startsWith('https:'); -class Connecting extends React.PureComponent { - public render (): React.ReactNode { - return this.renderExtension() || this.renderConnecting(); +function renderExtension ({ className, isWaitingInjected, t }: Props): React.ReactNode { + if (!isWaitingInjected) { + return null; } - private renderExtension (): React.ReactNode { - const { className, isWaitingInjected, t } = this.props; + return ( + +
{t('Waiting for authorization from the extension. Please open the installed extension and approve or reject access.')}
+
+ ); +} - if (!isWaitingInjected) { - return null; - } - - return ( - -
{t('Waiting for authorization from the extension. Please open the installed extension and approve or reject access.')}
-
- ); +function renderConnecting ({ className, isApiConnected, t }: Props): React.ReactNode { + if (isApiConnected) { + return null; } - private renderConnecting (): React.ReactNode { - const { className, isApiConnected, t } = this.props; + return ( + +
{t('You are not connected to a node. Ensure that your node is running and that the Websocket endpoint is reachable.')}
+ { + isFirefox && isWs + ?
{t('With the Firefox browser connecting to insecure WebSockets ({{wsUrl}}) will fail due to the browser not allowing localhost access from a secure site.', { replace: { wsUrl } })}
+ : undefined + } + { + isWs && isWsRemote && isHttps + ?
{t('You are connecting from a secure location to an insecure WebSocket ({{wsUrl}}). Due to browser mixed-content security policies this connection type is not allowed. Change the RPC service to a secure \'wss\' endpoint.', { replace: { wsUrl } })}
+ : undefined + } +
+ ); +} - if (isApiConnected) { - return null; - } - - const wsUrl = settings.apiUrl; - const isWs = wsUrl.startsWith('ws://'); - const isWsRemote = wsUrl.includes('127.0.0.1'); - const isHttps = window.location.protocol.startsWith('https:'); - - return ( - -
{t('You are not connected to a node. Ensure that your node is running and that the Websocket endpoint is reachable.')}
- { - isFirefox && isWs - ?
{t('With the Firefox browser connecting to insecure WebSockets ({{wsUrl}}) will fail due to the browser not allowing localhost access from a secure site.', { replace: { wsUrl } })}
- : undefined - } - { - isWs && isWsRemote && isHttps - ?
{t('You are connecting from a secure location to an insecure WebSocket ({{wsUrl}}). Due to browser mixed-content security policies this connection type is not allowed. Change the RPC service to a secure \'wss\' endpoint.', { replace: { wsUrl } })}
- : undefined - } -
- ); - } +function Connecting (props: Props): React.ReactElement { + return ( + <> + {renderExtension(props) || renderConnecting(props)} + + ); } export default withMulti( diff --git a/packages/react-components/src/ActionItem.tsx b/packages/react-components/src/ActionItem.tsx index 574b04ba7b..5d952fcf1d 100644 --- a/packages/react-components/src/ActionItem.tsx +++ b/packages/react-components/src/ActionItem.tsx @@ -28,37 +28,25 @@ export const styles = ` ${proposedActionStyles} `; -class ActionItem extends React.PureComponent { - public render (): React.ReactNode { - const { className, children, accessory } = this.props; - - return ( - -
-
-
- {this.renderProposal()} -
- {accessory} +function ActionItem ({ className, children, accessory, idNumber, proposal, expandNested }: Props): React.ReactElement { + return ( + +
+
+
+
- {children} + {accessory}
- - ); - } - - private renderProposal (): React.ReactNode { - const { idNumber, proposal, expandNested } = this.props; - - return ( - - ); - } + {children} +
+
+ ); } -export default withRouter(styled(ActionItem as React.ComponentClass)`${styles}`); +export default withRouter(styled(ActionItem)`${styles}`); diff --git a/packages/react-components/src/AddressCard.tsx b/packages/react-components/src/AddressCard.tsx index c77e0fb564..73409a2399 100644 --- a/packages/react-components/src/AddressCard.tsx +++ b/packages/react-components/src/AddressCard.tsx @@ -20,30 +20,27 @@ interface Props extends AddressProps { withExplorer?: boolean; } -class AddressCard extends React.PureComponent { - public render (): React.ReactNode { - const { className, value, withExplorer } = this.props; - - return ( - - - {withExplorer && ( -
- -
- )} -
- ); - } +function AddressCard (props: Props): React.ReactElement { + const { className, value, withExplorer } = props; + return ( + + + {withExplorer && ( +
+ +
+ )} +
+ ); } -export default translate(styled(AddressCard as React.ComponentClass)` +export default translate(styled(AddressCard)` display: flex; flex-direction: column; justify-content: space-between; diff --git a/packages/react-components/src/AddressInfo.tsx b/packages/react-components/src/AddressInfo.tsx index 7d5a57d5a3..ac202876ab 100644 --- a/packages/react-components/src/AddressInfo.tsx +++ b/packages/react-components/src/AddressInfo.tsx @@ -49,239 +49,225 @@ type Props = BareProps & I18nProps & { withValidatorPrefs?: boolean | ValidatorPrefsType; }; -class AddressInfo extends React.PureComponent { - public render (): React.ReactNode { - const { children, className } = this.props; +// either true (filtered above already) or [own, ...all extras bonded funds from nominators] +function renderBonded ({ staking_info, t }: Props, bonded: true | BN[]): React.ReactNode { + let value; - return ( -
-
- {this.renderBalances()} - {this.renderHexSessionId()} - {this.renderValidatorPrefs()} - {this.renderRewardDestination()} -
+ if (Array.isArray(bonded)) { + // Get the sum of funds bonded by any nominator (if available) + const extras = bonded.filter((_, index): boolean => index !== 0); + const extra = extras.reduce((total, value): BN => total.add(value), new BN(0)).gtn(0) + ? `(+${extras.map((bonded): string => formatBalance(bonded)).join(', ')})` + : ''; - {this.renderExtended()} - {children && ( -
- {children} -
- )} -
- ); + value = `${formatBalance(bonded[0])} ${extra}`; + } else if (staking_info && staking_info.stakingLedger && staking_info.accountId.eq(staking_info.stashId)) { + value = formatBalance(staking_info.stakingLedger.active); } - private renderBalances (): React.ReactNode { - const { balances_all, staking_info, t, withBalance = true } = this.props; - const balanceDisplay = withBalance === true - ? { available: true, bonded: true, free: true, redeemable: true, unlocking: true } - : withBalance || undefined; - - if (!balanceDisplay || !balances_all) { - return null; - } - - return ( + return value + ? ( <> - {balanceDisplay.free && ( - <> -