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 ( <>