import classNames from 'clsx'; import React from 'react'; import { defineMessages, FormattedMessage } from 'react-intl'; import { useDispatch } from 'react-redux'; import { patchMe } from 'soapbox/actions/me'; import { Avatar, Button, Card, CardBody, Icon, Spinner, Stack, Text } from 'soapbox/components/ui'; import { useOwnAccount } from 'soapbox/hooks'; import toast from 'soapbox/toast'; import { isDefaultAvatar } from 'soapbox/utils/accounts'; import resizeImage from 'soapbox/utils/resize-image'; import type { AxiosError } from 'axios'; const messages = defineMessages({ error: { id: 'onboarding.error', defaultMessage: 'An unexpected error occurred. Please try again or skip this step.' }, }); const AvatarSelectionStep = ({ onNext }: { onNext: () => void }) => { const dispatch = useDispatch(); const account = useOwnAccount(); const fileInput = React.useRef(null); const [selectedFile, setSelectedFile] = React.useState(); const [isSubmitting, setSubmitting] = React.useState(false); const [isDisabled, setDisabled] = React.useState(true); const isDefault = account ? isDefaultAvatar(account.avatar) : false; const openFilePicker = () => { fileInput.current?.click(); }; const handleFileChange = (event: React.ChangeEvent) => { const maxPixels = 400 * 400; const rawFile = event.target.files?.item(0); if (!rawFile) return; resizeImage(rawFile, maxPixels).then((file) => { const url = file ? URL.createObjectURL(file) : account?.avatar as string; setSelectedFile(url); setSubmitting(true); const formData = new FormData(); formData.append('avatar', rawFile); const credentials = dispatch(patchMe(formData)); Promise.all([credentials]).then(() => { setDisabled(false); setSubmitting(false); onNext(); }).catch((error: AxiosError) => { setSubmitting(false); setDisabled(false); setSelectedFile(null); if (error.response?.status === 422) { toast.error((error.response.data as any).error.replace('Validation failed: ', '')); } else { toast.error(messages.error); } }); }).catch(console.error); }; return (
{account && ( )} {isSubmitting && (
)}
{isDisabled && ( )}
); }; export default AvatarSelectionStep;