From ce6adb19aab3f1bc17ff43157f3fd8dcaa1badda Mon Sep 17 00:00:00 2001 From: Hunardeep Singh Date: Mon, 8 Jul 2024 23:59:52 -0600 Subject: [PATCH] Hunar (#54) * Add folder for student register issue: There was a router problem for student register resolve: always make page.js for route just separate them by making different folders. * database * - Created useContext for user (uid) - Added service folder with functions to read and write data from the database - Added authentication for student/homepage and student/information - Updated student/login page to fix email and password authentication issues * starting login page * login page design * signup page complete * adding colors * file change * forget password * add modal * add conditions * login page * add posst function for menu and restaurant details * meging with main * adding restaurant information page * fetching and adding data for restaurants * merging from origin * merge from orign * adding emails status in database * add and create function for adding new email with active status add and create function for getting email names where active status is true make changes in register page to check that email is in database for register * chengin import sttement * adding terms condition * adding logos facility * merge from master * update database sait-staff * adding search option * commit * connecting sait-staff with database * merging form master * changes on sait * mege from master to origin * changing restaurant info table * adding data to database * updating cards * adding logc for increase and decrease arrow * adding databse changes * updating datasbe function to getting student information * adding authentication for restaurants * changing Searching bar * adding search bar in sait-staff * updating restaurant login page * adding update,delete and change password features * adding fuctionality of deleting user's information from database including user data and restaurant menu, delete data from storage and from authentication * update edit functionallity in sait-staff and add delete functionality in restaurant * add add functionality for admins * Hunar (#46) * Add folder for student register issue: There was a router problem for student register resolve: always make page.js for route just separate them by making different folders. * database * - Created useContext for user (uid) - Added service folder with functions to read and write data from the database - Added authentication for student/homepage and student/information - Updated student/login page to fix email and password authentication issues * starting login page * login page design * signup page complete * adding colors * file change * forget password * add modal * add conditions * login page * add posst function for menu and restaurant details * meging with main * adding restaurant information page * fetching and adding data for restaurants * merging from origin * merge from orign * adding emails status in database * add and create function for adding new email with active status add and create function for getting email names where active status is true make changes in register page to check that email is in database for register * chengin import sttement * adding terms condition * adding logos facility * merge from master * update database sait-staff * adding search option * commit * connecting sait-staff with database * merging form master * changes on sait * mege from master to origin * changing restaurant info table * adding data to database * updating cards * adding logc for increase and decrease arrow * adding databse changes * updating datasbe function to getting student information * adding authentication for restaurants * changing Searching bar * adding search bar in sait-staff * updating restaurant login page * adding update,delete and change password features * adding fuctionality of deleting user's information from database including user data and restaurant menu, delete data from storage and from authentication --------- Co-authored-by: Abdel Mouzahir <123598139+Abdelmouzahir@users.noreply.github.com> * trying to show username in saitstaff * console log * small change * remove azure * adding loading animation * loading in all login pages * trying to fetch the name * fetch data in login * comment to hunar * problem solved mentioned by abdel * updatinf sait-login * add feature of edit and update active state * add search functionality in sait-staff * add feature of updating password and delete account * small change * add profil picture in saitadmin * approuval * add profile page for sait-staff and resolve the problem related sait-staff login * connect profile part with database * update sait-staff components --------- Co-authored-by: Abdel Mouzahir <123598139+Abdelmouzahir@users.noreply.github.com> Co-authored-by: abdel mouzahir --- Components/UserGreeting.js | 48 +++---- app/sait-staff/page.js | 32 ++++- app/sait-staff/settingS/index.js | 22 +-- app/sait-staff/settingS/userProfile/page.js | 141 ++++++++++++++++++-- 4 files changed, 179 insertions(+), 64 deletions(-) diff --git a/Components/UserGreeting.js b/Components/UserGreeting.js index bc51a91..e446228 100644 --- a/Components/UserGreeting.js +++ b/Components/UserGreeting.js @@ -1,43 +1,31 @@ import { useEffect, useState } from "react"; -import { AiOutlineUser } from "react-icons/ai"; -import { useUserAuth } from "@/services/utils"; -import { getSaitDataByUser } from "@/services/GetRequest/getRequest"; -const UserGreeting = ({setActiveTab}) => { - const { user } = useUserAuth(); - const [userData, setUserData] = useState(null); - const [userName, setUserName] = useState("User"); - const [email, setEmail] = useState(""); - const [userImage, setUserImage] = useState(""); - - async function fetchSaitStaffUserInformation() { - const data = await getSaitDataByUser(user); - setUserData(data); - console.log(data); - } +const UserGreeting = ({ setActiveTab, data }) => { + const [userName, setUserName] = useState(data[0].name); + const [email, setEmail] = useState(data[0].email); + const [userImage, setUserImage] = useState(data[0].imageUrl); useEffect(() => { - if (!user==false && user){ - fetchSaitStaffUserInformation(); + if(data){ + setUserName(data[0].name); + setEmail(data[0].email); + setUserImage(data[0].imageUrl); } - }, [user]); - - useEffect(() => { - if(userData){ - setUserName(userData[0].name); - setEmail(userData[0].email); - setUserImage(userData[0].imageUrl); - } - }, [userData]); - - useEffect(() => {console.log(userImage)}, [userName, email, userImage]); + }, [data]); return ( -
{setActiveTab("setting")}}> +
{ + setActiveTab("setting"); + }} + >
User Image
diff --git a/app/sait-staff/page.js b/app/sait-staff/page.js index 0e52c61..c4319a7 100644 --- a/app/sait-staff/page.js +++ b/app/sait-staff/page.js @@ -9,15 +9,33 @@ import UserGreeting from "@/Components/UserGreeting"; import SaitStaffNav from "@/Components/SaitStaffNav"; import { useState, useEffect } from "react"; import { useRouter } from "next/navigation"; +import { getSaitDataByUser } from "@/services/GetRequest/getRequest"; +import { useUserAuth } from "@/services/utils"; export default function Page() { const [isCollapsed, setIsCollapsed] = useState(false); const [activeTab, setActiveTab] = useState("home"); // Set default active tab - const [user, setUser] = useState(null); const [hideUserGreeting, setHideUserGreeting] = useState(false); + const [userData, setUserData] = useState(null); + const {user} = useUserAuth(); const router = useRouter(); + //getting user data + async function fetchSaitStaffUserInformation() { + const data = await getSaitDataByUser(user); + setUserData(data); + console.log("default: ",data); + } + + useEffect(() => { + if (!user == false && user) { + fetchSaitStaffUserInformation(); + } + },[user]); + useEffect(() => {console.log('user data changed')},[userData]) + + // function to select the tab const handleTabClick = (tabName) => { setActiveTab(tabName); @@ -51,7 +69,7 @@ export default function Page() { setIsCollapsed={setIsCollapsed} onTabClick={handleTabClick} /> -
- +
-
+
):( +
+

Loading...

+
)} + ); } diff --git a/app/sait-staff/settingS/index.js b/app/sait-staff/settingS/index.js index 3341446..a12a506 100644 --- a/app/sait-staff/settingS/index.js +++ b/app/sait-staff/settingS/index.js @@ -6,7 +6,6 @@ import { Button } from "@/components/ui/button" import Passwordreset from "./passwordreset/page" import { Label } from "@/components/ui/label" import { Input } from "@/components/ui/input" -import { getSaitDataByUser } from "@/services/GetRequest/getRequest" import { useUserAuth } from "@/services/utils" import { useEffect, useState } from "react"; import { getAuth } from "firebase/auth"; @@ -15,25 +14,16 @@ import { useRouter } from "next/navigation"; import UserProfile from "./userProfile/page"; -export default function Settings() { +export default function Settings({data,getUserData}) { const route = useRouter(); const [userEmail, setUserEmail] = useState(""); const { user } = useUserAuth(); const auth = getAuth(); - const [saitData, setSaitData] = useState(null); - async function getUserData(){ - const data = await getSaitDataByUser(user); + const [saitData, setSaitData] = useState(data); + + useEffect(() => {if(data){ setSaitData(data); - } - useEffect(() => { - if(user!==false && user){ - getUserData(); - console.log(auth.currentUser) - } - if(user==false){ - route.push("/"); - } - },[user]); + }}, [data]); const handleDeleteAccount = async (e) => { e.preventDefault(); @@ -51,7 +41,7 @@ export default function Settings() {
{saitData ? (
- +
diff --git a/app/sait-staff/settingS/userProfile/page.js b/app/sait-staff/settingS/userProfile/page.js index 3341d58..8322609 100644 --- a/app/sait-staff/settingS/userProfile/page.js +++ b/app/sait-staff/settingS/userProfile/page.js @@ -5,32 +5,141 @@ */ "use client"; -import { useState, useEffect } from "react"; -import { Card, CardContent, CardFooter } from "@/components/ui/card"; -import { Label } from "@/components/ui/label"; -import { Switch } from "@/components/ui/switch"; -import { Input } from "@/components/ui/input"; -import { Button } from "@/components/ui/button"; +import { useState, useEffect, useRef, use } from "react"; +import { Card, CardContent, CardFooter } from "@/Components/ui/card"; +import { Label } from "@/Components/ui/label"; +import { Input } from "@/Components/ui/input"; +import { Button } from "@/Components/ui/button"; import { formatPhoneNumber } from "@/Constant/formated"; +import { db } from "@/app/firebase/config"; +import { updateDoc, doc } from "firebase/firestore"; +import { CgArrowsExchange } from "react-icons/cg"; +import { ref, uploadBytesResumable, getDownloadURL } from "firebase/storage"; +import { storage } from "@/app/firebase/config"; -export default function UserProfile({ data }) { +export default function UserProfile({ data, getUserData }) { const [name, setName] = useState(data[0].name); const [email, setEmail] = useState(data[0].email); const [phoneNumber, setPhoneNumber] = useState(data[0].phoneNumber); const [address, setAddress] = useState(data[0].address); const [role, setRole] = useState(data[0].role); - const [active, setActive] = useState(data[0].active); + const fileInpt = useRef(null); + useEffect(() => { - console.log("data: ", data); + if (data) { + setName(data[0].name); + setEmail(data[0].email); + setPhoneNumber(data[0].phoneNumber); + setAddress(data[0].address); + setRole(data[0].role); + } }, [data]); + + const handleDivClick = () => { + fileInpt.current.click(); + }; + + async function uploadImage(image) { + const storageRef = ref(storage, `Saitstaff/${data[0].uid}/${image.name}`); + const uploadTask = uploadBytesResumable(storageRef, image); + uploadTask.on( + "state_changed", + (snapshot) => { + const fileProgress = + (snapshot.bytesTransferred / snapshot.totalBytes) * 100; + console.log(`Upload is ${fileProgress}% done`); + switch (snapshot.state) { + case "paused": + console.log("Upload is paused"); + break; + case "running": + console.log("Upload is running"); + break; + } + }, + (error) => { + console.error("Upload failed", error); + }, + async () => { + const downloadURL = await getDownloadURL(uploadTask.snapshot.ref); + + try { + // Query to find the restaurant document with the matching userId + const docRef = doc(db, "saitStaff", data[0].id); + await updateDoc(docRef, { + imageUrl: downloadURL, + }).then(() => { + alert("Image uploaded successfully"); + getUserData(); + }); + } catch (error) { + console.error("Error writing document: ", error); + } + } + ); + } + + const validExtensions = [ + ".png", + ".jpeg", + ".jpg", + ".gif", + ".bmp", + ".tiff", + ".tif", + ".svg", + ".webp", + ".ico", + ".heif", + ".heic", + ".raw", + ]; + const handleFileChange = (event) => { + const file = event.target.files[0]; + if (validExtensions.some((ext) => file.name.toLowerCase().endsWith(ext))) { + uploadImage(file); + } else { + console.log("Invalid file format"); + } + }; + + async function updateProfile(e) { + e.preventDefault(); + try { + const docRef = doc(db, "saitStaff", data[0].id); + await updateDoc(docRef, { + name: name, + phoneNumber: phoneNumber, + address: address, + }).then(() => { + alert("Profile updated successfully"); + getUserData(); + }); + } catch (err) { + console.log(err); + } + } return ( // Changes to be made here
+ > + handleFileChange(e)} + className="hidden" + /> +
+ +
+

{data[0].name}

@@ -97,8 +206,14 @@ export default function UserProfile({ data }) {
-