Skip to content

Commit

Permalink
Added missing documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mbeps committed Mar 11, 2023
1 parent 4443f4b commit e879a65
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 37 deletions.
10 changes: 5 additions & 5 deletions src/pages/community/[communityId]/comments/[pid].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import { useAuthState } from "react-firebase-hooks/auth";
/**
* Displays a single post.
* Contains:
* - PostItem component
* - About component
* - Comments component
* - PostItem component
* - About component
* - Comments component
*
* @returns React.FC - Single post page with all components
* @returns {React.FC} - Single post page with all components
*/
const PostPage: React.FC = () => {
const { postStateValue, setPostStateValue, onDeletePost, onVote } =
Expand All @@ -34,7 +34,7 @@ const PostPage: React.FC = () => {
* Refreshing the page or pasting link to the post loads an empty page.
* This is because the community page was bypassed hence the state is empty.
* If the state is empty then fetch the data from Firebase.
* @param postId (string) - Post ID for the post to be fetched
* @param {string} postId - Post ID for the post to be fetched
*/
const fetchPost = async (postId: string) => {
try {
Expand Down
17 changes: 15 additions & 2 deletions src/pages/community/[communityId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ import React, { useEffect } from "react";
import { useSetRecoilState } from "recoil";
import safeJsonStringify from "safe-json-stringify";

/**
* @param {Community} communityData - Community data for the current community
*/
type CommunityPageProps = {
communityData: Community;
};

/**
* Displays the community page with the community's posts and information.
* @param {Community} communityData - Community data for the current community
* @returns {React.FC<CommunityPageProps>} - Community page component
*/
const CommunityPage: React.FC<CommunityPageProps> = ({ communityData }) => {
const setCommunityStateValue = useSetRecoilState(communityState);

Expand All @@ -28,11 +36,10 @@ const CommunityPage: React.FC<CommunityPageProps> = ({ communityData }) => {
}, [communityData, setCommunityStateValue]);

if (!communityData) {
// if community data is not available, return not found page
return <NotFound />;
}

//useEffect

return (
<>
<Header communityData={communityData} />
Expand All @@ -49,6 +56,12 @@ const CommunityPage: React.FC<CommunityPageProps> = ({ communityData }) => {
);
};

/**
* Gets the community data for the current community.
* Returns the community data as props to the client.
* @param {GetServerSidePropsContext} context - GetServerSidePropsContext object
* @returns {Promise<{props: {communityData: Community}}>} - Community data for the current community
*/
export async function getServerSideProps(context: GetServerSidePropsContext) {
// get the community data and pass it to client
try {
Expand Down
10 changes: 9 additions & 1 deletion src/pages/community/[communityId]/submit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ import AuthButtons from "@/components/Navbar/RightContent/AuthButtons";
import NewPostForm from "@/components/Posts/NewPostForm";
import { auth } from "@/firebase/clientApp";
import useCommunityData from "@/hooks/useCommunityData";
import { Box, Flex, Stack, Text } from "@chakra-ui/react";
import { Box, Stack, Text } from "@chakra-ui/react";
import React from "react";
import { useAuthState } from "react-firebase-hooks/auth";
import { useSetRecoilState } from "recoil";

/**
* Post submission page where the user can create a new post.
* If the user is not logged in, they will be prompted to log in.
* Displays:
* - Post creation form
* - Community information card
* @returns {React.FC} - Submit post page component
*/
const SubmitPostPage: React.FC = () => {
const [user] = useAuthState(auth);
// const communityStateValue = useRecoilValue(communityState);
Expand Down
64 changes: 35 additions & 29 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/* eslint-disable react-hooks/exhaustive-deps */
import Head from "next/head";
import Image from "next/image";
import { Inter } from "@next/font/google";
import styles from "@/styles/Home.module.css";
import { Post, PostVote } from "@/atoms/postsAtom";
import CreatePostLink from "@/components/Community/CreatePostLink";
import PersonalHome from "@/components/Community/PersonalHome";
import Recommendations from "@/components/Community/Recommendations";
import PageContent from "@/components/Layout/PageContent";
import { useAuthState } from "react-firebase-hooks/auth";
import PostItem from "@/components/Posts/PostItem";
import PostLoader from "@/components/Posts/PostLoader";
import { auth, firestore } from "@/firebase/clientApp";
import { useEffect, useState } from "react";
import { useRecoilValue } from "recoil";
import { communityState } from "@/atoms/communitiesAtom";
import useCommunityData from "@/hooks/useCommunityData";
import usePosts from "@/hooks/usePosts";
import { Stack } from "@chakra-ui/react";
import { Inter } from "@next/font/google";
import {
collection,
getDocs,
Expand All @@ -17,15 +19,8 @@ import {
query,
where,
} from "firebase/firestore";
import usePosts from "@/hooks/usePosts";
import { Post, PostVote } 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";
import useCommunityData from "@/hooks/useCommunityData";
import Recommendations from "@/components/Community/Recommendations";
import PersonalHome from "@/components/Community/PersonalHome";
import { useEffect, useState } from "react";
import { useAuthState } from "react-firebase-hooks/auth";

const inter = Inter({ subsets: ["latin"] });

Expand All @@ -41,30 +36,35 @@ export default function Home() {
onDeletePost,
} = usePosts();

/**
* Creates a home feed for a currently logged in user.
* If the user is a member of any communities, it will display posts from those communities.
* If the user is not a member of any communities, it will display generic posts.
*/
const buildUserHomeFeed = async () => {
setLoading(true);

try {
if (communityStateValue.mySnippets.length) {
const myCommunityIds = communityStateValue.mySnippets.map(
(snippet) => snippet.communityId
);
); // get all community ids that the user is a member of
const postQuery = query(
collection(firestore, "posts"),
where("communityId", "in", myCommunityIds),
// orderBy("voteStatus", "desc"),
limit(10)
);
); // get all posts in community with certain requirements
const postDocs = await getDocs(postQuery);
const posts = postDocs.docs.map((doc) => ({
id: doc.id,
...doc.data(),
}));
})); // get all posts in community

setPostStateValue((prev) => ({
...prev,
posts: posts as Post[],
}));
})); // set posts in state
} else {
buildGenericHomeFeed();
}
Expand All @@ -74,45 +74,51 @@ export default function Home() {
}
};

/**
* Creates a generic home feed for a user that is not logged in.
*/
const buildGenericHomeFeed = async () => {
setLoading(true);
try {
const postQuery = query(
collection(firestore, "posts"),
orderBy("voteStatus", "desc"),
limit(10)
);
); // get all posts in community with certain requirements

const postDocs = await getDocs(postQuery);
const posts = postDocs.docs.map((doc) => ({ id: doc.id, ...doc.data() }));
const postDocs = await getDocs(postQuery); // get all posts in community
const posts = postDocs.docs.map((doc) => ({ id: doc.id, ...doc.data() })); // get all posts in community
setPostStateValue((prev) => ({
...prev,
posts: posts as Post[],
}));
})); // set posts in state
} catch (error) {
console.log("Error: buildGenericHomeFeed", error);
} finally {
setLoading(false);
}
};

/**
* Gets the votes for the posts that are currently in the home feed.
*/
const getUserPostVotes = async () => {
try {
const postIds = postStateValue.posts.map((post) => post.id);
const postIds = postStateValue.posts.map((post) => post.id); // get all post ids in home feed
const postVotesQuery = query(
collection(firestore, `users/${user?.uid}/postVotes`),
where("postId", "in", postIds)
);
); // get all post votes for posts in home feed
const postVoteDocs = await getDocs(postVotesQuery);
const postVotes = postVoteDocs.docs.map((doc) => ({
id: doc.id,
...doc.data(),
}));
})); // get all post votes for posts in home feed

setPostStateValue((prev) => ({
...prev,
postVotes: postVotes as PostVote[],
}));
})); // set post votes in state
} catch (error) {}
};

Expand Down

0 comments on commit e879a65

Please sign in to comment.