generated from hcp-uw/react-express-starter-template
-
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 #43 from hcp-uw/settings-again
Settings again
- Loading branch information
Showing
11 changed files
with
412 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,4 +85,5 @@ nav { | |
|
||
#profile-icon { | ||
display: block; | ||
border-radius: 50%; | ||
} |
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,66 @@ | ||
// import { db } from '../../../../starter-backend/src/config/firebase-config'; | ||
// import { doc, updateDoc } from "firebase/firestore"; | ||
|
||
// export async function EditBioField(): Promise<void> { | ||
// const userRef = doc(db, "users", "frank"); | ||
|
||
// await updateDoc(userRef, { | ||
// "favorites.color": "Red" | ||
// }); | ||
|
||
// db.collection("users").doc("frank").update({ | ||
// "favorites.firebase": "Help")}; | ||
// } | ||
|
||
import { useState } from "react"; | ||
import { useNavigate } from "react-router-dom"; | ||
import { useAuth } from '../../contexts/AuthContext'; | ||
import './settings.css'; | ||
|
||
/* Allows the user to edit their bio */ | ||
|
||
type EditBioProps = {isModal: boolean, setIsModal: React.Dispatch<React.SetStateAction<boolean>>} | ||
|
||
export default function EditBio({ isModal, setIsModal }: EditBioProps) { | ||
const navigate = useNavigate(); | ||
const user = useAuth(); | ||
|
||
if (user === null) { | ||
throw new Error("Not logged in"); | ||
} | ||
const currentUser = user.currentUser; | ||
if (currentUser === null) { | ||
throw new Error("Not logged in"); | ||
} | ||
|
||
const [bio, setBio] = useState(""); | ||
const setError = user.setError; | ||
|
||
const handleEditBio = async (e: any) => { | ||
// e.preventDefault(); | ||
|
||
// try { | ||
// setError(""); | ||
// await user.updateUserProfile(currentUser, { displayName: name }); | ||
|
||
// setIsModal(false); | ||
// navigate("/settings"); | ||
// } catch (e) { | ||
// setError("failed to update display name"); | ||
// } | ||
} | ||
|
||
if (!isModal) { | ||
return null; | ||
} else { | ||
return ( | ||
<div className="edit-name-popup"> | ||
<form> | ||
<div><input id="new-name" type="text" placeholder="Enter your bio" onChange={(e) => setBio(e.target.value)}></input></div> | ||
<button onClick={handleEditBio}>Save</button> | ||
<button onClick={() => setIsModal(false)}>Cancel</button> | ||
</form> | ||
</div> | ||
); | ||
} | ||
} |
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,52 @@ | ||
import { useState } from "react"; | ||
import { useNavigate } from "react-router-dom"; | ||
import { useAuth } from '../../contexts/AuthContext'; | ||
import './settings.css'; | ||
|
||
/* Allows the user to edit their display name */ | ||
|
||
type EditDisplayNameProps = {isModal: boolean, setIsModal: React.Dispatch<React.SetStateAction<boolean>>} | ||
|
||
export default function EditName({ isModal, setIsModal }: EditDisplayNameProps) { | ||
const navigate = useNavigate(); | ||
const user = useAuth(); | ||
|
||
if (user === null) { | ||
throw new Error("Not logged in"); | ||
} | ||
const currentUser = user.currentUser; | ||
if (currentUser === null) { | ||
throw new Error("Not logged in"); | ||
} | ||
|
||
const [name, setName] = useState(""); | ||
const setError = user.setError; | ||
|
||
const handleEditDisplayName = async (e: any) => { | ||
e.preventDefault(); | ||
|
||
try { | ||
setError(""); | ||
await user.updateUserProfile(currentUser, { displayName: name }); | ||
|
||
setIsModal(false); | ||
navigate("/settings"); | ||
} catch (e) { | ||
setError("failed to update display name"); | ||
} | ||
} | ||
|
||
if (!isModal) { | ||
return null; | ||
} else { | ||
return ( | ||
<div className="edit-name-popup"> | ||
<form> | ||
<div><input id="new-name" type="text" placeholder="Enter new display name" onChange={(e) => setName(e.target.value)}></input></div> | ||
<button onClick={handleEditDisplayName}>Save</button> | ||
<button onClick={() => setIsModal(false)}>Cancel</button> | ||
</form> | ||
</div> | ||
); | ||
} | ||
} |
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,67 @@ | ||
import React from "react"; | ||
import { useEffect, useState } from "react"; | ||
import { useAuth } from '../../contexts/AuthContext'; | ||
import { storage } from '../../config/firebase'; | ||
import { ref, uploadBytes, getDownloadURL } from 'firebase/storage'; | ||
import profile from '../../assets/profile-button.png'; | ||
|
||
/* Allows the user to change their profile photo by uploading an image from their device */ | ||
|
||
export default function UploadProfilePic(): JSX.Element { | ||
const user = useAuth(); | ||
if (user === null) { | ||
throw new Error('error'); | ||
} | ||
const currentUser = user.currentUser; | ||
if (currentUser === null) { | ||
throw new Error('error'); | ||
} | ||
|
||
async function upload(file: any, user: any, setLoading: any) { | ||
if (user === null) { | ||
throw new Error('error'); | ||
} | ||
const currentUser = user.currentUser; | ||
const fileRef = ref(storage, currentUser.uid + '.png'); | ||
|
||
setLoading(true); | ||
const snapshot = await uploadBytes(fileRef, file); | ||
const photoURL = await getDownloadURL(fileRef); | ||
|
||
setPhotoUrl(photoURL); | ||
user.updateUserProfile(currentUser, {photoURL: photoURL}); | ||
setLoading(false); | ||
} | ||
|
||
const [photo, setPhoto] = useState(null); | ||
const [loading, setLoading] = useState(false); | ||
const [photoUrl, setPhotoUrl] = useState(''); | ||
|
||
function handleChange(e: any) { | ||
if (e.target.files[0]) { | ||
setPhoto(e.target.files[0]); | ||
} | ||
} | ||
|
||
function handleClick() { | ||
upload(photo, user, setLoading); | ||
} | ||
|
||
useEffect(() => { | ||
if (currentUser?.photoURL) { | ||
setPhotoUrl(currentUser.photoURL); | ||
} | ||
}, [currentUser]); | ||
|
||
return ( | ||
<div className='profile-text'> | ||
<div className='container'> | ||
<div id='image-cropper'><img src={photoUrl || profile} alt='profile' id='img'></img></div> | ||
</div> | ||
<p></p> | ||
<p id='edit-profile-text'>Edit profile photo:</p> | ||
<input type="file" onChange={handleChange}></input> | ||
<button disabled={loading || !photo} onClick={handleClick}>Upload</button> | ||
</div> | ||
) | ||
} |
Oops, something went wrong.