Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion campuscommune/components/AvatarPopoverContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ const AvatarPopoverContent: React.FC<AvatarPopoverContentProps> = ({
user_name,
user_email,
}) => {

const router = useRouter();

return (
Expand Down
20 changes: 2 additions & 18 deletions campuscommune/hooks/useGetCurrentUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,24 @@ 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<currentUserType>({} 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);
};

getCurrentUser();
}, [user]);


useEffect(() => {
// listen for auth state changes
const unsubscribe = onAuthStateChanged(auth, (user) => {
if (!user) {
router.push("/auth/signin");
}
});

return () => {
unsubscribe();
}
}, []);

return { currentUser };
}
Expand Down
1 change: 0 additions & 1 deletion campuscommune/modals/buttons/PostModeBtns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const PostModeBtns: React.FC<PostModeBtnsProps> = ({
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);
Expand Down
66 changes: 34 additions & 32 deletions campuscommune/modals/questionModal/AskModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";



Expand All @@ -27,6 +28,7 @@ const AskModal = () => {
const { setPostLoading } = usePostLoadingStore();
const { currentUser } = useGetCurrentUser();
const [categories, setCategories] = useState<string[]>([]);
const { mutate } = useFeed();


const handleAddQuestion = useCallback(async () => {
Expand All @@ -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("");
Expand All @@ -56,6 +58,7 @@ const AskModal = () => {
}, 2000);

toast.success("Question added successfully!");
mutate();
} catch (error) {
console.log(error);
toast.error("Error adding question");
Expand All @@ -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,
Expand All @@ -93,7 +95,7 @@ const AskModal = () => {
console.log(error);
toast.error("Error adding post");
}

}, []);

const handleChangeStep = useCallback(() => {
Expand All @@ -106,39 +108,39 @@ const AskModal = () => {


return (
<div
className={`flex flex-col gap-2 overflow-y-scroll `}
>
<CreateMode
<div
className={`flex flex-col gap-2 overflow-y-scroll `}
>
<CreateMode
mode={mode}
setMode={setMode}
step={step}
text={text}
setText={setText}
image={image}
setImage={setImage}
title={title}
setTitle={setTitle}
description={description}
setDescription={setDescription}
setCategories={setCategories}
/>
<ModalFooter
style={{ borderTopWidth: "0.5px" }}
className="w-full py-3 px-3 border-t-neutral-700 flex items-end justify-end gap-3">
<BottomActions
mode={mode}
setMode={setMode}
step={step}
text={text}
handleChangeStep={handleChangeStep}
handleAddQuestion={handleAddQuestion}
setStep={setStep}
setText={setText}
image={image}
setImage={setImage}
title={title}
setTitle={setTitle}
description={description}
setDescription={setDescription}
setCategories={setCategories}
handleAddPost={handleAddPost}
/>
<ModalFooter
style={{ borderTopWidth: "0.5px" }}
className="w-full py-3 px-3 border-t-neutral-700 flex items-end justify-end gap-3">
<BottomActions
mode={mode}
step={step}
handleChangeStep={handleChangeStep}
handleAddQuestion={handleAddQuestion}
setStep={setStep}
setText={setText}
image={image}
setImage={setImage}
handleAddPost={handleAddPost}
/>
</ModalFooter>
</div>
</ModalFooter>
</div>
);
};

Expand Down
9 changes: 0 additions & 9 deletions campuscommune/mypages/layout/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,17 @@ const Header = () => {
const { toggleSidebar, isSidebarOpen } = useSidebarStore();
const [user] = useAuthState(auth);
const [ notificationsCount, setNotificationsCount ] = useState<number>(0);
const usersCollectionRef = collection(db, "user");
const notificationsCollectionRef = collection(db, "notifications");
// const [currentUser, setCurrentUser] = useState<currentUserType>({} 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) => {
setNotificationsCount(snapshot.docs.length);
});

return () => {
getCurrentUser();
unsubscribe();
}
}, [user]);
Expand Down
15 changes: 14 additions & 1 deletion campuscommune/mypages/profile/Hero.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<currentUserType> = ({
Expand All @@ -23,6 +25,8 @@ const Hero: React.FC<currentUserType> = ({
level,
about_me,
}) => {
const [showEditProfile, setShowEditProfile] = useState<boolean>(false);
const profileRef = useRef<HTMLSpanElement | null>(null);

const levels = {
1: "Freshman",
Expand All @@ -36,11 +40,20 @@ const Hero: React.FC<currentUserType> = ({
<div className="flex flex-col gap-4 w-full ">
<div className="flex flex-row gap-2 items-start w-full">
<div className="pr-2">
{showEditProfile && profileRef.current &&
<div className={`p-2 bg-blue-500 rounded-full absolute z-30 top-[${profileRef.current.getBoundingClientRect().top + 100}rem] left-[${profileRef.current.getBoundingClientRect().left}rem]`}>
<SlPencil size={18} className="text-[#fff] cursor-pointer" />
</div>
}
<Avatar
onMouseEnter={() => 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"}}
/>
</div>
<div className="flex flex-col gap-2">
Expand Down
2 changes: 1 addition & 1 deletion campuscommune/mypages/profile/ProfileFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const ProfileFeed = ({

if (feed.length <= 0) return (
<div className="w-full h-full flex justify-center items-center">
<h1 className="text-2xl font-semibold text-center">No posts yet</h1>
<h1 className="text-2xl font-semibold text-center tra">No posts yet</h1>
</div>
);

Expand Down