import { useEffect, useMemo, useState } from 'react' import Alert from '@mui/material/Alert' import Box from '@mui/material/Box' import Button from '@mui/material/Button' import Dialog from '@mui/material/Dialog' import DialogActions from '@mui/material/DialogActions' import DialogContent from '@mui/material/DialogContent' import DialogTitle from '@mui/material/DialogTitle' import Stack from '@mui/material/Stack' import TextField from '@mui/material/TextField' import Typography from '@mui/material/Typography' import axios from 'axios' import { PAYMENT_TRANSFER_INSTRUCTIONS_PLAIN } from '@/shared/constants/payment-instructions' type Props = { open: boolean isPending: boolean error: unknown onClose: () => void onSubmit: (params: { detail: string; receiptFile: File | null }) => void } function paySubmitErrorMessage(err: unknown): string { if (axios.isAxiosError(err)) { const raw = err.response?.data const apiMsg = raw && typeof raw === 'object' && 'error' in raw && typeof (raw as { error: unknown }).error === 'string' ? (raw as { error: string }).error : null return apiMsg || err.message || 'Не удалось отправить данные оплаты' } if (err instanceof Error) return err.message return 'Не удалось отправить данные оплаты' } export function PaymentDialog({ open, isPending, error, onClose, onSubmit }: Props) { const [detail, setDetail] = useState('') const [receiptFile, setReceiptFile] = useState(null) const [clientError, setClientError] = useState(null) const receiptPreviewUrl = useMemo(() => { if (!receiptFile) return null return URL.createObjectURL(receiptFile) }, [receiptFile]) useEffect(() => { if (!receiptPreviewUrl) return return () => URL.revokeObjectURL(receiptPreviewUrl) }, [receiptPreviewUrl]) const reset = () => { setDetail('') setReceiptFile(null) setClientError(null) } const handleClose = () => { if (isPending) return reset() onClose() } const handleSubmit = () => { const hasText = detail.trim().length > 0 const hasFile = Boolean(receiptFile) if (!hasText && !hasFile) { setClientError('Укажите комментарий и/или прикрепите чек.') return } setClientError(null) onSubmit({ detail: detail.trim(), receiptFile }) } return ( Подтверждение оплаты {PAYMENT_TRANSFER_INSTRUCTIONS_PLAIN} { setDetail(e.target.value) setClientError(null) }} fullWidth multiline minRows={3} sx={{ mb: 2 }} /> {receiptFile && ( )} Нужен текст комментария и/или изображение чека. {receiptPreviewUrl && ( )} {clientError && ( {clientError} )} {error ? ( {paySubmitErrorMessage(error)} ) : null} ) }