Skip to content

Commit

Permalink
Implemented home feed for authenticated users
Browse files Browse the repository at this point in the history
- Added state to track whether user's community data has been fetched
  • Loading branch information
mbeps committed Feb 21, 2023
1 parent 451a639 commit cf161ba
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/atoms/communitiesAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ export interface CommunitySnippet {
interface CommunityState {
mySnippets: CommunitySnippet[]; // stores a list of community snippets
currentCommunity?: Community; // user is not always in a community hence optional
snippetFetched: boolean;
}

/**
* Initially, the array for the community state is empty.
*/
const defaultCommunityState: CommunityState = {
mySnippets: [],
snippetFetched: false,
};

/**
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useCommunityData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const useCommunityData = () => {
setCommunityStateValue((prev) => ({
...prev,
mySnippets: snippets as CommunitySnippet[],
snippetFetched: true,
}));
} catch (error: any) {
console.log("Error: getMySnippets", error);
Expand Down Expand Up @@ -193,6 +194,7 @@ const useCommunityData = () => {
setCommunityStateValue((prev) => ({
...prev,
mySnippets: [],
snippetFetched: false,
}));
return;
}
Expand Down
60 changes: 54 additions & 6 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,28 @@ 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 {
collection,
getDocs,
limit,
orderBy,
query,
where,
} 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";
import useCommunityData from "@/hooks/useCommunityData";

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

export default function Home() {
const [user, loadingUser] = useAuthState(auth);
const [loading, setLoading] = useState(false);
const communityStateValue = useRecoilValue(communityState);
const { communityStateValue } = useCommunityData();
const {
setPostStateValue,
postStateValue,
Expand All @@ -30,9 +38,39 @@ export default function Home() {
onDeletePost,
} = usePosts();

const buildUserHomeFeed = () => {};
const buildUserHomeFeed = async () => {
setLoading(true);

try {
if (communityStateValue.mySnippets.length) {
const myCommunityIds = communityStateValue.mySnippets.map(
(snippet) => snippet.communityId
);
const postQuery = query(
collection(firestore, "posts"),
where("communityId", "in", myCommunityIds),
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[],
}));
} else {
buildGenericHomeFeed();
}
} catch (error) {
} finally {
setLoading(false);
}
};

const buildNoUserHomeFeed = async () => {
const buildGenericHomeFeed = async () => {
setLoading(true);
try {
const postQuery = query(
Expand All @@ -48,22 +86,32 @@ export default function Home() {
posts: posts as Post[],
}));
} catch (error) {
console.log("Error: buildNoUserHomeFeed", error);
console.log("Error: buildGenericHomeFeed", error);
} finally {
setLoading(false);
}
};

const getUserPostVotes = () => {};

/**
* Loads the home feed for authenticated users.
* Runs when the community snippets have been fetched when the user
*/
useEffect(() => {
if (communityStateValue.mySnippets) {
buildUserHomeFeed();
}
}, [communityStateValue.snippetFetched]);

/**
* 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();
buildGenericHomeFeed();
}
}, [user, loadingUser]);

Expand Down

0 comments on commit cf161ba

Please sign in to comment.