Skip to content

Commit

Permalink
Implemented home feed for unauthenticated users
Browse files Browse the repository at this point in the history
- Show community logo on post items while in the nome page
  • Loading branch information
mbeps committed Feb 21, 2023
1 parent c80430a commit 451a639
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 10 deletions.
7 changes: 6 additions & 1 deletion src/components/Posts/NewPostForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -63,7 +64,10 @@ export type FormTab = {
icon: typeof Icon.arguments;
};

const NewPostForm: React.FC<NewPostFormProps> = ({ user }) => {
const NewPostForm: React.FC<NewPostFormProps> = ({
user,
communityImageURL,
}) => {
const router = useRouter();
const [selectedTab, setSelectedTab] = useState(formTabs[0].title); // formTabs[0] = Post
const [textInputs, setTextInputs] = useState({
Expand All @@ -79,6 +83,7 @@ const NewPostForm: React.FC<NewPostFormProps> = ({ 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,
Expand Down
39 changes: 38 additions & 1 deletion src/components/Posts/PostItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Flex,
Icon,
Image,
Link,
Skeleton,
Spinner,
Stack,
Expand All @@ -19,6 +20,7 @@ import {
IoArrowDownCircleSharp,
IoArrowUpCircleOutline,
IoArrowUpCircleSharp,
IoPeopleCircleOutline,
} from "react-icons/io5";
import { MdOutlineDelete } from "react-icons/md";

Expand All @@ -43,6 +45,7 @@ type PostItemProps = {
) => void; // function to handle voting
onDeletePost: (post: Post) => Promise<boolean>; // 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<PostItemProps> = ({
Expand All @@ -52,6 +55,7 @@ const PostItem: React.FC<PostItemProps> = ({
onVote,
onDeletePost,
onSelectPost,
isHomePage,
}) => {
const [loadingImage, setLoadingImage] = useState(true);
const [error, setError] = useState(false);
Expand Down Expand Up @@ -155,8 +159,41 @@ const PostItem: React.FC<PostItemProps> = ({
{/* right side containing content */}
<Flex direction="column" width="100%">
<Stack spacing={1} p="10px">
<Stack direction="row" spacing={0.5} align="center" fontSize="9pt">
<Stack
direction="row"
spacing={0.5}
align="center"
fontSize="9pt"
borderRadius="full"
boxSize="18px"
mr={2}
width="100%"
>
{/* Check whether home page to decide whether to display community image */}
{isHomePage && (
<>
{post.communityImageURL ? (
<Image src={post.communityImageURL} alt="Community logo" />
) : (
<Icon
as={IoPeopleCircleOutline}
mr={1}
fontSize="18pt"
color="red.500"
/>
)}
<Link href={`/community/${post.communityId}`}>
<Text
fontWeight={700}
_hover={{ textDecoration: "underline" }}
pr={2}
onClick={(event) => event.stopPropagation()}
>
{post.communityId}
</Text>
</Link>
</>
)}
<Text>{topText}</Text>
</Stack>
{/* post title */}
Expand Down
7 changes: 6 additions & 1 deletion src/pages/community/[communityId]/submit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ const SubmitPostPage: React.FC = () => {
Create Post
</Text>
</Box>
{user && <NewPostForm user={user} />}
{user && (
<NewPostForm
user={user}
communityImageURL={communityStateValue.currentCommunity?.imageURL}
/>
)}
</>
<>
{communityStateValue.currentCommunity && (
Expand Down
103 changes: 96 additions & 7 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div>Hello</div>
)
<PageContent>
<>
<CreatePostLink />
{loading ? (
<PostLoader />
) : (
<Stack>
{postStateValue.posts.map((post) => (
<PostItem
key={post.id}
post={post}
onSelectPost={onSelectPost}
onDeletePost={onDeletePost}
onVote={onVote}
userVoteValue={
postStateValue.postVotes.find(
(item) => item.postId === post.id
)?.voteValue
}
userIsCreator={user?.uid === post.creatorId}
isHomePage={true}
/>
))}
</Stack>
)}
</>

<></>
</PageContent>
);
}

0 comments on commit 451a639

Please sign in to comment.