-
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 #19 from sryung1225/dev
- Loading branch information
Showing
14 changed files
with
339 additions
and
115 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import React, { useState } from 'react'; | ||
import { updateProfile } from 'firebase/auth'; | ||
import { doc, updateDoc } from 'firebase/firestore'; | ||
import { | ||
deleteObject, | ||
getDownloadURL, | ||
ref, | ||
uploadBytes, | ||
} from 'firebase/storage'; | ||
import { auth, db, storage } from '../firebase.ts'; | ||
import IUser from '../interfaces/IUser.ts'; | ||
import CompressImage from '../utils/compress-image.tsx'; | ||
import useEscClose from '../utils/use-esc-close.tsx'; | ||
import * as S from '../styles/profile-form.ts'; | ||
import { ReactComponent as IconUser } from '../assets/images/i-user.svg'; | ||
import { ReactComponent as LoadingSpinner } from '../assets/images/loading-spinner-mini.svg'; | ||
|
||
interface IEditProfileForm extends Pick<IUser, 'userAvatar' | 'userName'> { | ||
onClose: () => void; | ||
} | ||
|
||
export default function EditProfileForm({ | ||
userAvatar: initialAvatar, | ||
userName: initialName, | ||
onClose, | ||
}: IEditProfileForm) { | ||
const user = auth.currentUser; | ||
const [isLoading, setLoading] = useState(false); | ||
const [avatar, setAvatar] = useState<File | null>(null); | ||
const [avatarPreview, setAvatarPreview] = useState(user?.photoURL); | ||
const onAvatarChange = async (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const images = e.target.files; | ||
if (images && images.length === 1) { | ||
const selectedImage = images[0]; | ||
const compressedImage = await CompressImage({ | ||
imageFile: selectedImage, | ||
size: 200, | ||
}); | ||
setAvatar(compressedImage); | ||
const previewUrl = compressedImage | ||
? URL.createObjectURL(compressedImage) | ||
: ''; | ||
setAvatarPreview(previewUrl); | ||
} | ||
}; | ||
const onAvatarDelete = () => { | ||
setAvatar(null); | ||
setAvatarPreview(null); | ||
}; | ||
|
||
const [name, setName] = useState(initialName); | ||
const onNameChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setName(e.target.value); | ||
}; | ||
|
||
const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
if (!user || isLoading || name === '') return; | ||
try { | ||
setLoading(true); | ||
const userDocRef = doc(db, 'users', user.uid); | ||
await updateDoc(userDocRef, { | ||
userName: name, | ||
}); | ||
await updateProfile(user, { | ||
displayName: name, | ||
}); | ||
const locationRef = ref(storage, `avatars/${user?.uid}`); | ||
if (avatar) { | ||
const result = await uploadBytes(locationRef, avatar); | ||
const url = await getDownloadURL(result.ref); | ||
await updateDoc(userDocRef, { | ||
userAvatar: url, | ||
}); | ||
await updateProfile(user, { | ||
photoURL: url, | ||
}); | ||
} else if (!avatar && initialAvatar && initialAvatar !== avatarPreview) { | ||
await updateProfile(user, { | ||
photoURL: '', | ||
}); | ||
await deleteObject(locationRef); | ||
await updateDoc(userDocRef, { | ||
userAvatar: null, | ||
}); | ||
} | ||
setAvatarPreview(null); | ||
setAvatar(null); | ||
} catch (error) { | ||
console.log(error); | ||
} finally { | ||
setLoading(false); | ||
onClose(); | ||
} | ||
}; | ||
useEscClose(onClose); | ||
return ( | ||
<S.Form onSubmit={onSubmit}> | ||
{avatarPreview ? ( | ||
<> | ||
<S.AttachAvatarPreview | ||
src={avatarPreview} | ||
alt="프로필이미지 미리보기" | ||
width="120" | ||
height="120" | ||
/> | ||
<S.AttachAvatarDelete type="button" onClick={onAvatarDelete} /> | ||
</> | ||
) : ( | ||
<S.AttachAvatarButton htmlFor="avatar_edit"> | ||
<IconUser /> | ||
</S.AttachAvatarButton> | ||
)} | ||
<S.AttachAvatarInput | ||
onChange={onAvatarChange} | ||
id="avatar_edit" | ||
type="file" | ||
accept="image/*" | ||
/> | ||
<S.InputText | ||
onChange={onNameChange} | ||
name="name_edit" | ||
type="text" | ||
placeholder="이름을 입력해주세요" | ||
value={name} | ||
required | ||
/> | ||
<S.SubmitButton type="submit"> | ||
{isLoading ? <LoadingSpinner /> : '수정'} | ||
</S.SubmitButton> | ||
</S.Form> | ||
); | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { doc, getDoc } from 'firebase/firestore'; | ||
import { auth, db } from '../firebase.ts'; | ||
import * as S from '../styles/profile.ts'; | ||
import * as P from '../styles/popup.ts'; | ||
import { ReactComponent as IconUser } from '../assets/images/i-user.svg'; | ||
import EditProfileForm from './edit-profile-form.tsx'; | ||
|
||
export default function UserProfile() { | ||
const user = auth.currentUser; | ||
const [userAvatar, setUserAvatar] = useState(''); | ||
const [userName, setUserName] = useState(''); | ||
useEffect(() => { | ||
const fetchUserData = async () => { | ||
if (!user) return; | ||
const userDoc = await getDoc(doc(db, 'users', user?.uid)); | ||
if (userDoc.exists()) { | ||
const userData = userDoc.data(); | ||
setUserAvatar(userData.userAvatar); | ||
setUserName(userData.userName); | ||
} | ||
}; | ||
fetchUserData(); | ||
}, []); | ||
const [editPopup, setEditPopup] = useState(false); | ||
const toggleEditPopup = () => { | ||
setEditPopup(!editPopup); | ||
}; | ||
return ( | ||
<S.Profile> | ||
<S.Avatar> | ||
{userAvatar ? ( | ||
<S.AvatarImage | ||
src={userAvatar} | ||
alt="프로필 이미지" | ||
width="120" | ||
height="120" | ||
/> | ||
) : ( | ||
<IconUser /> | ||
)} | ||
</S.Avatar> | ||
<S.Name>{userName}</S.Name> | ||
<S.EditButton onClick={toggleEditPopup} type="button"> | ||
프로필 수정 | ||
</S.EditButton> | ||
{editPopup ? ( | ||
<P.PopupWrapper> | ||
<P.Popup> | ||
<P.CloseButton onClick={toggleEditPopup} type="button" /> | ||
<EditProfileForm | ||
userAvatar={userAvatar} | ||
userName={userName} | ||
onClose={toggleEditPopup} | ||
/> | ||
</P.Popup> | ||
</P.PopupWrapper> | ||
) : null} | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default interface IUser { | ||
userId: string; | ||
userName: string; | ||
userAvatar: string; | ||
} |
Oops, something went wrong.