diff --git a/real_estate/client/src/App.jsx b/real_estate/client/src/App.jsx index c37ea01..69eaed7 100644 --- a/real_estate/client/src/App.jsx +++ b/real_estate/client/src/App.jsx @@ -3,8 +3,9 @@ import Home from './pages/Home' import Login from './pages/Login' import SignUp from './pages/SignUp' import Contact from './pages/Contact' -import { Profile } from './pages/Profile' +import {Profile} from './pages/Profile' import Navbar from './components/Navbar' +import PrivateRoute from './components/PrivateRoute' const App = () => { @@ -13,7 +14,9 @@ const App = () => { }/> - }/> + }> + }/> + }/> }/> }/> diff --git a/real_estate/client/src/components/Navbar.jsx b/real_estate/client/src/components/Navbar.jsx index bfeaa9b..7946f4f 100644 --- a/real_estate/client/src/components/Navbar.jsx +++ b/real_estate/client/src/components/Navbar.jsx @@ -1,9 +1,10 @@ //import { IoIosSearch } from "react-icons/io"; import { IoIosLock } from "react-icons/io"; import {Link} from 'react-router-dom'; -//import {useSelector} from 'react-redux' +import {useSelector} from 'react-redux' const Navbar = () => { + const {currentUser} = useSelector((state) => state.user) return ( @@ -18,12 +19,19 @@ const Navbar = () => { */} - - Home - Buying - Selling - Contact - Login + + Home + Buying + Selling + Contact + + {currentUser ? ( + + ): ( + Login + )} + + diff --git a/real_estate/client/src/firebase.js b/real_estate/client/src/firebase.js index eeae095..7c79911 100644 --- a/real_estate/client/src/firebase.js +++ b/real_estate/client/src/firebase.js @@ -14,4 +14,14 @@ const firebaseConfig = { }; // Initialize Firebase -export const app = initializeApp(firebaseConfig); \ No newline at end of file +export const app = initializeApp(firebaseConfig); + + + + + +//firebase storage ==> change the rule to +//allow read; +//allow write: if +//request.resource.size < 2 * 1024 *1024 && +//request.resource.contentType.matches('image/.*') \ No newline at end of file diff --git a/real_estate/client/src/pages/Profile.jsx b/real_estate/client/src/pages/Profile.jsx index 0f1ffcf..b768f2d 100644 --- a/real_estate/client/src/pages/Profile.jsx +++ b/real_estate/client/src/pages/Profile.jsx @@ -1,7 +1,68 @@ - +import {useSelector} from'react-redux' +import { useEffect, useRef, useState } from 'react' +import {getDownloadURL, getStorage, ref, uploadBytesResumable} from 'firebase/storage' +import { app } from '../firebase'; export const Profile = () => { + const fileRef = useRef(null); + const {currentUser} = useSelector((state) => state.user); + const [file, setFile] = useState(undefined); + const [filePercentage, setFilePercentage] = useState(0); + const [fileUploadError, setFileUploadError] = useState(false) + const [formData, setFormData] = useState({}) + + + + useEffect (() => { + if(file){ + handleFileUpload(file); + } + }, [file]); + + const handleFileUpload = (file) =>{ + const storage = getStorage(app); + const fileName = new Date().getTime()+ file.name; + const storageRef = ref(storage, fileName); + const uploadTask = uploadBytesResumable(storageRef, file); + + uploadTask.on('state_changed', + (snapshot) =>{ + const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100; + setFilePercentage(Math.round(progress)); + }, + + (error) => { + setFileUploadError(true); + }, + () => { + getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => setFormData({...formData, avatar: downloadURL})); + } + ) + }; + + return ( - Profile + + Profile + + setFile(e.target.files[0])} type="file" ref={fileRef} hidden accept='image/*'/> + fileRef.current.click()} src={formData.avatar || currentUser.avatar} alt="" className='rounded-full h-24 w-24 object-cover self-center mt-2 cursor-pointer bg-blue-100'/> + + { + fileUploadError ? (Error image upload (image must be less than 2 mb) ) : + filePercentage > 0 && filePercentage < 100 ? ({`Uploading ${filePercentage}%`}) : + filePercentage === 100 ? (Image successfully uploaded) : ("") + } + + + + + Update + + + Delete Account + Log Out + + ) }
+ { + fileUploadError ? (Error image upload (image must be less than 2 mb) ) : + filePercentage > 0 && filePercentage < 100 ? ({`Uploading ${filePercentage}%`}) : + filePercentage === 100 ? (Image successfully uploaded) : ("") + } +