Post.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import {useEffect, useState} from "react";
  2. import picture from "../assets/img/brokenPicture.png";
  3. import Carousel from "react-material-ui-carousel";
  4. import AutoAwesomeMotionIcon from "@mui/icons-material/AutoAwesomeMotion";
  5. import ModalWindow from "./ModalWindow";
  6. import AvaLoginBlock from "./AvaLoginBlock";
  7. import CommentsBlock from "./CommentsBlock";
  8. import styled from "styled-components";
  9. import LikeBlock from "./LikeBlock";
  10. import {gqlOnePost} from "../shared/services&utilits/gqlRequest";
  11. const PostWrapper = styled.div`
  12. position: relative;
  13. display: flex;
  14. min-width: 300px;
  15. max-width: 300px;
  16. margin: 15px;
  17. box-shadow: rgba(0, 0, 0, 0.25) 0px 0.0625em 0.0625em, rgba(0, 0, 0, 0.25) 0px 0.125em 0.5em, rgba(255, 255, 255, 0.1) 0px 0px 0px 1px inset;
  18. `
  19. const LogoManyImg = styled.div`
  20. position: absolute;
  21. z-index: 2;
  22. right: 0;
  23. width: 25px;
  24. height: 35px;
  25. transform: scaleX(-1);
  26. `
  27. const PicsInModalWrapper = styled.div`
  28. width: 50%;
  29. max-height: 100%;
  30. margin: 30px;
  31. overflow: hidden;
  32. display: flex;
  33. align-items: center;
  34. justify-content: center;
  35. box-shadow: rgba(0, 0, 0, 0.25) 0px 0.0625em 0.0625em, rgba(0, 0, 0, 0.25) 0px 0.125em 0.5em, rgba(255, 255, 255, 0.1) 0px 0px 0px 1px inset;
  36. `
  37. const AvaLoginWrap = styled.div`
  38. display: flex;
  39. margin-top: 30px;
  40. width: 80%;
  41. `
  42. const DescriptionPostWrapper = styled.div`
  43. height: fit-content;
  44. width: 100%;
  45. box-shadow: rgba(17, 17, 26, 0.1) 0px 1px 0px;
  46. `
  47. const InfoBlockInModalWrapper = styled.div`
  48. width: 50%;
  49. `
  50. const Title = styled.p`
  51. `
  52. const Text = styled.p`
  53. `
  54. function Post({postId}){
  55. const [isModal, setModal] = useState(false);
  56. const [postData, setPostData] = useState(null);
  57. useEffect(() => {
  58. if (!postData) {
  59. gqlOnePost(postId).then(res => res.json())
  60. .then(data =>setPostData(data["data"].PostFindOne))
  61. }
  62. }, [])
  63. function updatePostData(){
  64. gqlOnePost(postId).then(res => res.json())
  65. .then(data =>setPostData(data["data"].PostFindOne))
  66. }
  67. function toggleModalWindow() {
  68. setModal(!isModal)
  69. }
  70. console.log(postData)
  71. const ImgBlock = ({images}) => {
  72. return (
  73. <>
  74. {!images && <img src={picture} alt={"pic"}/>}
  75. {images && images.length === 1 && <img src={images[0].url} alt={"pic"} style={{width: "100%"}}/>}
  76. {images && images.length > 1 && <div style={{width: "100%"}}>
  77. <Carousel autoPlay={false} navButtonsAlwaysVisible={true}>{
  78. images.map((item, i) => <img src={item.url} key={i} style={{width: "100%"}}
  79. alt={"picture"}/>)
  80. }
  81. </Carousel>
  82. </div>}
  83. </>
  84. )
  85. };
  86. // console.log(post)
  87. return (
  88. <>
  89. {postData &&
  90. <PostWrapper onClick={() => toggleModalWindow()}>
  91. <img src={postData.images ? postData.images[0].url : picture} alt="post" style={{width: "100%"}}/>
  92. {
  93. postData.images?.length > 1 && <LogoManyImg>
  94. <AutoAwesomeMotionIcon/>
  95. </LogoManyImg>
  96. }
  97. {isModal &&
  98. <ModalWindow closeModal={toggleModalWindow}>
  99. <PicsInModalWrapper>
  100. <ImgBlock images={postData.images}/>
  101. </PicsInModalWrapper>
  102. <InfoBlockInModalWrapper>
  103. <AvaLoginWrap>
  104. <AvaLoginBlock urlAva={postData.owner.avatar} name={postData.owner.login}/>
  105. </AvaLoginWrap>
  106. <DescriptionPostWrapper>
  107. <Title>Title:{postData.title}</Title>
  108. <Text>{postData.text}</Text>
  109. </DescriptionPostWrapper>
  110. <CommentsBlock post={postData}/>
  111. {postData.likes &&
  112. <LikeBlock likes={postData.likes} postId={postData._id} updatePostData={updatePostData}/>
  113. }
  114. </InfoBlockInModalWrapper>
  115. </ModalWindow>
  116. }
  117. </PostWrapper>
  118. }
  119. </>
  120. )
  121. };
  122. export default Post;