@polkadot/types#Data TypeScript Examples
The following examples show how to use
@polkadot/types#Data.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IdentitySub.tsx From crust-apps with Apache License 2.0 | 6 votes |
function extractInfo ([[ids], opts]: [[string[]], Option<ITuple<[AccountId, Data]>>[]]): [string, string][] {
return ids.reduce((result: [string, string][], id, index): [string, string][] => {
const opt = opts[index];
if (opt.isSome) {
const [, data] = opt.unwrap();
if (data.isRaw) {
result.push([id, u8aToString(data.asRaw)]);
}
}
return result;
}, []);
}
Example #2
Source File: utils.ts From polkadot-registrar-watcher with Apache License 2.0 | 6 votes |
isDataPresent = (data: Data): boolean => {
return !data.isNull && !data.isEmpty && !data.isNone
}
Example #3
Source File: IdentityMain.tsx From crust-apps with Apache License 2.0 | 5 votes |
function setData (data: Data, setActive: null | ((isActive: boolean) => void), setVal: (val: string) => void): void {
if (data.isRaw) {
setActive && setActive(true);
setVal(u8aToString(data.asRaw.toU8a(true)));
}
}
Example #4
Source File: IdentityMain.tsx From crust-apps with Apache License 2.0 | 4 votes |
function IdentityMain ({ address, className = '', onClose }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const identityOpt = useCall<Option<Registration>>(api.query.identity.identityOf, [address]);
const [{ info, okAll, okDisplay, okEmail, okLegal, okRiot, okTwitter, okWeb }, setInfo] = useState<ValueState>({ info: {}, okAll: false });
const [hasEmail, setHasEmail] = useState(false);
const [hasLegal, setHasLegal] = useState(false);
const [hasRiot, setHasRiot] = useState(false);
const [hasTwitter, setHasTwitter] = useState(false);
const [hasWeb, setHasWeb] = useState(false);
const [valDisplay, setValDisplay] = useState(() => (getAddressMeta(address).name || '').replace(/\(.*\)/, '').trim());
const [valEmail, setValEmail] = useState('');
const [valLegal, setValLegal] = useState('');
const [valRiot, setValRiot] = useState('');
const [valTwitter, setValTwitter] = useState('');
const [valWeb, setValWeb] = useState('');
const [gotPreviousIdentity, setGotPreviousIdentity] = useState(false);
useEffect((): void => {
if (identityOpt && identityOpt.isSome) {
const { info } = identityOpt.unwrap();
setData(info.display, null, setValDisplay);
setData(info.email, setHasEmail, setValEmail);
setData(info.legal, setHasLegal, setValLegal);
setData(info.riot, setHasRiot, setValRiot);
setData(info.twitter, setHasTwitter, setValTwitter);
setData(info.web, setHasWeb, setValWeb);
[info.display, info.email, info.legal, info.riot, info.twitter, info.web].some((info: Data) => {
if (info.isRaw) {
setGotPreviousIdentity(true);
return true;
} else {
return false;
}
});
}
}, [identityOpt]);
useEffect((): void => {
const okDisplay = checkValue(true, valDisplay, 1, [], [], []);
const okEmail = checkValue(hasEmail, valEmail, 3, ['@'], WHITESPACE, []);
const okLegal = checkValue(hasLegal, valLegal, 1, [], [], []);
const okRiot = checkValue(hasRiot, valRiot, 6, [':'], WHITESPACE, ['@', '~']);
const okTwitter = checkValue(hasTwitter, valTwitter, 3, [], WHITESPACE, ['@']);
const okWeb = checkValue(hasWeb, valWeb, 8, ['.'], WHITESPACE, ['https://', 'http://']);
setInfo({
info: {
display: { [okDisplay ? 'raw' : 'none']: valDisplay || null },
email: { [okEmail && hasEmail ? 'raw' : 'none']: okEmail && hasEmail ? valEmail : null },
legal: { [okLegal && hasLegal ? 'raw' : 'none']: okLegal && hasLegal ? valLegal : null },
riot: { [okRiot && hasRiot ? 'raw' : 'none']: okRiot && hasRiot ? valRiot : null },
twitter: { [okTwitter && hasTwitter ? 'raw' : 'none']: okTwitter && hasTwitter ? valTwitter : null },
web: { [okWeb && hasWeb ? 'raw' : 'none']: okWeb && hasWeb ? valWeb : null }
},
okAll: okDisplay && okEmail && okLegal && okRiot && okTwitter && okWeb,
okDisplay,
okEmail,
okLegal,
okRiot,
okTwitter,
okWeb
});
}, [hasEmail, hasLegal, hasRiot, hasTwitter, hasWeb, valDisplay, valEmail, valLegal, valRiot, valTwitter, valWeb]);
return (
<Modal
className={className}
header={t<string>('Register identity')}
>
<Modal.Content>
<Input
autoFocus
help={t<string>('The name that will be displayed in your accounts list.')}
isError={!okDisplay}
label={t<string>('display name')}
maxLength={32}
onChange={setValDisplay}
placeholder={t('My On-Chain Name')}
value={valDisplay}
/>
<WrapToggle
onChange={setHasLegal}
value={hasLegal}
>
<Input
help={t<string>('The legal name for this identity.')}
isDisabled={!hasLegal}
isError={!okLegal}
label={t<string>('legal name')}
maxLength={32}
onChange={setValLegal}
placeholder={t('Full Legal Name')}
value={hasLegal ? valLegal : '<none>'}
/>
</WrapToggle>
<WrapToggle
onChange={setHasEmail}
value={hasEmail}
>
<Input
help={t<string>('The email address associated with this identity.')}
isDisabled={!hasEmail}
isError={!okEmail}
label={t<string>('email')}
maxLength={32}
onChange={setValEmail}
placeholder={t('[email protected]')}
value={hasEmail ? valEmail : '<none>'}
/>
</WrapToggle>
<WrapToggle
onChange={setHasWeb}
value={hasWeb}
>
<Input
help={t<string>('An URL that is linked to this identity.')}
isDisabled={!hasWeb}
isError={!okWeb}
label={t<string>('web')}
maxLength={32}
onChange={setValWeb}
placeholder={t('https://example.com')}
value={hasWeb ? valWeb : '<none>'}
/>
</WrapToggle>
<WrapToggle
onChange={setHasTwitter}
value={hasTwitter}
>
<Input
help={t<string>('The twitter name for this identity.')}
isDisabled={!hasTwitter}
isError={!okTwitter}
label={t<string>('twitter')}
onChange={setValTwitter}
placeholder={t('@YourTwitterName')}
value={hasTwitter ? valTwitter : '<none>'}
/>
</WrapToggle>
<WrapToggle
onChange={setHasRiot}
value={hasRiot}
>
<Input
help={t<string>('a riot name linked to this identity')}
isDisabled={!hasRiot}
isError={!okRiot}
label={t<string>('riot name')}
maxLength={32}
onChange={setValRiot}
placeholder={t('@yourname:matrix.org')}
value={hasRiot ? valRiot : '<none>'}
/>
</WrapToggle>
<InputBalance
defaultValue={api.consts.identity?.basicDeposit}
help={t<string>('Total amount of fund that will be reserved. These funds are returned when the identity is cleared')}
isDisabled
label={t<string>('total deposit')}
/>
</Modal.Content>
<Modal.Actions onCancel={onClose}>
<TxButton
accountId={address}
icon={'trash-alt'}
isDisabled={!gotPreviousIdentity}
label={t<string>('Clear Identity')}
onStart={onClose}
tx={api.tx.identity.clearIdentity}
/>
<TxButton
accountId={address}
isDisabled={!okAll}
label={t<string>('Set Identity')}
onStart={onClose}
params={[info]}
tx={api.tx.identity.setIdentity}
/>
</Modal.Actions>
</Modal>
);
}
Example #5
Source File: IdentitySub.tsx From crust-apps with Apache License 2.0 | 4 votes |
function IdentitySubModal ({ address, className, onClose }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const { allAccounts } = useAccounts();
const queryIds = useCall<string[]>(api.query.identity.subsOf, [address], transformIds);
const queryInfos = useCall<[[string[]], Option<ITuple<[AccountId, Data]>>[]]>(queryIds && queryIds.length !== 0 && api.query.identity.superOf.multi, [queryIds], transformInfo);
const [infos, setInfos] = useState<[string, string][] | undefined>();
useEffect((): void => {
if (queryInfos) {
setInfos(extractInfo(queryInfos));
} else if (queryIds && !queryIds.length) {
setInfos([]);
}
}, [allAccounts, queryIds, queryInfos]);
const _rowAdd = useCallback(
() => setInfos((infos) => infos && infos.concat([[allAccounts[0], '']])),
[allAccounts]
);
const _rowRemove = useCallback(
() => setInfos((infos) => infos && infos.slice(0, infos.length - 1)),
[]
);
const _setAddress = useCallback(
(index: number, address: string) => setInfos((infos) => (infos || []).map(([a, n], i) => [index === i ? address : a, n])),
[]
);
const _setName = useCallback(
(index: number, name: string) => setInfos((infos) => (infos || []).map(([a, n], i) => [a, index === i ? name : n])),
[]
);
return (
<Modal
className={className}
header={t<string>('Register sub-identities')}
size='large'
>
<Modal.Content>
{!infos
? <Spinner label={t<string>('Retrieving sub-identities')} />
: (
<div>
{!infos.length
? <article>{t('No sub identities set.')}</article>
: infos.map(([address, name], index) =>
<IdentitySubMemo
address={address}
index={index}
key={index}
name={name}
setAddress={_setAddress}
setName={_setName}
t={t}
/>
)
}
<Button.Group>
<Button
icon='plus'
label={t<string>('Add sub')}
onClick={_rowAdd}
/>
<Button
icon='minus'
isDisabled={infos.length === 0}
label={t<string>('Remove sub')}
onClick={_rowRemove}
/>
</Button.Group>
</div>
)
}
</Modal.Content>
<Modal.Actions onCancel={onClose}>
{infos && (
<TxButton
accountId={address}
isDisabled={infos.some(([address, raw]) => !address || !raw)}
label={t<string>('Set Subs')}
onStart={onClose}
params={[
infos.map(([address, raw]) => [address, { raw }])
]}
tx={api.tx.identity.setSubs}
/>
)}
</Modal.Actions>
</Modal>
);
}