import React from 'react'; import { AccountService } from '../../account'; import { HelpField } from '../../help/HelpField'; import { Markdown } from '../../presentation/Markdown'; const { useEffect, useState } = React; export interface IUserVerificationProps { label?: string; onValidChange: (isValid: boolean) => void; // Provide one of: expectedValue?: string; account?: string; } export function UserVerification(props: IUserVerificationProps) { const { label, account } = props; const [value, setValue] = useState(''); if (props.account && props.expectedValue) { throw new Error("Supply either the 'account' or the 'expectedValue' prop, but not both"); } const expectedValue = props.expectedValue || account; const matches = value === expectedValue; const [challengeDestructiveActions, setChallengeDestructiveActions] = useState(true); useEffect(() => { account && AccountService.challengeDestructiveActions(account).then(setChallengeDestructiveActions); }, [account]); const hideComponent = account && !challengeDestructiveActions; useEffect(() => props.onValidChange(hideComponent || matches), [matches, hideComponent]); const defaultLabel = ( <> Type the name of the account ({expectedValue}) to continue ); const className = `form-control input-sm highlight-pristine ${matches ? '' : 'ng-invalid'}`; return hideComponent ? null : (
{label ? : defaultLabel}
{' '} setValue(evt.target.value)} />
); }