-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented home feed for unauthenticated users
- Show community logo on post items while in the nome page
- Loading branch information
Showing
4 changed files
with
146 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |