useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, async user => {
if (user) {
const userDocRef = doc(db, 'users', user.uid);
const userDocSnap = await getDoc(userDocRef);
if (userDocSnap.exists()) {
const userData = userDocSnap.data();
setUserProfile({
email: user.email,
nickName: userData.nickname,
birthDate: userData.birthdate,
photoURL: userData.photoURL,
sexType: userData.sexType,
});
}
} else {
setUserProfile(null);
}
});
return () => unsubscribe();
}, []);
//편집 모드 전환
const clickNickNameEditModeHandler = () => {
setIsNickNameEditing(!isNickNameEditing);
setNewNickName(userProfile?.nickName || '');
};
//닉네임 저장
const clickNickNameSaveHandler = async () => {
if (auth.currentUser) {
await updateDoc(doc(db, 'users', auth.currentUser.uid), {
nickname: newNickName,
});
setUserProfile(prev => ({ ...prev, nickName: newNickName }));
setIsNickNameEditing(false);
}
};
const clickSaveSexTypeHandler = async () => {
if (auth.currentUser && userProfile) {
await updateDoc(doc(db, 'users', auth.currentUser.uid), {
sexType: sexType,
});
setUserProfile({ ...userProfile, sexType: sexType });
}
};
const clickSaveBirthDateHandler = async () => {
if (auth.currentUser && newBirthDate) {
await updateDoc(doc(db, 'users', auth.currentUser.uid), {
birthdate: newBirthDate,
});
setUserProfile({ ...userProfile, birthDate: newBirthDate });
}
};
const clickProfileImageHandler = async (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (!file || !auth.currentUser) return;
const storageRef = ref(storage, `ProfilePic/${auth.currentUser.uid}`);
await uploadBytes(storageRef, file);
const photoURL = await getDownloadURL(storageRef);
await updateDoc(doc(db, 'users', auth.currentUser.uid), {photoURL});
setUserProfile(prev => ({ ...prev, photoURL: photoURL }));
};
오늘의 한줄평 : 새록새록 지난번 과제하던것도 프로젝트 하던 것도 생각이 났었다. 건강상태가 안좋은 요즘, 그래도 열심히 하자.