From 2a6def4501c4a8e3c563cad2d08051f30e45c6b6 Mon Sep 17 00:00:00 2001 From: Joel Komieter Date: Sat, 3 Aug 2024 14:29:46 -0400 Subject: [PATCH] New push --- .../components/AvatarPopoverContent.tsx | 1 - campuscommune/hooks/useGetCurrentUser.tsx | 20 +----- campuscommune/modals/buttons/PostModeBtns.tsx | 1 - .../modals/questionModal/AskModal.tsx | 66 ++++++++++--------- campuscommune/mypages/layout/Header.tsx | 9 --- campuscommune/mypages/profile/Hero.tsx | 15 ++++- campuscommune/mypages/profile/ProfileFeed.tsx | 2 +- 7 files changed, 51 insertions(+), 63 deletions(-) diff --git a/campuscommune/components/AvatarPopoverContent.tsx b/campuscommune/components/AvatarPopoverContent.tsx index 624fe53..d7e1e38 100644 --- a/campuscommune/components/AvatarPopoverContent.tsx +++ b/campuscommune/components/AvatarPopoverContent.tsx @@ -84,7 +84,6 @@ const AvatarPopoverContent: React.FC = ({ user_name, user_email, }) => { - const router = useRouter(); return ( diff --git a/campuscommune/hooks/useGetCurrentUser.tsx b/campuscommune/hooks/useGetCurrentUser.tsx index 3dd8a33..eedb304 100644 --- a/campuscommune/hooks/useGetCurrentUser.tsx +++ b/campuscommune/hooks/useGetCurrentUser.tsx @@ -3,19 +3,17 @@ import { useAuthState } from "react-firebase-hooks/auth"; import { auth, db } from "@/firebase/config"; import { currentUserType } from "@/types"; import { collection, getDocs, query, where } from "firebase/firestore"; -import { useRouter } from "next/navigation"; import { onAuthStateChanged } from "firebase/auth"; -// This hook is used to get the current user's data from the database + const useGetCurrentUser = () => { const [user] = useAuthState(auth); const [currentUser, setCurrentUser] = React.useState({} as currentUserType); const usersCollectionRef = collection(db, "user"); - const router = useRouter(); - useEffect(() => { const getCurrentUser = async () => { + if (!user) return; const userRef = query(usersCollectionRef, where("email", "==", user?.email || "")); const querySnapshot = await getDocs(userRef); setCurrentUser(querySnapshot.docs.map((doc) => doc.data())[0] as currentUserType); @@ -23,20 +21,6 @@ const useGetCurrentUser = () => { getCurrentUser(); }, [user]); - - - useEffect(() => { - // listen for auth state changes - const unsubscribe = onAuthStateChanged(auth, (user) => { - if (!user) { - router.push("/auth/signin"); - } - }); - - return () => { - unsubscribe(); - } - }, []); return { currentUser }; } diff --git a/campuscommune/modals/buttons/PostModeBtns.tsx b/campuscommune/modals/buttons/PostModeBtns.tsx index a8bfa88..29cbe98 100644 --- a/campuscommune/modals/buttons/PostModeBtns.tsx +++ b/campuscommune/modals/buttons/PostModeBtns.tsx @@ -41,7 +41,6 @@ const PostModeBtns: React.FC = ({ reader.onload = async (event: any) => { const image = await resizeFile(file) as string; setImage(image as string); - console.log(`Post: ${event.target.result}`); }; reader.readAsDataURL(file); diff --git a/campuscommune/modals/questionModal/AskModal.tsx b/campuscommune/modals/questionModal/AskModal.tsx index 89d1671..e410a15 100644 --- a/campuscommune/modals/questionModal/AskModal.tsx +++ b/campuscommune/modals/questionModal/AskModal.tsx @@ -10,6 +10,7 @@ import BottomActions from "../buttons/BottomActions"; import CreateMode from "./CreateMode"; import { ModalFooter } from "@nextui-org/react"; import { Answer, QuestionType } from "@/types"; +import { useFeed } from "@/services/useFeed"; @@ -27,6 +28,7 @@ const AskModal = () => { const { setPostLoading } = usePostLoadingStore(); const { currentUser } = useGetCurrentUser(); const [categories, setCategories] = useState([]); + const { mutate } = useFeed(); const handleAddQuestion = useCallback(async () => { @@ -46,7 +48,7 @@ const AskModal = () => { answers: [] as Answer[], } as unknown as QuestionType; - await addDoc(questionsCollectionRef, Question); + Object.keys(currentUser).length > 0 && await addDoc(questionsCollectionRef, Question); setOpen(false); setStep(1); setText(""); @@ -56,6 +58,7 @@ const AskModal = () => { }, 2000); toast.success("Question added successfully!"); + mutate(); } catch (error) { console.log(error); toast.error("Error adding question"); @@ -64,16 +67,15 @@ const AskModal = () => { }, [text]); const handleAddPost = useCallback(async () => { - if (title.split("").length < 1 && description.split("").length < 1) return; + if (title.split("").length === 0 && description.split("").length === 0) return; try { setPostLoading(true); - const Post = { title: title, body: description, author_email: currentUser.email, author_major: currentUser.major, - author_name: currentUser.username, + author_name: currentUser.full_name, created_at: new Date(), type: "post", image: image as string, @@ -93,7 +95,7 @@ const AskModal = () => { console.log(error); toast.error("Error adding post"); } - + }, []); const handleChangeStep = useCallback(() => { @@ -106,39 +108,39 @@ const AskModal = () => { return ( -
- + + + - - - -
+ + ); }; diff --git a/campuscommune/mypages/layout/Header.tsx b/campuscommune/mypages/layout/Header.tsx index 366d3d8..352a291 100644 --- a/campuscommune/mypages/layout/Header.tsx +++ b/campuscommune/mypages/layout/Header.tsx @@ -24,18 +24,10 @@ const Header = () => { const { toggleSidebar, isSidebarOpen } = useSidebarStore(); const [user] = useAuthState(auth); const [ notificationsCount, setNotificationsCount ] = useState(0); - const usersCollectionRef = collection(db, "user"); const notificationsCollectionRef = collection(db, "notifications"); - // const [currentUser, setCurrentUser] = useState({} as currentUserType); const { currentUser } = useGetCurrentUser(); useEffect(() => { - const getCurrentUser = async () => { - // const userRef = query(usersCollectionRef, where("email", "==", user?.email || "")); - // const querySnapshot = await getDocs(userRef); - // setCurrentUser(querySnapshot.docs.map((doc) => doc.data())[0] as currentUserType); - }; - // listen for queried notifications for currentUser and update notificationsCount const notificationsRef = query(notificationsCollectionRef, where("recipient_email", "==", user?.email || "")); const unsubscribe = onSnapshot(notificationsRef, (snapshot) => { @@ -43,7 +35,6 @@ const Header = () => { }); return () => { - getCurrentUser(); unsubscribe(); } }, [user]); diff --git a/campuscommune/mypages/profile/Hero.tsx b/campuscommune/mypages/profile/Hero.tsx index da6c610..c6b0c51 100644 --- a/campuscommune/mypages/profile/Hero.tsx +++ b/campuscommune/mypages/profile/Hero.tsx @@ -8,6 +8,8 @@ import ProfileAbout from "./ProfileAbout"; import ProfileBtns from "./ProfileBtns"; import { Popover, PopoverTrigger, PopoverContent } from "@nextui-org/react"; import { FaTwitter } from "react-icons/fa"; +import { SlPencil } from "react-icons/sl"; +import { useRef, useState } from "react"; const Hero: React.FC = ({ @@ -23,6 +25,8 @@ const Hero: React.FC = ({ level, about_me, }) => { + const [showEditProfile, setShowEditProfile] = useState(false); + const profileRef = useRef(null); const levels = { 1: "Freshman", @@ -36,11 +40,20 @@ const Hero: React.FC = ({
+ {showEditProfile && profileRef.current && +
+ +
+ } setShowEditProfile(true)} + onMouseLeave={() => setShowEditProfile(false)} + onClick={() => {}} + ref={profileRef} src={profile_pic || "https://collegelifemadeeasy.com/wp-content/uploads/2023/01/black-women-scholarships.jpg"} size="lg" alt="Profile" - style={{width: "120px", height: "120px"}} + style={{width: "120px", height: "120px", cursor: "pointer"}} />
diff --git a/campuscommune/mypages/profile/ProfileFeed.tsx b/campuscommune/mypages/profile/ProfileFeed.tsx index d8dac35..6ee335a 100644 --- a/campuscommune/mypages/profile/ProfileFeed.tsx +++ b/campuscommune/mypages/profile/ProfileFeed.tsx @@ -32,7 +32,7 @@ const ProfileFeed = ({ if (feed.length <= 0) return (
-

No posts yet

+

No posts yet

);