Skip to content

Commit

Permalink
feat(image): await direct upload process
Browse files Browse the repository at this point in the history
refs:
- 2023-09-dev-02#RB-0003
  • Loading branch information
robertu7 committed Oct 6, 2023
1 parent f34e47b commit da879ac
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 17 deletions.
12 changes: 6 additions & 6 deletions src/components/Editor/SetCover/Uploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
toast,
Translate,
useCreateDraft,
useDirectImageUpload,
useMutation,
useRoute,
useUnloadConfirm,
Expand Down Expand Up @@ -77,6 +78,7 @@ const Uploader: React.FC<UploaderProps> = ({
undefined,
{ showToast: false }
)
const { upload: uploadImage, uploading } = useDirectImageUpload()

const { isInPath } = useRoute()
const { createDraft } = useCreateDraft()
Expand Down Expand Up @@ -123,9 +125,7 @@ const Uploader: React.FC<UploaderProps> = ({
const { id: assetId, path, uploadURL } = data?.directImageUpload || {}

if (assetId && path && uploadURL) {
const formData = new FormData()
formData.append('file', file)
await fetch(uploadURL, { method: 'POST', body: formData })
await uploadImage({ uploadURL, file })

// (async) mark asset draft as false
directImageUploadDone({
Expand All @@ -151,10 +151,10 @@ const Uploader: React.FC<UploaderProps> = ({

const labelClasses = classNames({
[styles.uploader]: true,
'u-area-disable': loading,
'u-area-disable': loading || uploading,
})

useUnloadConfirm({ block: loading })
useUnloadConfirm({ block: loading || uploading })

return (
<label className={labelClasses} htmlFor={fieldId}>
Expand All @@ -169,7 +169,7 @@ const Uploader: React.FC<UploaderProps> = ({
<Translate id="uploadCover" />
</TextIcon>

{loading && <IconSpinner16 color="greyLight" />}
{(loading || uploading) && <IconSpinner16 color="greyLight" />}
</h3>

<p>
Expand Down
13 changes: 9 additions & 4 deletions src/components/FileUploader/AvatarUploader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
Spinner,
toast,
Translate,
useDirectImageUpload,
useMutation,
} from '~/components'
import {
Expand Down Expand Up @@ -65,6 +66,8 @@ export const AvatarUploader: React.FC<AvatarUploaderProps> = ({
undefined,
{ showToast: false }
)
const { upload: uploadImage, uploading } = useDirectImageUpload()

const [avatar, setAvatar] = useState<string | undefined | null>(
avatarProps.src
)
Expand Down Expand Up @@ -104,9 +107,7 @@ export const AvatarUploader: React.FC<AvatarUploaderProps> = ({
const { id: assetId, path, uploadURL } = data?.directImageUpload || {}

if (assetId && path && uploadURL) {
const formData = new FormData()
formData.append('file', file)
await fetch(uploadURL, { method: 'POST', body: formData })
await uploadImage({ uploadURL, file })

// (async) mark asset draft as false
directImageUploadDone({
Expand Down Expand Up @@ -144,7 +145,11 @@ export const AvatarUploader: React.FC<AvatarUploaderProps> = ({
{isCircle && <CircleAvatar size="xxxl" {...avatarProps} src={avatar} />}

<div className={styles.mask}>
{loading ? <Spinner /> : <IconCamera24 color="white" size="lg" />}
{loading || uploading ? (
<Spinner />
) : (
<IconCamera24 color="white" size="lg" />
)}
</div>

<VisuallyHidden>
Expand Down
13 changes: 9 additions & 4 deletions src/components/FileUploader/CoverUploader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
Spinner,
toast,
Translate,
useDirectImageUpload,
useMutation,
} from '~/components'
import {
Expand Down Expand Up @@ -97,6 +98,8 @@ export const CoverUploader = ({
undefined,
{ showToast: false }
)
const { upload: uploadImage, uploading } = useDirectImageUpload()

const acceptTypes =
type === 'collection'
? ACCEPTED_COLLECTION_UPLOAD_IMAGE_TYPES.join(',')
Expand Down Expand Up @@ -130,9 +133,7 @@ export const CoverUploader = ({
const { id: assetId, path, uploadURL } = data?.directImageUpload || {}

if (assetId && path && uploadURL) {
const formData = new FormData()
formData.append('file', file)
await fetch(uploadURL, { method: 'POST', body: formData })
await uploadImage({ uploadURL, file })

// (async) mark asset draft as false
directImageUploadDone({
Expand All @@ -159,7 +160,11 @@ export const CoverUploader = ({

const Mask = () => (
<div className={styles.mask}>
{loading ? <Spinner /> : <IconCamera24 color="white" size="xl" />}
{loading || uploading ? (
<Spinner />
) : (
<IconCamera24 color="white" size="xl" />
)}
</div>
)

Expand Down
1 change: 1 addition & 0 deletions src/components/Hook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './useCountdown'
export * from './useCreateDraft'
export * from './useCuration'
export * from './useDialogSwitch'
export * from './useDirectImageUpload'
export * from './useERC20'
export * from './useEventListener'
export * from './useFeatures'
Expand Down
27 changes: 27 additions & 0 deletions src/components/Hook/useDirectImageUpload.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useState } from 'react'

export const useDirectImageUpload = () => {
const [uploading, setUploading] = useState(false)

const upload = async ({
uploadURL,
file,
}: {
uploadURL: string
file: File
}) => {
setUploading(true)

try {
const formData = new FormData()
formData.append('file', file)
await fetch(uploadURL, { method: 'POST', body: formData })
setUploading(false)
} catch (error) {
setUploading(false)
throw error
}
}

return { upload, uploading }
}
7 changes: 4 additions & 3 deletions src/views/Me/DraftDetail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
Throw404,
toast,
useCreateDraft,
useDirectImageUpload,
useRoute,
useUnloadConfirm,
} from '~/components'
Expand Down Expand Up @@ -101,6 +102,8 @@ const DraftDetail = () => {
undefined,
{ showToast: false }
)
const { upload: uploadImage } = useDirectImageUpload()

const [saveStatus, setSaveStatus] = useState<
'saved' | 'saving' | 'saveFailed'
>()
Expand Down Expand Up @@ -187,9 +190,7 @@ const DraftDetail = () => {

if (assetId && path && uploadURL) {
try {
const data = new FormData()
data.append('file', input.file)
await fetch(uploadURL, { method: 'POST', body: data })
await uploadImage({ uploadURL, file: input.file })
} catch (error) {
console.error(error)
throw new Error('upload not successful')
Expand Down

0 comments on commit da879ac

Please sign in to comment.