사용자 정보 수정 기능 구현

This commit is contained in:
hyeonsu
2025-08-27 17:32:41 +09:00
parent e347b52900
commit 00ce90a9f0
3 changed files with 132 additions and 36 deletions

View File

@@ -3,6 +3,7 @@
import { useState, useCallback } from "react";
import { ProfileFormData, ProfileModalState } from "@/types/profile";
import { LAYOUT_CONFIG, MESSAGES } from "@/constants/layout";
import { apiCall } from "@/lib/api/client";
// 알림 모달 상태 타입
interface AlertModalState {
@@ -181,48 +182,43 @@ export const useProfile = (user: any, refreshUserData: () => Promise<void>) => {
photoData = modalState.selectedImage;
}
// 사용자 정보 저장
const userSaveData = {
userId: user.userId,
// 사용자 정보 저장 데이터 준비
const updateData = {
userName: modalState.formData.userName,
email: modalState.formData.email,
deptName: modalState.formData.deptName,
positionName: modalState.formData.positionName,
locale: modalState.formData.locale,
photo: photoData,
photo: photoData !== user.photo ? photoData : undefined, // 변경된 경우만 전송
};
console.log("사용자 정보 저장 요청:", userSaveData);
console.log("프로필 업데이트 요청:", updateData);
const userResponse = await fetch(`${LAYOUT_CONFIG.API_BASE_URL}${LAYOUT_CONFIG.ENDPOINTS.USER_SAVE}`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(userSaveData),
});
// API 호출 (JWT 토큰 자동 포함)
const response = await apiCall("PUT", "/admin/profile", updateData);
if (userResponse.ok) {
const userResult = await userResponse.json();
console.log("사용자 정보 저장 응답:", userResult);
console.log("프로필 업데이트 응답:", response);
if (userResult.result) {
// 성공: 세션 정보 새로고침
await refreshUserData();
setModalState((prev) => ({
...prev,
selectedFile: null,
isOpen: false,
}));
showAlert("저장 완료", MESSAGES.PROFILE_SAVE_SUCCESS, "success");
} else {
throw new Error(userResult.msg || "사용자 정보 저장 실패");
if (response.result) {
// locale이 변경된 경우 전역 변수와 localStorage 업데이트
if (modalState.formData.locale && modalState.formData.locale !== user.locale) {
if (typeof window !== "undefined") {
// 전역 변수 업데이트
(window as any).__GLOBAL_USER_LANG = modalState.formData.locale;
// localStorage 업데이트
localStorage.setItem("userLocale", modalState.formData.locale);
console.log("🌍 사용자 locale 업데이트:", modalState.formData.locale);
}
}
// 성공: 사용자 정보 새로고침
await refreshUserData();
setModalState((prev) => ({
...prev,
selectedFile: null,
isOpen: false,
}));
showAlert("저장 완료", "프로필이 성공적으로 업데이트되었습니다.", "success");
} else {
const errorText = await userResponse.text();
console.error("API 응답 오류:", errorText);
throw new Error(`사용자 정보 저장 실패: ${userResponse.status} ${userResponse.statusText}`);
throw new Error(response.message || "프로필 업데이트 실패");
}
} catch (error) {
console.error("프로필 저장 실패:", error);