52일 차 회고
import {
collection,
onSnapshot,
orderBy,
query,
where,
addDoc,
} from 'firebase/firestore';
import { authService, dbService } from '../firebase';
import { useFocusEffect } from '@react-navigation/native';
import { signOut } from 'firebase/auth';
const MyPage = ({ navigation: { navigate, reset, setOptions } }) => {
const newProfile = {
nickName,
profileText,
userId: authService.currentUser?.uid,
createdAt: Date.now(),
};
const addProfile = async () => {
if (nickName && profileText) {
await addDoc(collection(dbService, 'profile'), newProfile);
setIsOpenModal(!isOpenModal);
setNickName('');
setProfileText('');
} else {
if (!nickName) {
alert('닉네임을 입력해주세요.');
} else if (!profileText) {
alert('자기소개를 입력해주세요.');
}
}
};
const logout = () => {
signOut(authService)
.then(() => {
console.log('로그아웃 성공');
navigate('Slide');
})
.catch((err) => alert(err));
};
useFocusEffect(
useCallback(() => {
if (!authService.currentUser) {
reset({
index: 1,
routes: [
{
name: 'Tabs',
params: {
screen: 'Slide',
},
},
{
name: 'Stacks',
params: {
screen: 'Login',
},
},
],
});
return;
}
setOptions({
headerRight: () => {
return (
<TouchableOpacity style={{ marginRight: 10 }} onPress={logout}>
<Text>로그아웃</Text>
</TouchableOpacity>
);
},
});
const q = query(
collection(dbService, 'profile'),
orderBy('createdAt', 'desc'),
where('userId', '==', authService.currentUser?.uid)
);
onSnapshot(q, (snapshot) => {
const newProfiles = snapshot.docs.map((doc) => {
const newProfile = {
id: doc.id,
...doc.data(),
};
return newProfile;
});
setProfile(newProfiles);
});
}, [])
);
const profileFirst = profile[0];
return (
<>
<MypageTop>
<ProfileId>{profileFirst?.nickName ?? '닉네임없음'}</ProfileId>
<ProfileText>
{profileFirst?.profileText ?? '안녕하세요. 반갑습니다.'}
</ProfileText>
<ProfileBTN
onPress={() => {
setIsOpenModal(true);
}}
>
<BTNText>내 정보 수정</BTNText>
</ProfileBTN>
</MypageTop>
</>
);
};
export default MyPage;
<ProfileId>{profileFirst?.nickName ?? '닉네임없음'}</ProfileId>
<ProfileText>
{profileFirst?.profileText ?? '안녕하세요. 반갑습니다.'}
</ProfileText>
const newProfile = {
nickName,
profileText,
userId: authService.currentUser?.uid,
createdAt: Date.now(),
};
const addProfile = async () => {
if (nickName && profileText) {
await addDoc(collection(dbService, 'profile'), newProfile);
setIsOpenModal(!isOpenModal);
setNickName('');
setProfileText('');
} else {
if (!nickName) {
alert('닉네임을 입력해주세요.');
} else if (!profileText) {
alert('자기소개를 입력해주세요.');
}
}
};
const logout = () => {
signOut(authService)
.then(() => {
console.log('로그아웃 성공');
navigate('Slide');
})
.catch((err) => alert(err));
};
useFocusEffect(
useCallback(() => {
if (!authService.currentUser) {
reset({
index: 1,
routes: [
{
name: 'Tabs',
params: {
screen: 'Slide',
},
},
{
name: 'Stacks',
params: {
screen: 'Login',
},
},
],
});
return;
}
setOptions({
headerRight: () => {
return (
<TouchableOpacity style={{ marginRight: 10 }} onPress={logout}>
<Text>로그아웃</Text>
</TouchableOpacity>
);
},
});
const q = query(
collection(dbService, 'profile'),
orderBy('createdAt', 'desc'),
where('userId', '==', authService.currentUser?.uid)
);
onSnapshot(q, (snapshot) => {
const newProfiles = snapshot.docs.map((doc) => {
const newProfile = {
id: doc.id,
...doc.data(),
};
return newProfile;
});
setProfile(newProfiles);
});
}, [])
);
const profileFirst = profile[0];