anagreg

Untitled

Nov 13th, 2023
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. import React from 'react'
  2. import {
  3. Box,
  4. Button,
  5. Text,
  6. Avatar,
  7. Card,
  8. CardBody,
  9. Heading,
  10. Flex,
  11. HStack,
  12. Spacer,
  13. Tooltip,
  14. AlertDialogOverlay,
  15. AlertDialogContent,
  16. AlertDialogHeader,
  17. AlertDialogCloseButton,
  18. AlertDialogBody,
  19. AlertDialogFooter,
  20. AlertDialog,
  21. useDisclosure } from '@chakra-ui/react'
  22. import { EditIcon, DeleteIcon } from '@chakra-ui/icons'
  23. import { useNavigate } from 'react-router-dom'
  24.  
  25. import { useContext,useEffect,useState } from 'react'
  26.  
  27.  
  28. import AppContext from '../../context/AppContext'
  29. import { deletePost } from '../../services/post.service'
  30.  
  31. import { getAllComments, getCommentsByAuthor } from '../../services/comment.service'
  32. import PropTypes from 'prop-types';
  33. import { getUserByHandle } from '../../services/users.service'
  34.  
  35. const DiscussionPost = ({ post }) => {
  36. const { user, userData } = useContext(AppContext)
  37. const navigate = useNavigate()
  38. const { isOpen, onOpen, onClose } = useDisclosure()
  39. const cancelRef = React.useRef()
  40. const[currentUserData, setCurrentUserData] = useState({})
  41.  
  42. const handleDelete = (e) => {
  43. e.preventDefault();
  44.  
  45. deletePost(post.id);
  46. }
  47.  
  48.  
  49. const handleEdit = () => {
  50. navigate("/edit-post", { state: { post }, replace: true })
  51. }
  52.  
  53. const goToPost = () => {
  54. navigate(`/post/${post.id}`, { state: { post }, replace: true })
  55. }
  56.  
  57.  
  58. const goToProfile = () => {
  59. if(post.author !== undefined || post.author !== null){
  60. navigate(`/profile/${post.author}`, { state: {author:post.author,authorPhoto:post.authorPhoto}, replace: true });
  61. }else{
  62. return;
  63. }
  64. };
  65.  
  66. useEffect(() => {
  67. console.log("a");
  68. getUserByHandle(post.author).then((snapshot) => {
  69. const user = snapshot.val();
  70. setCurrentUserData({ ...user });
  71. });
  72. }, [post.author]);
  73.  
  74. return (
  75.  
  76. <>
  77. <Card
  78. // backgroundColor='#DAEAB4'
  79. mt='5'
  80. _hover={{backgroundColor:"rgba(0,0,0,0.05)"}} >
  81. <CardBody >
  82. {/* <Flex justify='center'> */}
  83. <Flex justify='left'>
  84. <HStack spacing='24px'>
  85. <Tooltip label={post.author}>
  86.  
  87. <Box
  88. onClick={goToProfile}
  89. >
  90. <Avatar size='sm' src={currentUserData.photoURL} ></Avatar>
  91. </Box>
  92.  
  93. </Tooltip>
  94. <Spacer />
  95. <Heading size='md' onClick={goToPost} _hover={{textDecoration:"none", cursor:'pointer'}}>
  96. {post.title.length > 50 ? `${(post.title).substring(0, 50)}...` : post.title}</Heading>
  97. <Spacer />
  98. <Text
  99. style={{ position: "absolute", left: "67%" }}
  100. >{post.createdOn.toDateString()}</Text>
  101.  
  102. <Text style={{ position: "absolute", left: "80%" }}>{post.likedBy ? (Object.keys(post.likedBy).length) : 0}</Text>
  103. <Text style={{ position: "absolute", left: "85%" }}>{post.comments ? (Object.keys(post.comments).length) : 0}</Text>
  104. {(userData.handle==post.author && userData.accountType!==0 || userData.accountType==2)&&
  105. <Button
  106. style={{ position: "absolute", left: "87%" }}
  107. variant='ghost'
  108. leftIcon={<EditIcon/>}
  109. onClick={handleEdit}
  110. size='sm'>
  111. Edit
  112. </Button>}
  113. {(userData.handle==post.author && userData.accountType!==0 || userData.accountType==2)&&
  114. <Button
  115. style={{ position: "absolute", left: "92%" }}
  116. variant='ghost'
  117. leftIcon={<DeleteIcon />}
  118. onClick={onOpen}
  119. size='sm'>
  120. Delete
  121. </Button>}
  122. <AlertDialog
  123. motionPreset='slideInBottom'
  124. leastDestructiveRef={cancelRef}
  125. onClose={onClose}
  126. isOpen={isOpen}
  127. isCentered
  128. >
  129. <AlertDialogOverlay />
  130.  
  131. <AlertDialogContent>
  132. <AlertDialogHeader>Delete post?</AlertDialogHeader>
  133. <AlertDialogCloseButton />
  134. <AlertDialogBody>
  135. Are you sure you want to delete this post?
  136. </AlertDialogBody>
  137. <AlertDialogFooter>
  138. <Button ref={cancelRef} onClick={onClose}>
  139. No
  140. </Button>
  141. <Button
  142. onClick={handleDelete}
  143. backgroundColor='#BC4749'
  144. color='white'
  145. ml={3}>
  146. Yes
  147. </Button>
  148. </AlertDialogFooter>
  149. </AlertDialogContent>
  150. </AlertDialog>
  151. </HStack>
  152. </Flex>
  153. </CardBody>
  154. </Card>
  155. </>
  156. )
  157. }
  158.  
  159.  
  160.  
  161. // DiscussionPost.propTypes = {
  162. // post: PropTypes.shape({
  163. // id: PropTypes.string.isRequired,
  164. // title: PropTypes.string.isRequired,
  165. // author: PropTypes.string.isRequired,
  166. // createdOn: PropTypes.instanceOf(Date).isRequired,
  167. // likedBy: PropTypes.object.isRequired,
  168. // comments: PropTypes.object.isRequired,
  169. // }).isRequired,
  170. // };
  171.  
  172. export default DiscussionPost;
Advertisement
Add Comment
Please, Sign In to add comment