diff --git a/src/App.tsx b/src/App.tsx index 53c4a7e..5a79895 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,6 +2,7 @@ import { Routes, Route } from 'react-router-dom'; import AuthPage from './components/AuthPage.tsx'; import DashboardPage from './components/DashboardPage.tsx'; +import EditProfilePage from './components/EditProfilePage.tsx'; import InitialPage from './components/InitialPage.tsx'; import MatchesPage from './components/MatchesPage.tsx'; @@ -13,6 +14,7 @@ export const App = () => { } /> } /> } /> + } /> ); }; diff --git a/src/components/EditProfilePage.tsx b/src/components/EditProfilePage.tsx new file mode 100644 index 0000000..f3d5b4b --- /dev/null +++ b/src/components/EditProfilePage.tsx @@ -0,0 +1,192 @@ +import { Box, Spinner, Input, Select, Stack, Button, useToast, Text, FormControl, FormLabel } from '@chakra-ui/react'; +import { getAuth } from 'firebase/auth'; +import { doc, updateDoc } from 'firebase/firestore'; +import { FormEvent, ChangeEvent, useEffect, useState } from 'react'; +import { useAuthState } from 'react-firebase-hooks/auth'; +import { useDocument } from 'react-firebase-hooks/firestore'; +import { Navigate } from 'react-router-dom'; + +import { db } from '../../firebase.config'; +import countries from '../components/countries.json'; + +const auth = getAuth(); + +function EditProfilePage() { + const [user, userLoading] = useAuthState(auth); + const toast = useToast(); + + // Get the current user from Firebase + const [currentUser, currentUserLoading] = useDocument(doc(db, 'users', user?.uid || 'asd')); + + const [userLoaded, setUserLoaded] = useState(false); + + const [age, setAge] = useState(''); + const [countryOfLiving, setCountryOfLiving] = useState(''); + const [gender, setGender] = useState(''); + const [name, setName] = useState(''); + const [preference, setPreference] = useState(''); + const [contactInfo, setContactInfo] = useState(''); + const [comment, setComment] = useState(''); + + useEffect(() => { + // Set initial values for the form fields after user is loaded + if (!userLoaded && !currentUserLoading) { + const currentUserData = currentUser?.data() || {}; + + setAge(currentUserData.age); + setCountryOfLiving(currentUserData.countryOfLiving); + setGender(currentUserData.gender); + setName(currentUserData.name); + setPreference(currentUserData.preference); + setContactInfo(currentUserData.contactInfo); + setComment(currentUserData.comment); + + setUserLoaded(true); + } + }, [userLoaded, currentUserLoading, currentUser]); + + const updateProfile = async (e: FormEvent) => { + e.preventDefault(); + + if (!currentUser) return; + + try { + await updateDoc(currentUser.ref, { + age, + countryOfLiving, + gender, + name, + preference, + contactInfo, + comment, + }); + + toast({ + status: 'success', + description: 'Successfully updated user.', + }); + } catch (e) { + toast({ status: 'error', description: 'Failed to update user.' }); + } + }; + + const handleNameChange = (e: ChangeEvent) => { + setName(e.target.value); + }; + + const handleAgeChange = (e: ChangeEvent) => { + setAge(e.target.value); + }; + + const handleGenderChange = (e: ChangeEvent) => { + setGender(e.target.value); + }; + + const handleCountryChange = (e: ChangeEvent) => { + setCountryOfLiving(e.target.value); + }; + + const handleContactChange = (e: ChangeEvent) => { + setContactInfo(e.target.value); + }; + + const handlePreferenceChange = (e: ChangeEvent) => { + setPreference(e.target.value); + }; + + const handleCommentChange = (e: ChangeEvent) => { + setComment(e.target.value); + }; + + // Do not show page content until auth state is fetched. + if (userLoading || currentUserLoading) { + return ; + } + + // If user isn't signed in, redirect to auth page. + if (!user) { + return ; + } + + return ( + + + Profile settings + +
+ + + Name + + + + + Age + + + + + Gender + + + + + Country of living + + + + + Contact Information + + + + + Preference + + + + + Comment + + + + + +
+
+ ); +} + +export default EditProfilePage;