From a67858364834227f2005b450c5f4816fef2e216e Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 9 Jul 2020 12:31:03 +0200 Subject: [PATCH] Fix accounts not showing with no democracy (#3149) * Fix accounts not showing with no democracy * Update packages/page-accounts/src/Accounts/Account.tsx * Adjust for Edgeware * Update packages/page-accounts/src/Accounts/Account.tsx * API dep --- .../page-accounts/src/Accounts/Account.tsx | 78 ++++++++++--------- packages/page-accounts/src/Accounts/index.tsx | 44 +++++------ packages/react-components/src/Table/Head.tsx | 6 +- 3 files changed, 66 insertions(+), 62 deletions(-) diff --git a/packages/page-accounts/src/Accounts/Account.tsx b/packages/page-accounts/src/Accounts/Account.tsx index 8e658aa6cc..8927e50256 100644 --- a/packages/page-accounts/src/Accounts/Account.tsx +++ b/packages/page-accounts/src/Accounts/Account.tsx @@ -347,36 +347,38 @@ function Account ({ account: { address, meta }, className = '', delegation, filt )} - - {isDelegateOpen && ( - - )} - {isUndelegateOpen && ( - - )} - {delegation && ( - - -
{delegation.conviction.toString()}
- - } - value={delegation.accountDelegated} - /> - )} - + {api.api.query.democracy?.votingOf && ( + + {isDelegateOpen && ( + + )} + {isUndelegateOpen && ( + + )} + {delegation && ( + + +
{delegation.conviction.toString()}
+ + } + value={delegation.accountDelegated} + /> + )} + + )} @@ -521,27 +523,27 @@ function Account ({ account: { address, meta }, className = '', delegation, filt {t('Multisig approvals')} ])} - {delegation?.accountDelegated && createMenuGroup([ - ( {t('Change democracy delegation')} - ), - (, + {t('Undelegate')} - ) + ])} - {!delegation?.accountDelegated && createMenuGroup([ - ( {t('Delegate democracy votes')} - ) + ])} (''); const [sortedAccountsWithDelegation, setSortedAccountsWithDelegation] = useState([]); const [{ sortedAccounts, sortedAddresses }, setSorted] = useState({ sortedAccounts: [], sortedAddresses: [] }); - const delegations = useCall(api.query.democracy.votingOf.multi, [sortedAddresses]); + const delegations = useCall(api.query.democracy?.votingOf?.multi, [sortedAddresses]); useEffect((): void => { const sortedAccounts = sortAccounts(allAccounts, favorites); @@ -82,31 +82,31 @@ function Overview ({ className = '', onStatusChange }: Props): React.ReactElemen }, [allAccounts, favorites]); useEffect(() => { - if (!delegations?.length) { + if (api.query.democracy?.votingOf && !delegations?.length) { return; } - const sortedAccountsWithDelegation = sortedAccounts?.map((account, index) => { - let delegation: Delegation | undefined; + setSortedAccountsWithDelegation( + sortedAccounts?.map((account, index) => { + let delegation: Delegation | undefined; - if (delegations[index].isDelegating) { - const _delegation = delegations[index].asDelegating; + if (delegations && delegations[index].isDelegating) { + const { balance: amount, conviction, target } = delegations[index].asDelegating; - delegation = { - accountDelegated: _delegation.target.toString(), - amount: _delegation.balance, - conviction: _delegation.conviction - }; - } + delegation = { + accountDelegated: target.toString(), + amount, + conviction + }; + } - return ({ - ...account, - delegation - }); - }); - - setSortedAccountsWithDelegation(sortedAccountsWithDelegation); - }, [delegations, sortedAccounts]); + return ({ + ...account, + delegation + }); + }) + ); + }, [api, delegations, sortedAccounts]); const _setBalance = useCallback( (account: string, balance: BN) => @@ -124,14 +124,14 @@ function Overview ({ className = '', onStatusChange }: Props): React.ReactElemen const header = useMemo(() => [ [t('accounts'), 'start', 3], [t('parent'), 'address'], - [t('delegation'), 'address ui--media-1500'], + api.query.democracy?.votingOf && [t('delegation'), 'address ui--media-1500'], [t('type')], [t('tags'), 'start'], [t('transactions'), 'ui--media-1500'], [t('balances')], [], [undefined, 'mini ui--media-1400'] - ], [t]); + ], [api, t]); const footer = useMemo(() => ( diff --git a/packages/react-components/src/Table/Head.tsx b/packages/react-components/src/Table/Head.tsx index 9a578dd1df..36fedd7500 100644 --- a/packages/react-components/src/Table/Head.tsx +++ b/packages/react-components/src/Table/Head.tsx @@ -5,10 +5,12 @@ import React from 'react'; import styled from 'styled-components'; +type HeaderDef = [React.ReactNode?, string?, number?, (() => void)?]; + interface Props { className?: string; filter?: React.ReactNode; - header: [React.ReactNode?, string?, number?, (() => void)?][]; + header: (null | undefined | HeaderDef)[]; isEmpty: boolean; } @@ -21,7 +23,7 @@ function Head ({ className = '', filter, header, isEmpty }: Props): React.ReactE )} - {header.map(([label, className = 'default', colSpan = 1, onClick], index) => + {header.filter((h): h is HeaderDef => !!h).map(([label, className = 'default', colSpan = 1, onClick], index) =>