From 5e1b8a8f4906e797f3085c7d9755ec6648fa5abe Mon Sep 17 00:00:00 2001 From: Jaco Date: Tue, 18 Jan 2022 08:23:55 +0100 Subject: [PATCH] Prepare for selectable groups (#6842) --- .../src/Actions/Account/index.tsx | 25 ++++++++++- packages/page-staking/src/Actions/index.tsx | 42 ++++++++++++++----- packages/page-staking/src/Payouts/index.tsx | 10 ++--- .../react-components/src/Button/Group.tsx | 4 ++ 4 files changed, 63 insertions(+), 18 deletions(-) diff --git a/packages/page-staking/src/Actions/Account/index.tsx b/packages/page-staking/src/Actions/Account/index.tsx index 82d6633f29..499391cb54 100644 --- a/packages/page-staking/src/Actions/Account/index.tsx +++ b/packages/page-staking/src/Actions/Account/index.tsx @@ -12,7 +12,7 @@ import React, { useCallback, useContext, useMemo } from 'react'; import styled from 'styled-components'; import { ApiPromise } from '@polkadot/api'; -import { AddressInfo, AddressMini, AddressSmall, Badge, Button, Menu, Popup, StakingBonded, StakingRedeemable, StakingUnbonding, StatusContext, TxButton } from '@polkadot/react-components'; +import { AddressInfo, AddressMini, AddressSmall, Badge, Button, Checkbox, Menu, Popup, StakingBonded, StakingRedeemable, StakingUnbonding, StatusContext, TxButton } from '@polkadot/react-components'; import { useApi, useCall, useToggle } from '@polkadot/react-hooks'; import { BN, formatNumber, isFunction } from '@polkadot/util'; @@ -34,8 +34,10 @@ interface Props { allSlashes?: [BN, UnappliedSlash[]][]; className?: string; isDisabled?: boolean; + isSelectable: boolean; info: StakerState; next?: string[]; + onSelect: (stashId: string, controllerId: string, isSelected: boolean) => void; stashId: string; targets: SortedTargets; validators?: string[]; @@ -68,7 +70,7 @@ function useStashCalls (api: ApiPromise, stashId: string) { return { balancesAll, spanCount, stakingAccount }; } -function Account ({ allSlashes, className = '', info: { controllerId, destination, hexSessionIdNext, hexSessionIdQueue, isLoading, isOwnController, isOwnStash, isStashNominating, isStashValidating, nominating, sessionIds, stakingLedger, stashId }, isDisabled, targets }: Props): React.ReactElement { +function Account ({ allSlashes, className = '', info: { controllerId, destination, hexSessionIdNext, hexSessionIdQueue, isLoading, isOwnController, isOwnStash, isStashNominating, isStashValidating, nominating, sessionIds, stakingLedger, stashId }, isDisabled, isSelectable, onSelect, targets }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const { queueExtrinsic } = useContext(StatusContext); @@ -84,6 +86,13 @@ function Account ({ allSlashes, className = '', info: { controllerId, destinatio const [isValidateOpen, toggleValidate] = useToggle(); const { balancesAll, spanCount, stakingAccount } = useStashCalls(api, stashId); + const _onSelect = useCallback( + (isSelected: boolean) => controllerId && onSelect(stashId, controllerId, isSelected), + [controllerId, stashId, onSelect] + ); + + const [isSelected, , setSelected] = useToggle(false, _onSelect); + const slashes = useMemo( () => extractSlashes(stashId, allSlashes), [allSlashes, stashId] @@ -375,6 +384,14 @@ function Account ({ allSlashes, className = '', info: { controllerId, destinatio )} + + {isSelectable && controllerId && ( + + )} + ); } @@ -385,4 +402,8 @@ export default React.memo(styled(Account)` margin-right: 0.25rem; vertical-align: inherit; } + + .hidden { + display: none; + } `); diff --git a/packages/page-staking/src/Actions/index.tsx b/packages/page-staking/src/Actions/index.tsx index 9c4cb33800..73f7bd1545 100644 --- a/packages/page-staking/src/Actions/index.tsx +++ b/packages/page-staking/src/Actions/index.tsx @@ -4,7 +4,7 @@ import type { StakerState } from '@polkadot/react-hooks/types'; import type { SortedTargets } from '../types'; -import React, { useMemo, useRef, useState } from 'react'; +import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { Button, Table, ToggleGroup } from '@polkadot/react-components'; import { useAvailableSlashes } from '@polkadot/react-hooks'; @@ -113,6 +113,7 @@ function Actions ({ className = '', isInElection, ownStashes, targets }: Props): const { t } = useTranslation(); const allSlashes = useAvailableSlashes(); const [typeIndex, setTypeIndex] = useState(0); + const [, setSelected] = useState<[string, string][]>([]); const headerRef = useRef([ [t('stashes'), 'start', 2], @@ -134,19 +135,36 @@ function Actions ({ className = '', isInElection, ownStashes, targets }: Props): [ownStashes] ); - const footer = useMemo(() => ( - - - {formatTotal(typeIndex, state)} - - - ), [state, typeIndex]); - - const filtered = useMemo( - () => state.foundStashes && filterStashes(typeIndex, state.foundStashes), + const [isSelectable, filtered, footer] = useMemo( + () => [ + false, // [1, 2].includes(typeIndex) + state.foundStashes && filterStashes(typeIndex, state.foundStashes), + ( + + + {formatTotal(typeIndex, state)} + + + ) + ], [state, typeIndex] ); + const onSelectStash = useCallback( + (stashId: string, controllerId: string, isSelected: boolean) => + setSelected((prev) => + isSelected + ? [...prev, [stashId, controllerId]] + : prev.filter(([s]) => s !== stashId) + ), + [] + ); + + useEffect( + () => setSelected([]), + [typeIndex] + ); + return (
@@ -176,7 +194,9 @@ function Actions ({ className = '', isInElection, ownStashes, targets }: Props): allSlashes={allSlashes} info={info} isDisabled={isInElection} + isSelectable={isSelectable} key={info.stashId} + onSelect={onSelectStash} targets={targets} /> ))} diff --git a/packages/page-staking/src/Payouts/index.tsx b/packages/page-staking/src/Payouts/index.tsx index f6f9ccda41..fd33c9e041 100644 --- a/packages/page-staking/src/Payouts/index.tsx +++ b/packages/page-staking/src/Payouts/index.tsx @@ -226,16 +226,16 @@ function Payouts ({ className = '', isInElection, ownValidators }: Props): React return (
- +