-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from sryung1225/dev
- Loading branch information
Showing
10 changed files
with
150 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,102 @@ | ||
import { useState } from 'react'; | ||
import { useRecoilValue } from 'recoil'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { useRecoilState } from 'recoil'; | ||
import { deleteUser } from 'firebase/auth'; | ||
import { deleteDoc, doc } from 'firebase/firestore'; | ||
import { deleteObject, ref } from 'firebase/storage'; | ||
import { auth, db, storage } from '@/firebase.ts'; | ||
import currentUserAtom from '@atom/current-user.tsx'; | ||
import IUser from '@type/IUser.ts'; | ||
import EditProfileForm from '@compo/profile/edit-profile-form.tsx'; | ||
import * as S from '@style/profile.ts'; | ||
import * as P from '@style/popup.ts'; | ||
import { ReactComponent as IconUser } from '@img/i-user.svg'; | ||
|
||
export default function UserProfile() { | ||
interface IUserProfile { | ||
user: IUser; | ||
} | ||
|
||
export default function UserProfile({ user }: IUserProfile) { | ||
const navigate = useNavigate(); | ||
const [currentUser, setCurrentUser] = useRecoilState(currentUserAtom); | ||
const [editPopup, setEditPopup] = useState(false); | ||
const currentUser = useRecoilValue(currentUserAtom); | ||
const [withdrawPopup, setWithdrawPopup] = useState(false); | ||
const toggleEditPopup = () => { | ||
setEditPopup(!editPopup); | ||
}; | ||
const toggleWithdrawPopup = () => { | ||
setWithdrawPopup(!withdrawPopup); | ||
}; | ||
const withdrawAccount = async () => { | ||
const authCurrentUser = auth.currentUser; | ||
if (!authCurrentUser) return; | ||
try { | ||
await deleteDoc(doc(db, 'users', authCurrentUser.uid)); | ||
const photoRef = ref(storage, `avatars/${authCurrentUser.uid}`); | ||
await deleteObject(photoRef); | ||
await deleteUser(authCurrentUser); | ||
} catch (error) { | ||
console.log(error); | ||
} finally { | ||
setCurrentUser({ | ||
userId: '', | ||
userAvatar: '', | ||
userName: '', | ||
}); | ||
navigate('/auth'); | ||
} | ||
}; | ||
return ( | ||
<S.Profile> | ||
<S.Avatar> | ||
{currentUser.userAvatar ? ( | ||
{user.userAvatar ? ( | ||
<S.AvatarImage | ||
src={currentUser.userAvatar} | ||
alt={`${currentUser.userName}의 프로필 사진`} | ||
src={user.userAvatar} | ||
alt={`${user.userName}의 프로필 사진`} | ||
width="120" | ||
height="120" | ||
/> | ||
) : ( | ||
<IconUser /> | ||
)} | ||
</S.Avatar> | ||
<S.Name>{currentUser.userName}</S.Name> | ||
<S.EditButton onClick={toggleEditPopup} type="button"> | ||
프로필 수정 | ||
</S.EditButton> | ||
{editPopup ? ( | ||
<P.PopupWrapper> | ||
<P.Popup> | ||
<P.CloseButton onClick={toggleEditPopup} type="button" /> | ||
<EditProfileForm onClose={toggleEditPopup} /> | ||
</P.Popup> | ||
</P.PopupWrapper> | ||
) : null} | ||
<S.Name>{user.userName}</S.Name> | ||
{user.userId === currentUser.userId && ( | ||
<S.Buttons> | ||
<S.EditButton onClick={toggleEditPopup} type="button"> | ||
프로필 수정 | ||
</S.EditButton> | ||
{editPopup && ( | ||
<P.PopupWrapper> | ||
<P.Popup> | ||
<P.CloseButton onClick={toggleEditPopup} type="button" /> | ||
<EditProfileForm onClose={toggleEditPopup} /> | ||
</P.Popup> | ||
</P.PopupWrapper> | ||
)} | ||
<S.WithdrawButton onClick={toggleWithdrawPopup} type="button"> | ||
회원 탈퇴 | ||
</S.WithdrawButton> | ||
{withdrawPopup && ( | ||
<P.PopupWrapper> | ||
<P.MiniPopup> | ||
<P.CloseButton onClick={toggleWithdrawPopup} type="button" /> | ||
<P.Text> | ||
정말 <span>탈퇴</span>하시겠습니까? | ||
</P.Text> | ||
<P.ButtonWrapper> | ||
<P.Button onClick={withdrawAccount} type="button"> | ||
예 | ||
</P.Button> | ||
<P.Button onClick={toggleWithdrawPopup} type="button"> | ||
아니요 | ||
</P.Button> | ||
</P.ButtonWrapper> | ||
</P.MiniPopup> | ||
</P.PopupWrapper> | ||
)} | ||
</S.Buttons> | ||
)} | ||
</S.Profile> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,45 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useLocation } from 'react-router-dom'; | ||
import { useRecoilValue } from 'recoil'; | ||
import { collection, getDocs, query, where } from 'firebase/firestore'; | ||
import { db } from '@/firebase.ts'; | ||
import WindowTop from '@compo/window-top.tsx'; | ||
import UserProfile from '@compo/profile/user-profile.tsx'; | ||
import UserTimeline from '@compo/profile/user-timeline.tsx'; | ||
import currentUserAtom from '@atom/current-user.tsx'; | ||
import IUser from '@type/IUser.ts'; | ||
import * as W from '@style/window.ts'; | ||
|
||
export default function Profile() { | ||
const location = useLocation(); | ||
const userFromLocation = location.search.slice(7); | ||
const [user, setUser] = useState<IUser | undefined>(); | ||
const currentUser = useRecoilValue(currentUserAtom); | ||
useEffect(() => { | ||
const fetchUser = async () => { | ||
const usersQuery = query( | ||
collection(db, 'users'), | ||
where('userId', '==', userFromLocation), | ||
); | ||
const snapshot = await getDocs(usersQuery); | ||
if (!snapshot.empty) { | ||
const userData = snapshot.docs[0].data() as IUser; | ||
setUser(userData); | ||
} else { | ||
console.error("can't find user data"); | ||
} | ||
}; | ||
fetchUser(); | ||
}, [userFromLocation, currentUser]); | ||
return ( | ||
<W.Window> | ||
<WindowTop /> | ||
<UserProfile /> | ||
<UserTimeline /> | ||
{user && ( | ||
<> | ||
<UserProfile user={user} /> | ||
<UserTimeline user={user} /> | ||
</> | ||
)} | ||
</W.Window> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters