import { CloudUpload, Delete, Person } from '@mui/icons-material'; import { Avatar, Box, Button, CircularProgress, Dialog, DialogActions, DialogContent, DialogTitle, Divider, FormControl, Grid, IconButton, InputLabel, MenuItem, Select, TextField, Typography, useMediaQuery, useTheme, } from '@mui/material'; import { useForm } from '@tanstack/react-form'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { t } from 'i18next'; import { FC, FormEvent, useState } from 'react'; import Api from '../../../api/Api'; import { User, UserImageUpdate } from '../../../types/User'; import ErrorComponent from '../../Error/ErrorComponent'; interface Props { user: User; open: boolean; onClose: () => void; } const UserImageDialog: FC = ({ user, open, onClose }) => { //eslint-disable-next-line @typescript-eslint/no-explicit-any const [error, setError] = useState(); const updateMutation = useMutation({ mutationFn: ({ data, id }: { data: UserImageUpdate; id?: number }) => { return Api.updateUserImage(data, id); }, }); const form = useForm({ onSubmit: async ({ value }) => { try { updateMutation.mutate( { data: value, id: Api.getAuthenticatedUser()?.id === user.id ? undefined : user.id }, { onSuccess: () => { handleClose(); const queryKey = Api.getAuthenticatedUser()?.id === user.id ? ['profile'] : ['profile', { id: user.id }]; queryClient.invalidateQueries({ queryKey }); }, onError: setError, } ); //eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (error: any) { setError(error); } }, }); const theme = useTheme(); const fullScreen = useMediaQuery(theme.breakpoints.only('xs'), { noSsr: true }); const queryClient = useQueryClient(); const formState = form.useStore((state) => ({ image: state.values.image, predefined: state.values.predefined })); const handleClose = () => { form.reset(); setError(undefined); onClose(); }; return ( ) => { e.preventDefault(); e.stopPropagation(); form.handleSubmit(); }, onKeyDown: (event: React.KeyboardEvent) => { if (event.key === 'Tab') { event.stopPropagation(); } }, noValidate: true, }} > {t('Edit image')} form.setFieldValue('image', undefined)}> {t('or')} ( {t('Predefined')} )} /> [state.canSubmit, state.isSubmitting]} children={([canSubmit]) => ( <> )} /> {error && ( )} ); }; export default UserImageDialog;