Council proposal to functional (#2553)
* Council proposal to functional * Small cleanups
This commit is contained in:
parent
6cfc8c9095
commit
f3f3ade6ce
@ -3,135 +3,99 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { SubmittableExtrinsic } from '@polkadot/api/types';
|
||||
import { ApiProps } from '@polkadot/react-api/types';
|
||||
import { Proposal } from '@polkadot/types/interfaces';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import React from 'react';
|
||||
import { registry, withApi, withMulti } from '@polkadot/react-api';
|
||||
import { Button, Extrinsic, InputNumber } from '@polkadot/react-components';
|
||||
import TxModal, { TxModalState, TxModalProps } from '@polkadot/react-components/TxModal';
|
||||
import { createType } from '@polkadot/types';
|
||||
import React, { useCallback, useEffect, useState } from 'react';
|
||||
import { Button, Extrinsic, InputAddress, InputNumber, Modal, TxButton } from '@polkadot/react-components';
|
||||
import { useApi, useToggle } from '@polkadot/react-hooks';
|
||||
|
||||
import translate from '../translate';
|
||||
import { useTranslation } from '../translate';
|
||||
|
||||
interface Props extends TxModalProps, ApiProps {
|
||||
interface Props {
|
||||
isMember: boolean;
|
||||
members: string[];
|
||||
}
|
||||
|
||||
interface State extends TxModalState {
|
||||
method: SubmittableExtrinsic<'promise'> | null;
|
||||
threshold: BN | null;
|
||||
interface Threshold {
|
||||
isThresholdValid: boolean;
|
||||
threshold?: BN;
|
||||
}
|
||||
|
||||
class Propose extends TxModal<Props, State> {
|
||||
constructor (props: Props) {
|
||||
super(props);
|
||||
function Propose ({ isMember, members }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const { apiDefaultTxSudo } = useApi();
|
||||
const [isOpen, toggleOpen] = useToggle();
|
||||
const [accountId, setAcountId] = useState<string | null>(null);
|
||||
const [method, setMethod] = useState<SubmittableExtrinsic<'promise'> | null | undefined>();
|
||||
const [{ isThresholdValid, threshold }, setThreshold] = useState<Threshold>({ isThresholdValid: false });
|
||||
|
||||
this.defaultState = {
|
||||
...this.defaultState,
|
||||
method: null,
|
||||
threshold: props.members.length ? new BN(Math.ceil(props.members.length * 0.5)) : null
|
||||
};
|
||||
this.state = this.defaultState;
|
||||
}
|
||||
useEffect((): void => {
|
||||
members && setThreshold({
|
||||
isThresholdValid: members.length !== 0,
|
||||
threshold: new BN(Math.ceil(members.length * 0.5))
|
||||
});
|
||||
}, [members]);
|
||||
|
||||
public static getDerivedStateFromProps ({ members }: Props, { threshold }: State): Pick<State, never> | null {
|
||||
if (!threshold && members.length) {
|
||||
return { threshold: new BN(Math.ceil(members.length * 0.5)) };
|
||||
}
|
||||
const _setMethod = useCallback(
|
||||
(method?: SubmittableExtrinsic<'promise'> | null) => setMethod(() => method),
|
||||
[]
|
||||
);
|
||||
|
||||
return null;
|
||||
}
|
||||
const _setThreshold = useCallback(
|
||||
(threshold?: BN) => setThreshold({
|
||||
isThresholdValid: !!threshold?.gtn(0),
|
||||
threshold
|
||||
}),
|
||||
[]
|
||||
);
|
||||
|
||||
protected headerText = (): string => this.props.t('Propose a council motion');
|
||||
|
||||
protected txMethod = (): string => 'council.propose';
|
||||
|
||||
protected txParams = (): [BN | null, ...Proposal[]] => {
|
||||
const { method, threshold } = this.state;
|
||||
|
||||
return [
|
||||
threshold,
|
||||
...(method ? [createType(registry, 'Proposal', method)] : [])
|
||||
];
|
||||
}
|
||||
|
||||
protected isDisabled = (): boolean => {
|
||||
const { members } = this.props;
|
||||
const { accountId, method, threshold } = this.state;
|
||||
|
||||
const hasThreshold = !!threshold && threshold.gtn(0) && threshold.ltn(members.length + 1);
|
||||
const hasMethod = !!method;
|
||||
|
||||
return !accountId || !hasMethod || !hasThreshold;
|
||||
}
|
||||
|
||||
protected renderTrigger = (): React.ReactNode => {
|
||||
const { isMember, t } = this.props;
|
||||
|
||||
return (
|
||||
return (
|
||||
<>
|
||||
<Button
|
||||
icon='add'
|
||||
isDisabled={!isMember}
|
||||
label={t('Propose motion')}
|
||||
onClick={this.showModal}
|
||||
onClick={toggleOpen}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
protected renderContent = (): React.ReactNode => {
|
||||
const { apiDefaultTxSudo, members, t } = this.props;
|
||||
const { threshold } = this.state;
|
||||
|
||||
return (
|
||||
<>
|
||||
<InputNumber
|
||||
className='medium'
|
||||
help={t('The minimum number of council votes required to approve this motion')}
|
||||
isError={!threshold || threshold.eqn(0) || threshold.gtn(members.length)}
|
||||
label={t('threshold')}
|
||||
onChange={this.onChangeThreshold}
|
||||
onEnter={this.sendTx}
|
||||
placeholder={
|
||||
t(
|
||||
'Positive number between 1 and {{memberCount}}',
|
||||
{ replace: { memberCount: members.length } }
|
||||
)
|
||||
}
|
||||
value={threshold || new BN(0)}
|
||||
/>
|
||||
<Extrinsic
|
||||
defaultValue={apiDefaultTxSudo}
|
||||
label={t('proposal')}
|
||||
onChange={this.onChangeExtrinsic}
|
||||
onEnter={this.sendTx}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
private onChangeThreshold = (threshold: BN | null = null): void => {
|
||||
const { members } = this.props;
|
||||
|
||||
if (members.length && !this.defaultState.threshold) {
|
||||
this.defaultState.threshold = new BN(Math.ceil(members.length * 0.5));
|
||||
}
|
||||
|
||||
this.setState({ threshold });
|
||||
}
|
||||
|
||||
private onChangeExtrinsic = (method?: SubmittableExtrinsic<'promise'>): void => {
|
||||
if (!method) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.setState({ method });
|
||||
}
|
||||
{isOpen && (
|
||||
<Modal header={t('Propose a council motion')}>
|
||||
<Modal.Content>
|
||||
<InputAddress
|
||||
filter={members}
|
||||
help={t('Select the account you wish to make the proposal with.')}
|
||||
label={t('propose from account')}
|
||||
onChange={setAcountId}
|
||||
type='account'
|
||||
withLabel
|
||||
/>
|
||||
<InputNumber
|
||||
className='medium'
|
||||
help={t('The minimum number of council votes required to approve this motion')}
|
||||
isError={!threshold || threshold.eqn(0) || threshold.gtn(members.length)}
|
||||
label={t('threshold')}
|
||||
onChange={_setThreshold}
|
||||
placeholder={t('Positive number between 1 and {{memberCount}}', { replace: { memberCount: members.length } })}
|
||||
value={threshold || new BN(0)}
|
||||
/>
|
||||
<Extrinsic
|
||||
defaultValue={apiDefaultTxSudo}
|
||||
label={t('proposal')}
|
||||
onChange={_setMethod}
|
||||
/>
|
||||
</Modal.Content>
|
||||
<Modal.Actions onCancel={toggleOpen}>
|
||||
<TxButton
|
||||
accountId={accountId}
|
||||
isDisabled={!method || !isThresholdValid}
|
||||
label={t('Propose')}
|
||||
params={[threshold, method]}
|
||||
tx='council.propose'
|
||||
/>
|
||||
</Modal.Actions>
|
||||
</Modal>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export default withMulti(
|
||||
translate(Propose),
|
||||
withApi
|
||||
);
|
||||
export default React.memo(Propose);
|
||||
|
||||
@ -7,9 +7,9 @@ import { AccountId } from '@polkadot/types/interfaces';
|
||||
|
||||
import React from 'react';
|
||||
import { Button, Table } from '@polkadot/react-components';
|
||||
import { useMembers } from '@polkadot/react-hooks';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
import useCouncilMembers from '../useCouncilMembers';
|
||||
import Motion from './Motion';
|
||||
import ProposeMotion from './ProposeMotion';
|
||||
import ProposeExternal from './ProposeExternal';
|
||||
@ -23,13 +23,12 @@ interface Props {
|
||||
|
||||
function Proposals ({ className, motions, prime }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const { isMember, members } = useCouncilMembers();
|
||||
const { isMember, members } = useMembers();
|
||||
|
||||
return (
|
||||
<div className={className}>
|
||||
<Button.Group>
|
||||
<ProposeMotion
|
||||
filter={members}
|
||||
isMember={isMember}
|
||||
members={members}
|
||||
/>
|
||||
|
||||
@ -1,37 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/app-democracy authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AccountId, Balance } from '@polkadot/types/interfaces';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useAccounts, useApi, useCall } from '@polkadot/react-hooks';
|
||||
|
||||
interface Result {
|
||||
isMember: boolean;
|
||||
members: string[];
|
||||
}
|
||||
|
||||
function transform (allAccounts: string[], queryMembers: [AccountId, Balance][]): Result {
|
||||
const members = queryMembers.map((accountId): string => accountId.toString());
|
||||
|
||||
return {
|
||||
isMember: members.some((accountId): boolean => allAccounts.includes(accountId)),
|
||||
members
|
||||
};
|
||||
}
|
||||
|
||||
export default function useCouncilMembers (): Result {
|
||||
const { api } = useApi();
|
||||
const { allAccounts } = useAccounts();
|
||||
const [state, setState] = useState<Result>({ isMember: false, members: [] });
|
||||
const queryMembers = useCall<[AccountId, Balance][]>(api.query.council.members, []);
|
||||
|
||||
useEffect((): void => {
|
||||
allAccounts && queryMembers && setState(
|
||||
transform(allAccounts, queryMembers)
|
||||
);
|
||||
}, [allAccounts, queryMembers]);
|
||||
|
||||
return state;
|
||||
}
|
||||
@ -4,11 +4,10 @@
|
||||
|
||||
import { DeriveTreasuryProposals } from '@polkadot/api-derive/types';
|
||||
import { BareProps as Props } from '@polkadot/react-components/types';
|
||||
import { AccountId, Balance } from '@polkadot/types/interfaces';
|
||||
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { Button } from '@polkadot/react-components';
|
||||
import { useAccounts, useApi, useCall, useIncrement, useIsMountedRef } from '@polkadot/react-hooks';
|
||||
import { useApi, useCall, useIncrement, useMembers, useIsMountedRef } from '@polkadot/react-hooks';
|
||||
|
||||
import ProposalCreate from './ProposalCreate';
|
||||
import Proposals from './Proposals';
|
||||
@ -16,17 +15,11 @@ import Summary from './Summary';
|
||||
import TipCreate from './TipCreate';
|
||||
import Tips from './Tips';
|
||||
|
||||
interface Members {
|
||||
isMember: boolean;
|
||||
members: string[];
|
||||
}
|
||||
|
||||
function Overview ({ className }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const info = useCall<DeriveTreasuryProposals>(api.derive.treasury.proposals, []);
|
||||
const { allAccounts } = useAccounts();
|
||||
const queryMembers = useCall<[AccountId, Balance][]>((api.query.electionsPhragmen || api.query.elections).members, []);
|
||||
const [{ isMember, members }, setMembers] = useState<Members>({ isMember: false, members: [] });
|
||||
const { isMember, members } = useMembers();
|
||||
|
||||
const mountedRef = useIsMountedRef();
|
||||
const [hashTrigger, triggerHashes] = useIncrement();
|
||||
const [hashes, setHashes] = useState<string[] | null>(null);
|
||||
@ -41,17 +34,6 @@ function Overview ({ className }: Props): React.ReactElement<Props> {
|
||||
}
|
||||
}, [api, hashTrigger, mountedRef]);
|
||||
|
||||
useEffect((): void => {
|
||||
if (allAccounts && queryMembers) {
|
||||
const members = queryMembers.map(([accountId]) => accountId.toString());
|
||||
|
||||
setMembers({
|
||||
isMember: members.some((accountId) => allAccounts.includes(accountId)),
|
||||
members
|
||||
});
|
||||
}
|
||||
}, [allAccounts, queryMembers]);
|
||||
|
||||
return (
|
||||
<div className={className}>
|
||||
<Summary
|
||||
|
||||
@ -2,7 +2,8 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AccountId, Balance } from '@polkadot/types/interfaces';
|
||||
import { DeriveElectionsInfo } from '@polkadot/api-derive/types';
|
||||
import { AccountId } from '@polkadot/types/interfaces';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useAccounts, useApi, useCall } from '@polkadot/react-hooks';
|
||||
@ -12,16 +13,16 @@ interface Result {
|
||||
members: string[];
|
||||
}
|
||||
|
||||
export default function useMembers (collective: 'council' | 'technicalCommittee'): Result {
|
||||
export default function useMembers (collective: 'council' | 'technicalCommittee' = 'council'): Result {
|
||||
const { api } = useApi();
|
||||
const { allAccounts, hasAccounts } = useAccounts();
|
||||
const [state, setState] = useState<Result>({ isMember: false, members: [] });
|
||||
const retrieved = (
|
||||
collective === 'council'
|
||||
// eslint-disable-next-line react-hooks/rules-of-hooks
|
||||
? useCall<string[]>(hasAccounts && ((api.query.electionsPhragmen || api.query.elections).members), [], {
|
||||
transform: (accounts: [AccountId, Balance][]): string[] =>
|
||||
accounts.map(([accountId]) => accountId.toString())
|
||||
? useCall<string[]>(hasAccounts && api.derive.elections.info, [], {
|
||||
transform: ({ members }: DeriveElectionsInfo): string[] =>
|
||||
members.map(([accountId]) => accountId.toString())
|
||||
})
|
||||
// eslint-disable-next-line react-hooks/rules-of-hooks
|
||||
: useCall<string[]>(hasAccounts && api.query.technicalCommittee.members, [], {
|
||||
@ -32,7 +33,7 @@ export default function useMembers (collective: 'council' | 'technicalCommittee'
|
||||
|
||||
useEffect((): void => {
|
||||
retrieved && setState({
|
||||
isMember: retrieved.some((accountId): boolean => allAccounts.includes(accountId)),
|
||||
isMember: retrieved.some((accountId) => allAccounts.includes(accountId)),
|
||||
members: retrieved
|
||||
});
|
||||
}, [allAccounts, retrieved]);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user