-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shared): 계정정보 수정 페이지 프로필사진 업로드 기능 추가 (#279)
* refactor(shared): useUploadPhoto hook 이름을 usePhotosUpload 로 변경 * feat(shared): 한 개의 이미지만 관리하는 usePhotoUpload hook 추가 * feat(shelter): 보호소 계정정보 수정 페이지에 프로필 이미지 업로드 기능 추가 * feat(volunteer): 봉사자 계정정보 수정 페이지에 프로필 이미지 업로드 기능 추가 * feat(shared): usePhotosUpload 의 getLocalImageUrls 함수에서 createObjectUrl 을 사용하도록 수정
- Loading branch information
Showing
8 changed files
with
123 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useToast, UseToastOptions } from '@chakra-ui/react'; | ||
import { useState } from 'react'; | ||
|
||
import { uploadImage } from '../apis/common/Image'; | ||
import { resizeImageFile } from '../utils/image'; | ||
|
||
const toastOption = ( | ||
description: string, | ||
status: UseToastOptions['status'], | ||
): UseToastOptions => { | ||
return { | ||
description, | ||
position: 'top', | ||
status, | ||
duration: 1500, | ||
isClosable: true, | ||
}; | ||
}; | ||
|
||
export const usePhotoUpload = (initialPhoto?: string) => { | ||
const [photo, setPhoto] = useState<string | undefined>(initialPhoto); | ||
|
||
const toast = useToast(); | ||
|
||
const handleUploadPhoto = async (files: FileList | null) => { | ||
if (!files) { | ||
return; | ||
} | ||
|
||
if (files.length !== 1) { | ||
toast(toastOption('프로필 이미지는 한 장만 등록 가능합니다.', 'error')); | ||
|
||
return; | ||
} | ||
|
||
const imageFile = files[0]; | ||
const localImageUrl = URL.createObjectURL(imageFile); | ||
setPhoto(localImageUrl); | ||
|
||
const resizedImage = await resizeImageFile(imageFile, 2); | ||
const formData = new FormData(); | ||
formData.append('images', resizedImage); | ||
|
||
const { data } = await uploadImage(formData); | ||
const [imageUrl] = data.imageUrls; | ||
|
||
setPhoto(imageUrl); | ||
}; | ||
|
||
const handleDeletePhoto = () => { | ||
setPhoto(undefined); | ||
}; | ||
|
||
return { | ||
photo, | ||
setPhoto, | ||
handleUploadPhoto, | ||
handleDeletePhoto, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters