import React from 'react'; import { defineMessages, FormattedMessage, useIntl } 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 messages = defineMessages({ usernamePlaceholder: { id: 'onboarding.display_name.placeholder', defaultMessage: 'Eg. John Smith' }, error: { id: 'onboarding.error', defaultMessage: 'An unexpected error occurred. Please try again or skip this step.' }, }); const DisplayNameStep = ({ onNext }: { onNext: () => void }) => { const intl = useIntl(); 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(messages.error)); } }); }; return (
} errors={errors} > setValue(event.target.value)} placeholder={intl.formatMessage(messages.usernamePlaceholder)} type='text' value={value} maxLength={30} />
); }; export default DisplayNameStep;