1- import { useEffect , useState } from 'react' ;
1+ import { ChangeEvent , useEffect , useRef , useState } from 'react' ;
22import { useNavigate } from 'react-router-dom' ;
33import { IconButton } from '@/components/IconButton' ;
44import {
@@ -13,6 +13,8 @@ import {
1313 StateMessageInput ,
1414 StateMessagePlus ,
1515 ErrorMessage ,
16+ ProfileImageResetButton ,
17+ ProfileImageContainer ,
1618} from './index.css' ;
1719
1820import Edit from '@/assets/img/Edit.svg' ;
@@ -23,15 +25,17 @@ import { useUserStore } from '@/stores/useUserStore';
2325import DefaultProfile from '@/assets/img/DefaultProfile.svg' ;
2426import AddIcon from '@/assets/img/Add.svg' ;
2527import { AxiosError } from 'axios' ;
28+ import { uploadImageToS3 } from '@/utils/uploadFile' ;
2629
2730export const MyProfile = ( ) => {
28- const { user, updateMyProfile } = useUserStore ( ) ;
31+ const { user, updateMyProfile, updateProfileImage } = useUserStore ( ) ;
2932 const navigate = useNavigate ( ) ;
3033 const [ isEditMode , setIsEditMode ] = useState ( false ) ;
3134 const [ nickname , setNickname ] = useState ( user ?. nickname ) ;
3235 const [ stateMessage , setStateMessage ] = useState ( user ?. stateMessage || null ) ;
3336 const [ isChanged , setIsChanged ] = useState ( false ) ;
3437 const [ errorMessage , setErrorMessage ] = useState ( '' ) ;
38+ const fileInputRef = useRef < HTMLInputElement > ( null ) ;
3539
3640 useEffect ( ( ) => {
3741 if ( user ) {
@@ -92,11 +96,56 @@ export const MyProfile = () => {
9296 setStateMessage ( user ?. stateMessage ) ;
9397 } ;
9498
99+ const handleImageClick = ( ) => {
100+ if ( isEditMode ) {
101+ fileInputRef . current ?. click ( ) ;
102+ }
103+ } ;
104+
105+ const handleImageChange = async ( e : ChangeEvent < HTMLInputElement > ) => {
106+ const file = e . target . files ?. [ 0 ] ;
107+ if ( ! file ) return ;
108+
109+ try {
110+ const imageUrl = await uploadImageToS3 ( file ) ;
111+ const updateUser = await updateProfileImage ( imageUrl ) ;
112+ console . log ( 'image change' , updateUser ) ;
113+ } catch {
114+ setErrorMessage ( '이미지 업로드에 실패했습니다.' ) ;
115+ }
116+ } ;
117+
118+ const handleImageReset = async ( ) => {
119+ const updateUser = await updateProfileImage ( null ) ;
120+ console . log ( 'image reset' , updateUser ) ;
121+ } ;
122+
95123 return (
96124 < Container >
97125 < Profile >
98126 < Header >
99- < ProfileImage src = { user . profileImageUrl ?? DefaultProfile } />
127+ < ProfileImageContainer >
128+ < ProfileImage
129+ src = { user . profileImageUrl ?? DefaultProfile }
130+ onClick = { handleImageClick }
131+ $onClick = { isEditMode }
132+ onError = { e => {
133+ e . currentTarget . src = DefaultProfile ;
134+ } }
135+ />
136+ < input
137+ type = "file"
138+ ref = { fileInputRef }
139+ onChange = { handleImageChange }
140+ style = { { display : 'none' } }
141+ accept = "image/*"
142+ />
143+ { isEditMode && (
144+ < ProfileImageResetButton onClick = { handleImageReset } >
145+ < img src = { Cancel } alt = "cancel" />
146+ </ ProfileImageResetButton >
147+ ) }
148+ </ ProfileImageContainer >
100149 < HeaderButtonContainer >
101150 < IconButton
102151 beforeImgUrl = { isEditMode ? Check : Edit }
0 commit comments