Skip to content

Commit

Permalink
fix(#48): S3에 임시로 저장된 프로필 이미지 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
uiop5809 committed Aug 22, 2024
1 parent edf3da4 commit f23b1ea
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/components/auth/UserProfileUpdate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Image from 'next/image'
import { useToast } from '@/hooks/useToast'
import {
useDeleteProfileImg,
useDeleteProfileImgS3,
usePostProfileImg,
useUserInfo,
} from '@/service/user/useUserService'
Expand All @@ -17,6 +18,8 @@ const UserProfileUpdate = ({ onUpdate }: UserProfileUpdateProps) => {
const fileInputRef = useRef<HTMLInputElement>(null)
const { mutate: postProfileImg } = usePostProfileImg()
const { mutate: deleteProfileImg } = useDeleteProfileImg()
const { mutate: deleteProfileImgS3 } = useDeleteProfileImgS3()

const { showToast } = useToast()

const [profileImgUrl, setProfileImgUrl] = useState<string | null>(null)
Expand Down Expand Up @@ -97,18 +100,34 @@ const UserProfileUpdate = ({ onUpdate }: UserProfileUpdateProps) => {
.toUpperCase()
const defaultImageUrl = `/images/mbti/${currentMbti}.svg`

deleteProfileImg(undefined, {
onSuccess: () => {
setProfileImgUrl(defaultImageUrl)
},
onError: () => {
if (profileImgUrl !== profile?.profileImgUrl) {
// DB에 저장된 이미지 삭제
if (profileImgUrl === profile?.profileImgUrl) {
deleteProfileImg(undefined, {
onSuccess: () => {
setProfileImgUrl(defaultImageUrl)
} else {
if (profileImgUrl === defaultImageUrl) {
showToast('기본 이미지는 삭제할 수 없습니다.')
}
},
onError: () => {
showToast('기본 이미지는 삭제할 수 없습니다.')
}
},
})
},
})
}
// S3에 임시로 저장된 이미지 삭제
else {
deleteProfileImgS3(profile?.profileImgUrl || '', {
onSuccess: () => {
setProfileImgUrl(defaultImageUrl)
if (profileImgUrl === defaultImageUrl) {
showToast('기본 이미지는 삭제할 수 없습니다.')
}
},
onError: () => {
showToast('기본 이미지는 삭제할 수 없습니다.')
},
})
}
}

if (!profile) return null
Expand Down
7 changes: 7 additions & 0 deletions src/service/user/UserQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ const queryOptions = {
await UserService.deleteProfileImg()
},
},

deleteProfileImgS3: {
queryKey: queryKeys.profileImg,
mutationFn: async (imageUrl: string): Promise<void> => {
await UserService.deleteProfileImgS3(imageUrl)
},
},
}

export default queryOptions
4 changes: 4 additions & 0 deletions src/service/user/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class UserService extends Service {
deleteProfileImg() {
return this.http.delete('/member/profile')
}

deleteProfileImgS3(imageUrl: string) {
return this.http.post(`/member/s3/file?imageUrl=${imageUrl}`)
}
}

export default new UserService()
11 changes: 11 additions & 0 deletions src/service/user/useUserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,22 @@ const useDeleteProfileImg = () => {
return useMutation<void, Error, void>(options)
}

const useDeleteProfileImgS3 = () => {
const mutationFn = (imageUrl: string): Promise<void> =>
queryOptions.deleteProfileImgS3.mutationFn(imageUrl)

const options: UseMutationOptions<void, Error, string, unknown> = {
mutationFn,
}
return useMutation<void, Error, string>(options)
}

export {
useProfile,
useTerms,
useUserInfo,
usePatchProfile,
usePostProfileImg,
useDeleteProfileImg,
useDeleteProfileImgS3,
}

0 comments on commit f23b1ea

Please sign in to comment.