-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e345b3c
commit f71d886
Showing
8 changed files
with
223 additions
and
12 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { Avatar } from "@chakra-ui/avatar"; | ||
import { Image } from "@chakra-ui/image"; | ||
import { Box, Flex, Text } from "@chakra-ui/layout"; | ||
import { Link, useNavigate } from "react-router-dom"; | ||
import Actions from "./Actions"; | ||
import { useEffect, useState } from "react"; | ||
import useShowToast from "../hooks/useShowToast"; | ||
import { formatDistanceToNow } from "date-fns"; | ||
|
||
const Post = ({ post, postedBy }) => { | ||
const [liked, setLiked] = useState(false); | ||
const [user, setUser] = useState(null); | ||
const showToast = useShowToast(); | ||
|
||
const navigate = useNavigate(); | ||
|
||
useEffect(() => { | ||
const getUser = async () => { | ||
try { | ||
const res = await fetch("/api/users/profile/" + postedBy); | ||
const data = await res.json(); | ||
console.log(data); | ||
if (data.error) { | ||
showToast("Error", data.error, "error"); | ||
return; | ||
} | ||
setUser(data); | ||
} catch (error) { | ||
showToast("Error", error.message, "error"); | ||
setUser(null); | ||
} | ||
}; | ||
|
||
getUser(); | ||
}, [postedBy, showToast]); | ||
|
||
if (!user) return null; | ||
return ( | ||
<Link to={`/${user.username}/post/${post._id}`}> | ||
<Flex gap={3} mb={4} py={5}> | ||
<Flex flexDirection={"column"} alignItems={"center"}> | ||
<Avatar | ||
size='md' | ||
name={user.name} | ||
src={user?.profilePic} | ||
onClick={(e) => { | ||
e.preventDefault(); | ||
navigate(`/${user.username}`); | ||
}} | ||
/> | ||
<Box w='1px' h={"full"} bg='gray.light' my={2}></Box> | ||
<Box position={"relative"} w={"full"}> | ||
{post.replies.length === 0 && <Text textAlign={"center"}>🥱</Text>} | ||
{post.replies[0] && ( | ||
<Avatar | ||
size='xs' | ||
name='John doe' | ||
src={post.replies[0].userProfilePic} | ||
position={"absolute"} | ||
top={"0px"} | ||
left='15px' | ||
padding={"2px"} | ||
/> | ||
)} | ||
|
||
{post.replies[1] && ( | ||
<Avatar | ||
size='xs' | ||
name='John doe' | ||
src={post.replies[1].userProfilePic} | ||
position={"absolute"} | ||
bottom={"0px"} | ||
right='-5px' | ||
padding={"2px"} | ||
/> | ||
)} | ||
|
||
{post.replies[2] && ( | ||
<Avatar | ||
size='xs' | ||
name='John doe' | ||
src={post.replies[2].userProfilePic} | ||
position={"absolute"} | ||
bottom={"0px"} | ||
left='4px' | ||
padding={"2px"} | ||
/> | ||
)} | ||
</Box> | ||
</Flex> | ||
<Flex flex={1} flexDirection={"column"} gap={2}> | ||
<Flex justifyContent={"space-between"} w={"full"}> | ||
<Flex w={"full"} alignItems={"center"}> | ||
<Text | ||
fontSize={"sm"} | ||
fontWeight={"bold"} | ||
onClick={(e) => { | ||
e.preventDefault(); | ||
navigate(`/${user.username}`); | ||
}} | ||
> | ||
{user?.username} | ||
</Text> | ||
<Image src='/verified.png' w={4} h={4} ml={1} /> | ||
</Flex> | ||
<Flex gap={4} alignItems={"center"}> | ||
<Text fontSize={"xs"} width={36} textAlign={"right"} color={"gray.light"}> | ||
{formatDistanceToNow(new Date(post.createdAt))} ago | ||
</Text> | ||
</Flex> | ||
</Flex> | ||
|
||
<Text fontSize={"sm"}>{post.text}</Text> | ||
{post.img && ( | ||
<Box borderRadius={6} overflow={"hidden"} border={"1px solid"} borderColor={"gray.light"}> | ||
<Image src={post.img} w={"full"} /> | ||
</Box> | ||
)} | ||
|
||
<Flex gap={3} my={1}> | ||
<Actions liked={liked} setLiked={setLiked} /> | ||
</Flex> | ||
|
||
<Flex gap={2} alignItems={"center"}> | ||
<Text color={"gray.light"} fontSize='sm'> | ||
{post.replies.length} replies | ||
</Text> | ||
<Box w={0.5} h={0.5} borderRadius={"full"} bg={"gray.light"}></Box> | ||
<Text color={"gray.light"} fontSize='sm'> | ||
{post.likes.length} likes | ||
</Text> | ||
</Flex> | ||
</Flex> | ||
</Flex> | ||
</Link> | ||
); | ||
}; | ||
|
||
export default Post; |
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