Skip to content

Commit 1dfb64a

Browse files
authored
♻️ [#600] Refactor: 기관 마이페이지 프리사인드 이미지 업로드 적용 (#604)
1 parent 6cd9982 commit 1dfb64a

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

src/features/edit-center-profile/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const EditCenterProfile = ({ data }: EditCenterProfileProps) => {
1919
const {
2020
preview,
2121
handleImageUpload,
22+
handlePutProfileImage,
2223
centerName,
2324
originalName,
2425
centerPhone,
@@ -56,7 +57,7 @@ const EditCenterProfile = ({ data }: EditCenterProfileProps) => {
5657
}
5758
onImageUpload={handleImageUpload}
5859
/>
59-
<ProfileImgEditButton label="수정하기" type="blue" onClick={() => console.log('프로필이미지 수정')} />
60+
<ProfileImgEditButton label="수정하기" type="blue" onClick={handlePutProfileImage} />
6061
</ProfileSection>
6162
<FormSection>
6263
<EditProfileForm

src/features/edit-center-profile/logic/useEditCenterProfile.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useEffect, useState } from 'react';
22
import {
33
useUpdateCenterProfile,
4+
useUpdateProfileImage,
45
type CenterProfileUpdateRequest
56
} from '@/store/queries/center-mypage/useCenterProfile';
67
import { useAlertDialog } from '@/store/stores/dialog/dialogStore';
@@ -12,6 +13,7 @@ interface UseEditCenterProfileProps {
1213

1314
const useEditCenterProfile = ({ data }: UseEditCenterProfileProps) => {
1415
const updateProfile = useUpdateCenterProfile();
16+
const updateProfileImage = useUpdateProfileImage();
1517
const { openAlert } = useAlertDialog();
1618

1719
// 각 input 상태 관리
@@ -39,6 +41,37 @@ const useEditCenterProfile = ({ data }: UseEditCenterProfileProps) => {
3941
setPreview(file);
4042
};
4143

44+
// const handlePutProfileImage = async () => {
45+
// if (!preview) {
46+
// openAlert('변경할 이미지를 선택해주세요.');
47+
// return;
48+
// }
49+
50+
// try {
51+
// const fileName = preview.name;
52+
// await updateProfileImage.mutateAsync(fileName);
53+
// openAlert('프로필 이미지가 수정되었습니다.');
54+
// } catch (error) {
55+
// console.error('이미지 수정 중 오류가 발생했습니다:', error);
56+
// openAlert('이미지 수정 중 오류가 발생했습니다. 다시 시도해주세요.');
57+
// }
58+
// };
59+
60+
const handlePutProfileImage = async () => {
61+
if (!preview) {
62+
openAlert('변경할 이미지를 선택해주세요.');
63+
return;
64+
}
65+
66+
try {
67+
await updateProfileImage.mutateAsync(preview); // File 객체 직접 전달
68+
openAlert('프로필 이미지가 수정되었습니다.');
69+
} catch (error) {
70+
console.error('이미지 수정 중 오류가 발생했습니다:', error);
71+
openAlert('이미지 수정 중 오류가 발생했습니다. 다시 시도해주세요.');
72+
}
73+
};
74+
4275
// 입력 핸들러 모음
4376
const handleNameChange = (name: string) => setCenterName(name);
4477
const handlePhoneChange = (phone: string, isValid: boolean) => {
@@ -82,6 +115,7 @@ const useEditCenterProfile = ({ data }: UseEditCenterProfileProps) => {
82115
return {
83116
preview,
84117
handleImageUpload,
118+
handlePutProfileImage,
85119
centerName,
86120
originalName,
87121
centerPhone,
@@ -94,7 +128,7 @@ const useEditCenterProfile = ({ data }: UseEditCenterProfileProps) => {
94128
// validURL,
95129
validPhone,
96130
handleEditProfile,
97-
isSubmitting: updateProfile.isPending
131+
isSubmitting: updateProfile.isPending || updateProfileImage.isPending
98132
};
99133
};
100134

src/store/queries/center-mypage/useCenterProfile.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ export interface CenterProfileUpdateRequest {
1717
homepage_url: string;
1818
}
1919

20+
// 프리사인드 이미지 업로드를 위한 타입 정의
21+
// interface PresignedUrlResponse {
22+
// code: number;
23+
// data: string;
24+
// }
25+
2026
// 기관 프로필 get 해오는 fetch 함수
2127
const fetchCenterProfile = async (centerId: string): Promise<centerProfileType> => {
2228
const response = await axiosInstance.get(`/api/center/profile/${centerId}`);
@@ -59,3 +65,43 @@ export const useUpdateCenterProfile = () => {
5965
}
6066
});
6167
};
68+
69+
// // 이미지 프로필 수정 put 훅
70+
export const useUpdateProfileImage = () => {
71+
const queryClient = useQueryClient();
72+
73+
return useMutation({
74+
mutationFn: async (file: File) => {
75+
try {
76+
// 프리사인드 URL 요청
77+
const response = await axiosInstance.put('/api/user/image', {
78+
file_name: file.name
79+
});
80+
81+
console.log('프리사인드 url 확인:', response);
82+
83+
if (!response.data) {
84+
throw new Error('프리사인드 url이 존재하지 않습니다.');
85+
}
86+
87+
const token = localStorage.getItem('token');
88+
89+
// 프리사인드 url로 파일 업로드
90+
await axios.put(response.data, file, {
91+
headers: {
92+
'Content-Type': file.type,
93+
Authorization: token || ''
94+
}
95+
});
96+
97+
console.log('파일 업로드 성공');
98+
} catch (error) {
99+
console.error('pre-signed 이미지 업로드 중 오류 발생:', error);
100+
throw error;
101+
}
102+
},
103+
onSuccess: () => {
104+
queryClient.invalidateQueries({ queryKey: ['centerProfile'] });
105+
}
106+
});
107+
};

0 commit comments

Comments
 (0)