diff --git a/src/components/User/UserProfile.jsx b/src/components/User/UserProfile.jsx index a650ec2..0b579a4 100644 --- a/src/components/User/UserProfile.jsx +++ b/src/components/User/UserProfile.jsx @@ -65,15 +65,18 @@ export default function UserProfile({ user }) { formData.append("file", file); formData.append("upload_preset", "hopehub"); + //store the image in cloudinary axios .post( "https://api.cloudinary.com/v1_1/dxic1agza/image/upload", formData ) .then((response) => { + //add the photourl to the user object + setCloudinaryImage(response.data.secure_url); setUser({ ...user, photoURL: response.data.secure_url }); - handlePhotoChange(); + handlePhotoChange(); // to update user infos in firebase }) .catch((error) => { console.error("cloudinary err", error); @@ -132,6 +135,7 @@ export default function UserProfile({ user }) { "dark:bg-slate-800 dark:text-NeutralWhite text-NeutralBlack bg-NeutralWhite", }); } else { + // change the user infos locally console.log("after update"); setUser({ ...user, @@ -147,7 +151,7 @@ export default function UserProfile({ user }) { photoURL: cloudinaryImage, idcard: idcard, }); - await updateUserProfile(); + await updateUserProfile(); // change the data in firebase toast.success("profile updated", { position: toast.POSITION.BOTTOM_CENTER, autoClose: 2500, @@ -212,6 +216,7 @@ export default function UserProfile({ user }) { if (phone !== auth.currentUser.phoneNumber) handlePhoneChange(); if (cloudinaryImage !== auth.currentUser.photoURL) handlePhotoChange(); + // update the user collection in firestore await updateDoc(doc(db, "users", user.uid), { ...user, name: fullName, diff --git a/src/components/UserAuth/login.jsx b/src/components/UserAuth/login.jsx index 90ae195..7e815a2 100755 --- a/src/components/UserAuth/login.jsx +++ b/src/components/UserAuth/login.jsx @@ -36,6 +36,7 @@ function Login({ isChecked, setChecked }) { return; } try { + // firebase function for sign in const userCredential = await signInWithEmailAndPassword( auth, email, diff --git a/src/components/UserAuth/signup.jsx b/src/components/UserAuth/signup.jsx index 373b093..96b32af 100755 --- a/src/components/UserAuth/signup.jsx +++ b/src/components/UserAuth/signup.jsx @@ -44,12 +44,13 @@ function Signup({ isChecked, setChecked }) { } try { + // create the user in firebase const userCredential = await createUserWithEmailAndPassword( auth, email, password ); - + //this data will be stored in firestore const userData = { uid: userCredential.user.uid, // Save the UID birthDate: bdate, @@ -58,13 +59,13 @@ function Signup({ isChecked, setChecked }) { name: `${firstname} ${lastname}`, email: email, }; - + // we update the user name await updateProfile(userCredential.user, { displayName: `${firstname} ${lastname}`, }); - + // we save the user infos in firestore await addUserToFirestore(userCredential, userData); - + //we store the uid in cookie to keep him logged in Cookie.set("loggedInUser", userCredential.user.uid, { expires: 7 }); router.push(`/thanks?from=${pathname}`); authChange(); @@ -80,7 +81,7 @@ function Signup({ isChecked, setChecked }) { autoClose: 2500, }); } finally { - // >>>>>>> develop + // reste all the fields setEmail(""); setLastname(""); setConfirmemail(""); diff --git a/src/components/booking/7Submission.jsx b/src/components/booking/7Submission.jsx index f39ca95..3e2d6b7 100644 --- a/src/components/booking/7Submission.jsx +++ b/src/components/booking/7Submission.jsx @@ -1,4 +1,12 @@ -import { doc, getDoc, setDoc, updateDoc } from "firebase/firestore"; +import { + collection, + doc, + getDoc, + getDocs, + setDoc, + updateDoc, + where, +} from "firebase/firestore"; import Head from "next/head"; import { useTranslation } from "next-i18next"; import React from "react"; @@ -9,8 +17,6 @@ export default function Submission({ OnNext, OnPrevious }) { const { bookingInfos, user, setUser } = useAppcontext(); const { t } = useTranslation("common"); async function handleSubmit() { - // send request to save the data - // ... const appointment = { date: bookingInfos.date, time: bookingInfos.start, @@ -89,6 +95,64 @@ export default function Submission({ OnNext, OnPrevious }) { `, }), }); + + const userId = user.uid; + + async function assignTherapist() { + if (!userId) { + console.error("User ID is undefined or null."); + return; + } + + try { + const usersRef = collection(db, "users"); + const usersSnapshot = await getDocs(usersRef); + const therapists = []; + + usersSnapshot.forEach((doc) => { + const userData = doc.data(); + if (userData.isTherapist === true) { + therapists.push({ id: doc.id, data: userData }); + } + }); + + if (therapists.length > 0) { + const randomIndex = Math.floor( + Math.random() * therapists.length + ); + const selectedTherapist = therapists[randomIndex]; + + // Update the user's document with the assigned therapist + await updateDoc(doc(db, "users", userId), { + hasTherapist: true, + therapistId: selectedTherapist.id, + }); + + // Add patient's ID to therapist's document + const therapistRef = doc(db, "users", selectedTherapist.id); + const therapistDoc = await getDoc(therapistRef); + const therapistData = therapistDoc.data(); + + if (!therapistData.patients) { + therapistData.patients = [userId]; + } else { + therapistData.patients.push(userId); + } + + await updateDoc(therapistRef, { + patients: therapistData.patients, + }); + + console.log("Therapist assigned:", selectedTherapist); + } else { + console.log("No therapists available."); + } + } catch (error) { + console.error("Error assigning therapist:", error); + } + } + + assignTherapist(); OnNext(); } diff --git a/src/components/calendarEvents/EventModal.jsx b/src/components/calendarEvents/EventModal.jsx index 696ec8a..a58b34b 100644 --- a/src/components/calendarEvents/EventModal.jsx +++ b/src/components/calendarEvents/EventModal.jsx @@ -1,7 +1,10 @@ import Link from "next/link"; import React from "react"; -function EventModal({ event, position, closeModal, userId }) { +function EventModal({ eventData, position, closeModal, userId }) { + console.log("info event", eventData._def.extendedProps); + const event = { ...eventData._def.extendedProps, date: eventData.startStr }; + console.log("event DAta nnennenen", event); return ( <>
{ - return { title: ev.time, id: ev.uid, ...ev }; - }) */ - : user?.appointments?.map((d) => ({ title: d.time, ...d })); - console.log("calender ave", events); - // console.log("appointments", appointments); - // if(user.isTherapist){ - // appointments.forEach((obj, id) => { - // const key = Object.keys(obj)[id]; - // // console.log('objjjj',obj[key]) - // return { title: obj[key]?.time, id: key, ...obj[key] }; - // }) - // } + // const [filteredAppointments, setFilteredAppointments] = useState([]); const [modalOpen, setModalOpen] = useState(false); const [eventData, setEventData] = useState(null); @@ -38,16 +25,23 @@ function Calendar({ appointments, user }) { left: "0px", top: "0px", }); + + const events = user.isTherapist + ? appointments.filter( + (appointment) => user?.patients.includes(appointment.id) // we filter only the patients assigned to this therapist + ) + : user?.appointments?.map((appointment) => ({ + title: appointment.time, + ...appointment, + })); // return the evens array in format accepted by FullCalndar + function handleEventClick(info) { if (user.isTherapist) { - const event = events.filter((obj) => { - return obj.id === info.event.id; - }); - setEventData(event[0]); - const click = info.jsEvent.clientX; + setEventData(info.event); + const click = info.jsEvent.clientX; // the mouse position let horizontal = 0; - const modalWidth = window.innerWidth > 700 ? 390 : 240; - const rect = info.el.getBoundingClientRect(); + const modalWidth = window.innerWidth > 700 ? 390 : 240; //specify the modal widt, small or bigger screens + const rect = info.el.getBoundingClientRect(); // get the event rectangle to use its coordinates to calculate the modal postion if (click + modalWidth + rect.width > window.innerWidth) { horizontal = rect.left - modalWidth; // if(horizontal-240<0){ @@ -55,16 +49,14 @@ function Calendar({ appointments, user }) { } else { horizontal = rect.left + rect.width; } - // console.log('horiiiiiiiii______window',click+modalWidth+rect.width, horizontal, window.innerWidth) setModalPosition({ left: `${horizontal}px`, top: `${rect.top - rect.height}px`, }); - setModalOpen(!modalOpen); + setModalOpen((prevModalOpen) => !prevModalOpen); } return; } - useEffect(() => {}, []); return ( @@ -72,10 +64,12 @@ function Calendar({ appointments, user }) { {modalOpen && ( { - setModalOpen(false); + setModalOpen((prevModalOpen) => !prevModalOpen); + setEventData(null); // Reset event data when closing the modal + // console.log("im in here", events); }} userId={user.uid} /> @@ -101,14 +95,20 @@ function Calendar({ appointments, user }) { center: "", end: "", }} - customButtons={{ - bookApp: { - text: "Book appointment", - click: function () { - router.push(`/booking?userid=${user.uid}`); - }, - }, - }} + customButtons={ + user.isTherapist + ? {} + : { + bookApp: { + text: "Book appointment", + click: function () { + router.push( + `/booking?userid=${user.uid}` + ); + }, + }, + } + } nowIndicator={true} />
@@ -147,10 +147,8 @@ export async function getServerSideProps({ locale, query }) { }); } } - console.log("appointments..............", appointments); const userDoc = await getDoc(doc(db, "users", userId)); - console.log("im userdoc in serverside", userDoc.exists()); if (!userDoc.exists()) { // Handle the case when the user with the specified ID is not found return { notFound: true };