test commit
This commit is contained in:
@@ -20,8 +20,8 @@ type Props = {
|
||||
error: unknown
|
||||
uploadError: unknown
|
||||
onClose: () => void
|
||||
onSubmit: (params: { rating: number; text: string; imageUrl: string | null }) => void
|
||||
onUploadImage: (file: File) => void
|
||||
onSubmit: (params: { rating: number; text: string; imageUrl: string | null }) => Promise<void>
|
||||
onUploadImage: (file: File) => Promise<{ url: string }>
|
||||
}
|
||||
|
||||
function reviewSubmitErrorMessage(err: unknown): string {
|
||||
@@ -55,11 +55,13 @@ export function ReviewDialog({
|
||||
const [rating, setRating] = useState<number>(5)
|
||||
const [text, setText] = useState('')
|
||||
const [imageUrl, setImageUrl] = useState<string | null>(null)
|
||||
const [localUploadError, setLocalUploadError] = useState<string | null>(null)
|
||||
|
||||
const reset = () => {
|
||||
setRating(5)
|
||||
setText('')
|
||||
setImageUrl(null)
|
||||
setLocalUploadError(null)
|
||||
}
|
||||
|
||||
const handleClose = () => {
|
||||
@@ -68,9 +70,9 @@ export function ReviewDialog({
|
||||
onClose()
|
||||
}
|
||||
|
||||
const handleSubmit = () => {
|
||||
const handleSubmit = async () => {
|
||||
if (isPending) return
|
||||
onSubmit({ rating, text: text.trim(), imageUrl })
|
||||
await onSubmit({ rating, text: text.trim(), imageUrl })
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -96,11 +98,19 @@ export function ReviewDialog({
|
||||
hidden
|
||||
type="file"
|
||||
accept="image/png,image/jpeg,image/webp"
|
||||
onChange={(e) => {
|
||||
onChange={async (e) => {
|
||||
const file = e.target.files?.[0]
|
||||
if (!file) return
|
||||
onUploadImage(file)
|
||||
e.currentTarget.value = ''
|
||||
setLocalUploadError(null)
|
||||
try {
|
||||
const result = await onUploadImage(file)
|
||||
setImageUrl(result.url)
|
||||
} catch (err) {
|
||||
setLocalUploadError(
|
||||
err instanceof Error ? err.message : 'Не удалось загрузить фото. Разрешены png, jpg, jpeg, webp.',
|
||||
)
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</Button>
|
||||
@@ -126,11 +136,13 @@ export function ReviewDialog({
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
{uploadError ? (
|
||||
{uploadError || localUploadError ? (
|
||||
<Alert severity="error" sx={{ mt: 2 }}>
|
||||
{uploadError instanceof Error
|
||||
? uploadError.message
|
||||
: 'Не удалось загрузить фото. Разрешены png, jpg, jpeg, webp.'}
|
||||
{localUploadError
|
||||
? localUploadError
|
||||
: uploadError instanceof Error
|
||||
? uploadError.message
|
||||
: 'Не удалось загрузить фото. Разрешены png, jpg, jpeg, webp.'}
|
||||
</Alert>
|
||||
) : null}
|
||||
{error ? (
|
||||
|
||||
@@ -17,7 +17,12 @@ type Props = {
|
||||
isUploadPending: boolean
|
||||
submitError: unknown
|
||||
uploadError: unknown
|
||||
onSubmitReview: (params: { productId: string; rating: number; text: string; imageUrl: string | null }) => void
|
||||
onSubmitReview: (params: {
|
||||
productId: string
|
||||
rating: number
|
||||
text: string
|
||||
imageUrl: string | null
|
||||
}) => Promise<void>
|
||||
onUploadImage: (file: File) => Promise<{ url: string }>
|
||||
}
|
||||
|
||||
@@ -75,17 +80,20 @@ export function ReviewSection({
|
||||
setTarget(null)
|
||||
setUploadedImageUrl(null)
|
||||
}}
|
||||
onSubmit={(params) => {
|
||||
onSubmit={async (params) => {
|
||||
if (!target) return
|
||||
onSubmitReview({
|
||||
await onSubmitReview({
|
||||
productId: target.productId,
|
||||
...params,
|
||||
imageUrl: uploadedImageUrl,
|
||||
})
|
||||
setTarget(null)
|
||||
setUploadedImageUrl(null)
|
||||
}}
|
||||
onUploadImage={async (file) => {
|
||||
const result = await onUploadImage(file)
|
||||
setUploadedImageUrl(result.url)
|
||||
return result
|
||||
}}
|
||||
/>
|
||||
</Box>
|
||||
|
||||
Reference in New Issue
Block a user