Pull in collective hooks (from PR) (#2264)
This commit is contained in:
parent
8346474cd4
commit
756203fab8
@ -2,6 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
export { default as useAccountId } from './useAccountId';
|
||||
export { default as useAccounts } from './useAccounts';
|
||||
export { default as useAddresses } from './useAddresses';
|
||||
export { default as useApi } from './useApi';
|
||||
@ -11,6 +12,8 @@ export { default as useForm } from './useForm';
|
||||
export { default as useDebounce } from './useDebounce';
|
||||
export { default as useFavorites } from './useFavorites';
|
||||
export { default as useIsMountedRef } from './useIsMountedRef';
|
||||
export { default as useMembers } from './useMembers';
|
||||
export { default as useModal } from './useModal';
|
||||
export { default as usePassword } from './usePassword';
|
||||
export { default as useToggle } from './useToggle';
|
||||
export { default as useTx } from './useTx';
|
||||
|
||||
@ -24,6 +24,12 @@ export type TxDefs = SubmittableExtrinsic | IExtrinsic | Call | TxDef | null;
|
||||
|
||||
export type TxSource<T extends TxDefs> = [T, boolean];
|
||||
|
||||
export interface ModalState {
|
||||
isOpen: boolean;
|
||||
onOpen: () => void;
|
||||
onClose: () => void;
|
||||
}
|
||||
|
||||
export interface Slash {
|
||||
accountId: AccountId;
|
||||
amount: Balance;
|
||||
|
||||
20
packages/react-hooks/src/useAccountId.tsx
Normal file
20
packages/react-hooks/src/useAccountId.tsx
Normal file
@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2020 @polkadot/react-hooks 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 { StringOrNull } from '@polkadot/react-components/types';
|
||||
|
||||
import { useState } from 'react';
|
||||
|
||||
export default function useAccountId (initialValue: StringOrNull = null, onChangeAccountId?: (_: StringOrNull) => void): [StringOrNull, (_: StringOrNull) => void] {
|
||||
const [accountId, setAccountId] = useState<StringOrNull>(initialValue);
|
||||
|
||||
return [
|
||||
accountId,
|
||||
(accountId: StringOrNull = null): void => {
|
||||
setAccountId(accountId);
|
||||
|
||||
onChangeAccountId && onChangeAccountId(accountId);
|
||||
}
|
||||
];
|
||||
}
|
||||
@ -98,7 +98,7 @@ function subscribe <T> (mounted: MountedRef, tracker: TrackerRef, fn: TrackFn |
|
||||
// - returns a promise with an unsubscribe function
|
||||
// - has a callback to set the value
|
||||
// FIXME The typings here need some serious TLC
|
||||
export default function useCall <T> (fn: TrackFn | undefined | null, params: CallParams, options: CallOptions<T> = {}): T | undefined {
|
||||
export default function useCall <T> (fn: TrackFn | undefined | null, params: CallParams = [], options: CallOptions<T> = {}): T | undefined {
|
||||
const mounted = useIsMountedRef();
|
||||
const tracker = useRef<Tracker>({ isActive: false, count: 0, serialized: null, subscriber: null });
|
||||
const [value, setValue] = useState<T | undefined>(options.defaultValue);
|
||||
|
||||
37
packages/react-hooks/src/useMembers.tsx
Normal file
37
packages/react-hooks/src/useMembers.tsx
Normal file
@ -0,0 +1,37 @@
|
||||
// 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 { useAccounts, useApi, useCall } from '@polkadot/react-hooks';
|
||||
|
||||
interface Result {
|
||||
isMember: boolean;
|
||||
members: string[];
|
||||
}
|
||||
|
||||
function getResult (allAccounts: string[], members: string[]): Result {
|
||||
return {
|
||||
isMember: members.some((accountId): boolean => allAccounts.includes(accountId)),
|
||||
members
|
||||
};
|
||||
}
|
||||
|
||||
export default function useMembers (collective: 'council' | 'technicalCommittee'): Result {
|
||||
const { api } = useApi();
|
||||
const { allAccounts } = useAccounts();
|
||||
const { isMember, members } = (
|
||||
collective === 'council'
|
||||
? useCall<Result>(api.query.electionsPhragmen?.members || api.query.elections.members, [], {
|
||||
transform: (accounts: [AccountId, Balance][]): Result =>
|
||||
getResult(allAccounts, accounts.map(([accountId]) => accountId.toString()))
|
||||
})
|
||||
: useCall<Result>(api.query.technicalCommittee.members, [], {
|
||||
transform: (accounts: AccountId[]): Result =>
|
||||
getResult(allAccounts, accounts.map(accountId => accountId.toString()))
|
||||
})
|
||||
) || { isMember: false, members: [] };
|
||||
|
||||
return { isMember, members };
|
||||
}
|
||||
25
packages/react-hooks/src/useModal.tsx
Normal file
25
packages/react-hooks/src/useModal.tsx
Normal file
@ -0,0 +1,25 @@
|
||||
// Copyright 2017-2020 @polkadot/react-hooks 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 { ModalState } from './types';
|
||||
|
||||
import useToggle from './useToggle';
|
||||
|
||||
export default function useModal (defaultIsOpen?: boolean, onOpen?: () => void, onClose?: () => void): ModalState {
|
||||
const [isOpen, , setIsOpen] = useToggle(defaultIsOpen || false);
|
||||
|
||||
return {
|
||||
isOpen,
|
||||
onOpen: (): void => {
|
||||
setIsOpen(true);
|
||||
|
||||
onOpen && onOpen();
|
||||
},
|
||||
onClose: (): void => {
|
||||
setIsOpen(false);
|
||||
|
||||
onClose && onClose();
|
||||
}
|
||||
};
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user