Skip to content

Commit

Permalink
✨ NABI-387 업로드 컴포넌트 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
oaoong committed Jan 1, 2024
1 parent d88f45b commit 83194c3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
2 changes: 0 additions & 2 deletions src/app/(root)/(routes)/(home)/components/CategorySection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ const CategorySection = () => {
className="w-8 h-8"
alt={`category-${v.key}`}
src={v.image}
quality={50}
sizes="32px"
loading="eager"
/>
<p className={` w-max ${TYPOGRAPHY.description}`}>{v.value}</p>
Expand Down
43 changes: 20 additions & 23 deletions src/components/domain/image-uploader/ImageUploader.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use client'

import React, { useState } from 'react'
import { useMutation } from '@tanstack/react-query'
import { MAX_IMAGE_NUMBER } from '@/constants/image'
import { useToast } from '@/hooks/useToast'
import { postImageFile } from '@/services/images'
import { isNotNull } from '@/utils/isNotNull'
import ImageBlock from './components/ImageBlock'
import UploadBlock from './components/UploadBlock'

Expand All @@ -24,29 +24,25 @@ const ImageUploader = ({
const { toast } = useToast()
const [images, setImages] = useState<string[]>(defaultImages)

async function uploadImages(files: FileList) {
const uploadPromises = Array.from(files).map(async (file) => {
try {
const res = await postImageFile(file)
return res.data
} catch (e) {
toast({
title: '이미지 업로드 실패',
description: '이미지 업로드에 실패했습니다. 다시 시도해주세요.',
})
return null
}
})

const uploadedImages = await Promise.all(uploadPromises)

const successfulUploads = uploadedImages
.filter((imageUrl) => imageUrl != null)
.map((imageUrl) => imageUrl)
.filter(isNotNull)
const uploadImageMutation = useMutation({
mutationFn: postImageFile,
mutationKey: ['postImage'],
onSuccess: (data) => {
setImages((currentImages) => [...currentImages, data.data])
onFilesChanged([...images, data.data])
},
onError: () => {
toast({
title: '이미지 업로드 실패',
description: '이미지 업로드에 실패했습니다. 다시 시도해주세요.',
})
},
})

setImages((images) => [...images, ...successfulUploads])
onFilesChanged([...images, ...successfulUploads])
const uploadImages = (files: FileList) => {
Array.from(files).forEach((file) => {
uploadImageMutation.mutate(file)
})
}

const onUploadHandler = (e: React.ChangeEvent<HTMLInputElement>) => {
Expand Down Expand Up @@ -74,6 +70,7 @@ const ImageUploader = ({
onUploadHandler={onUploadHandler}
currentPhotoNumber={images.length}
maxPhotoNumber={maxImageNumber}
isUploading={uploadImageMutation.isPending}
/>
{images.map((image, index) => {
const isThumbnail = index === 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ type UploadBlockType = {
onUploadHandler: (_e: React.ChangeEvent<HTMLInputElement>) => void
currentPhotoNumber: number
maxPhotoNumber: number
isUploading: boolean
}

const UploadBlock = ({
onUploadHandler,
currentPhotoNumber = 0,
maxPhotoNumber,
isUploading,
}: UploadBlockType) => {
const inputRef = useRef<HTMLInputElement>(null)

Expand All @@ -35,7 +37,11 @@ const UploadBlock = ({
TYPOGRAPHY.description,
'text-text-secondary-color',
)}
>{`${currentPhotoNumber}/${maxPhotoNumber}`}</span>
>
{isUploading
? `업로드 중`
: `${currentPhotoNumber}/${maxPhotoNumber}`}
</span>
</div>
<Input
ref={inputRef}
Expand Down
10 changes: 5 additions & 5 deletions src/services/images/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ const postImageFile = async (file: File) => {
const formData = new FormData()
formData.append('file', file)

const response = await apiClient.post(
ApiEndPoint.postImageFile(),
formData,
{},
)
const response: Promise<{
code: string
data: string
messages: string
}> = await apiClient.post(ApiEndPoint.postImageFile(), formData, {})

return response
}
Expand Down

0 comments on commit 83194c3

Please sign in to comment.