diff --git a/src/components/Posts/NewPostForm.tsx b/src/components/Posts/NewPostForm.tsx index 5251ef6..c8802a5 100644 --- a/src/components/Posts/NewPostForm.tsx +++ b/src/components/Posts/NewPostForm.tsx @@ -32,6 +32,7 @@ import TabItem from "./TabItem"; */ type NewPostFormProps = { user: User; // parent component checks user so additional checks aer not needed ut + communityImageURL?: string; }; // Tab items which are static (not react) hence outside @@ -63,7 +64,10 @@ export type FormTab = { icon: typeof Icon.arguments; }; -const NewPostForm: React.FC = ({ user }) => { +const NewPostForm: React.FC = ({ + user, + communityImageURL, +}) => { const router = useRouter(); const [selectedTab, setSelectedTab] = useState(formTabs[0].title); // formTabs[0] = Post const [textInputs, setTextInputs] = useState({ @@ -79,6 +83,7 @@ const NewPostForm: React.FC = ({ user }) => { // create a new post object const newPost: Post = { communityId: communityId as string, + communityImageURL: communityImageURL || "", creatorId: user?.uid, creatorUsername: user.email!.split("@")[0], title: textInputs.title, diff --git a/src/components/Posts/PostItem.tsx b/src/components/Posts/PostItem.tsx index d424a97..48b4dfd 100644 --- a/src/components/Posts/PostItem.tsx +++ b/src/components/Posts/PostItem.tsx @@ -5,6 +5,7 @@ import { Flex, Icon, Image, + Link, Skeleton, Spinner, Stack, @@ -19,6 +20,7 @@ import { IoArrowDownCircleSharp, IoArrowUpCircleOutline, IoArrowUpCircleSharp, + IoPeopleCircleOutline, } from "react-icons/io5"; import { MdOutlineDelete } from "react-icons/md"; @@ -43,6 +45,7 @@ type PostItemProps = { ) => void; // function to handle voting onDeletePost: (post: Post) => Promise; // function to handle deleting post onSelectPost?: (post: Post) => void; // optional because once a post is selected it cannot be reselected + isHomePage?: boolean; }; const PostItem: React.FC = ({ @@ -52,6 +55,7 @@ const PostItem: React.FC = ({ onVote, onDeletePost, onSelectPost, + isHomePage, }) => { const [loadingImage, setLoadingImage] = useState(true); const [error, setError] = useState(false); @@ -155,8 +159,41 @@ const PostItem: React.FC = ({ {/* right side containing content */} - + {/* Check whether home page to decide whether to display community image */} + {isHomePage && ( + <> + {post.communityImageURL ? ( + Community logo + ) : ( + + )} + + event.stopPropagation()} + > + {post.communityId} + + + + )} {topText} {/* post title */} diff --git a/src/pages/community/[communityId]/submit.tsx b/src/pages/community/[communityId]/submit.tsx index a396c4a..28ebe37 100644 --- a/src/pages/community/[communityId]/submit.tsx +++ b/src/pages/community/[communityId]/submit.tsx @@ -22,7 +22,12 @@ const SubmitPostPage: React.FC = () => { Create Post - {user && } + {user && ( + + )} <> {communityStateValue.currentCommunity && ( diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 4210c9f..92ec166 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -1,12 +1,101 @@ -import Head from 'next/head' -import Image from 'next/image' -import { Inter } from '@next/font/google' -import styles from '@/styles/Home.module.css' +import Head from "next/head"; +import Image from "next/image"; +import { Inter } from "@next/font/google"; +import styles from "@/styles/Home.module.css"; +import PageContent from "@/components/Layout/PageContent"; +import { useAuthState } from "react-firebase-hooks/auth"; +import { auth, firestore } from "@/firebase/clientApp"; +import { useEffect, useState } from "react"; +import { useRecoilValue } from "recoil"; +import { communityState } from "@/atoms/communitiesAtom"; +import { collection, getDocs, limit, orderBy, query } from "firebase/firestore"; +import usePosts from "@/hooks/usePosts"; +import { Post } from "@/atoms/postsAtom"; +import CreatePostLink from "@/components/Community/CreatePostLink"; +import PostLoader from "@/components/Posts/PostLoader"; +import { Stack } from "@chakra-ui/react"; +import PostItem from "@/components/Posts/PostItem"; -const inter = Inter({ subsets: ['latin'] }) +const inter = Inter({ subsets: ["latin"] }); export default function Home() { + const [user, loadingUser] = useAuthState(auth); + const [loading, setLoading] = useState(false); + const communityStateValue = useRecoilValue(communityState); + const { + setPostStateValue, + postStateValue, + onSelectPost, + onVote, + onDeletePost, + } = usePosts(); + + const buildUserHomeFeed = () => {}; + + const buildNoUserHomeFeed = async () => { + setLoading(true); + try { + const postQuery = query( + collection(firestore, "posts"), + orderBy("voteStatus", "desc"), + limit(10) + ); + + const postDocs = await getDocs(postQuery); + const posts = postDocs.docs.map((doc) => ({ id: doc.id, ...doc.data() })); + setPostStateValue((prev) => ({ + ...prev, + posts: posts as Post[], + })); + } catch (error) { + console.log("Error: buildNoUserHomeFeed", error); + } finally { + setLoading(false); + } + }; + + const getUserPostVotes = () => {}; + + /** + * Loads the home feed for unauthenticated users. + * Runs when there is no user and the system is no longer attempting to fetch a user. + * While the system is attempting to fetch user, the user is null. + */ + useEffect(() => { + if (!user && !loadingUser) { + buildNoUserHomeFeed(); + } + }, [user, loadingUser]); + return ( -
Hello
- ) + + <> + + {loading ? ( + + ) : ( + + {postStateValue.posts.map((post) => ( + item.postId === post.id + )?.voteValue + } + userIsCreator={user?.uid === post.creatorId} + isHomePage={true} + /> + ))} + + )} + + + <> + + ); }