import * as React from 'react'; import { FormattedMessage } from 'react-intl'; import { useDispatch } from 'react-redux'; import { patchMe } from 'soapbox/actions/me'; import snackbar from 'soapbox/actions/snackbar'; import { Button, Card, CardBody, FormGroup, Input, Stack, Text } from 'soapbox/components/ui'; import { useOwnAccount } from 'soapbox/hooks'; import type { AxiosError } from 'axios'; const DisplayNameStep = ({ onNext }: { onNext: () => void }) => { const dispatch = useDispatch(); const account = useOwnAccount(); const [value, setValue] = React.useState(account?.display_name || ''); const [isSubmitting, setSubmitting] = React.useState(false); const [errors, setErrors] = React.useState([]); const trimmedValue = value.trim(); const isValid = trimmedValue.length > 0; const isDisabled = !isValid || value.length > 30; const hintText = React.useMemo(() => { const charsLeft = 30 - value.length; const suffix = charsLeft === 1 ? 'character remaining' : 'characters remaining'; return `${charsLeft} ${suffix}`; }, [value]); const handleSubmit = () => { setSubmitting(true); const credentials = dispatch(patchMe({ display_name: value })); Promise.all([credentials]) .then(() => { setSubmitting(false); onNext(); }).catch((error: AxiosError) => { setSubmitting(false); if (error.response?.status === 422) { setErrors([(error.response.data as any).error.replace('Validation failed: ', '')]); } else { dispatch(snackbar.error('An unexpected error occurred. Please try again or skip this step.')); } }); }; return (
setValue(event.target.value)} placeholder='Eg. John Smith' type='text' value={value} maxLength={30} />
); }; export default DisplayNameStep;