Skip to content

Commit

Permalink
fixed image upload input failure not erroring
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Sep 24, 2024
1 parent 62aff6c commit f74fb8a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Close } from '@mui/icons-material'
import { ComponentType } from 'react'
import { useFieldArray, useFormContext } from 'react-hook-form'
import toast from 'react-hot-toast'
import { useTranslation } from 'react-i18next'

import {
Expand All @@ -18,6 +19,7 @@ import {
} from '@dao-dao/stateless'
import { TransProps } from '@dao-dao/types'
import {
processError,
transformIpfsUrlToHttpsIfNecessary,
validateRequired,
} from '@dao-dao/utils'
Expand Down Expand Up @@ -125,6 +127,7 @@ export const ContributionFormInput = ({
setValue(`files.${index}.url`, url)
setValue(`files.${index}.mimetype`, file.type)
}}
onError={(err) => toast.error(processError(err))}
/>
) : (
<FileUploadInput
Expand All @@ -134,6 +137,7 @@ export const ContributionFormInput = ({
setValue(`files.${index}.url`, url)
setValue(`files.${index}.mimetype`, file.type)
}}
onError={(err) => toast.error(processError(err))}
/>
))
)}
Expand Down
11 changes: 10 additions & 1 deletion packages/stateless/components/inputs/ImageDropInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CloudDownloadRounded, CloudUploadRounded } from '@mui/icons-material'
import clsx from 'clsx'
import { ComponentType, useEffect, useState } from 'react'
import { ComponentType, MutableRefObject, useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'

import { TransProps } from '@dao-dao/types'
Expand All @@ -12,6 +12,10 @@ export type ImageDropInputProps = Pick<
FileDropInputProps,
'onSelect' | 'className' | 'loading'
> & {
/**
* Ref that gets set to a function that clears the image.
*/
clearImageRef?: MutableRefObject<() => void>
currentImage?: string
Trans: ComponentType<TransProps>
}
Expand All @@ -20,6 +24,7 @@ export const ImageDropInput = ({
onSelect: _onSelect,
className,
loading,
clearImageRef,
currentImage,
Trans,
}: ImageDropInputProps) => {
Expand All @@ -32,6 +37,10 @@ export const ImageDropInput = ({
_onSelect(file, url)
}

if (clearImageRef) {
clearImageRef.current = () => setImage(undefined)
}

// If passed in image changes, update the image.
useEffect(() => {
if (currentImage) {
Expand Down
13 changes: 11 additions & 2 deletions packages/stateless/components/inputs/ImageUploadInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react'
import { useRef, useState } from 'react'

import { ImageDropInput, ImageDropInputProps } from './ImageDropInput'

Expand All @@ -16,6 +16,7 @@ export const ImageUploadInput = ({
...props
}: ImageUploadInputProps) => {
const [uploading, setUploading] = useState(false)
const clearImageRef = useRef<() => void>(() => {})

const uploadFile = async (file: File) => {
setUploading(true)
Expand Down Expand Up @@ -47,10 +48,18 @@ export const ImageUploadInput = ({
} catch (err) {
console.error(err)
onError?.(err)
clearImageRef.current()
} finally {
setUploading(false)
}
}

return <ImageDropInput loading={uploading} onSelect={uploadFile} {...props} />
return (
<ImageDropInput
clearImageRef={clearImageRef}
loading={uploading}
onSelect={uploadFile}
{...props}
/>
)
}

0 comments on commit f74fb8a

Please sign in to comment.