comments.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import { url } from "../../App";
  2. import { useHistory, useParams } from "react-router-dom";
  3. import { createContext, useState } from "react";
  4. import './style.scss'
  5. import {
  6. Card,
  7. CardHeader,
  8. CardContent,
  9. CardActions,
  10. Avatar,
  11. IconButton,
  12. Typography,
  13. Box,
  14. Divider,
  15. Popover,
  16. Tooltip
  17. } from '@mui/material'
  18. import {
  19. FavoriteBorderRounded,
  20. SendRounded,
  21. ChatBubbleOutline,
  22. TurnedInNot,
  23. } from '@mui/icons-material/'
  24. import PopupState, { bindTrigger, bindPopover } from 'material-ui-popup-state';
  25. import Grid from '@mui/material/Unstable_Grid2';
  26. import CCommentsFeed from "./comments_feed";
  27. import CommentField from "./comment_field";
  28. // контекст для управления состоянием данных для отправки комментов
  29. export const CommentContext = createContext()
  30. export function CardPost({ postData }) {
  31. // console.log('postData: ', postData)
  32. const history = useHistory()
  33. const { postId } = useParams()
  34. // отслеживаем состояние поля ввода комментария для поста
  35. const [comment, setComment] = useState({ text: '', post: { _id: postId } })
  36. // дата поста
  37. const dateofPost = new Date(+postData.createdAt)
  38. const months = ['января', 'февраля', 'марта', 'апреля', 'мая', 'июня', 'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря']
  39. const dateofPostParse = `${dateofPost.getDate() < 10 ? '0' + dateofPost.getDate() : dateofPost.getDate()}
  40. ${months[dateofPost.getMonth()]}
  41. ${dateofPost.getFullYear()}
  42. ${dateofPost.getHours()}:${dateofPost.getMinutes() < 10 ? '0' + dateofPost.getMinutes() : dateofPost.getMinutes()}`
  43. // Переход на профиль пользователя
  44. function toAccount() {
  45. history.push(`/user/${postData?.owner?._id}`)
  46. }
  47. // перевод фокуса на строку добавления комментария при клике на иконку комментария
  48. function addCommentFocus() {
  49. document.getElementById('addCommentField').focus()
  50. }
  51. // отслеживание состояния текста для ToolTips
  52. // const [toolTipText, setToolTipText] = useState('Нажмите для копирования')
  53. const [toolTipText, setToolTipText] = useState(true)
  54. let text = toolTipText ? 'Нажмите для копирования' : 'Ссылка скопирована в буфер обмена'
  55. // функция копирования ссылки на пост
  56. function copyShareLink(text) {
  57. // получаем урл текущей страницы
  58. const postUrl = window.location.href
  59. navigator.clipboard.writeText(postUrl)
  60. // setToolTipText('Ссылка скопирована в буфер обмена')
  61. // setTimeout(() => setToolTipText('Нажмите для копирования'), 1500)
  62. setToolTipText(!toolTipText)
  63. setTimeout(() => setToolTipText(toolTipText), 1500)
  64. }
  65. return (
  66. <CommentContext.Provider value={[comment, setComment]}>
  67. <Card
  68. sx={{
  69. display: 'flex',
  70. flexDirection: 'column',
  71. minHeight: '80vh',
  72. borderRadius: 0
  73. }}
  74. >
  75. <CardHeader
  76. avatar={
  77. <Avatar
  78. alt={postData?.owner?.login}
  79. src={(url + postData?.owner?.avatar?.url)}
  80. sx={{
  81. width: 50,
  82. height: 50
  83. }}
  84. />
  85. }
  86. title={
  87. <Typography
  88. sx={{
  89. cursor: 'pointer'
  90. }}
  91. onClick={toAccount}
  92. >
  93. {postData?.owner?.login}
  94. </Typography>
  95. }
  96. subheader={dateofPostParse}
  97. />
  98. <Divider />
  99. <CardContent
  100. className='post-comments'
  101. sx={{
  102. flex: '0 0 450px',
  103. overflowY: 'auto',
  104. }}
  105. >
  106. <Grid
  107. container
  108. spacing={2}
  109. sx={{
  110. marginBottom: 3
  111. }}
  112. >
  113. <Grid
  114. xs={2}
  115. sx={{
  116. flex: '0 0 45px'
  117. }}
  118. >
  119. <Avatar
  120. alt={postData?.owner?.login}
  121. src={(url + postData?.owner?.avatar?.url)}
  122. sx={{
  123. width: 40,
  124. height: 40
  125. }}
  126. />
  127. </Grid>
  128. <Grid
  129. xs={10}
  130. >
  131. <Typography
  132. variant="subtitle2"
  133. color="text.secondary"
  134. sx={{
  135. cursor: 'pointer',
  136. width: 'fit-content'
  137. }}
  138. onClick={toAccount}
  139. >
  140. {postData?.owner?.login}
  141. </Typography>
  142. <Typography
  143. variant="subtitle2"
  144. color="text.primary"
  145. >
  146. {postData?.title}
  147. </Typography>
  148. <Typography
  149. variant="body2"
  150. color="text.secondary"
  151. >
  152. {postData?.text}
  153. </Typography>
  154. </Grid>
  155. </Grid>
  156. <Grid>
  157. <CCommentsFeed />
  158. </Grid>
  159. </CardContent>
  160. <Divider />
  161. <CardActions disableSpacing>
  162. <Box
  163. sx={{
  164. flexGrow: 1
  165. }}>
  166. <Grid
  167. xs={12}
  168. container
  169. justifyContent="space-between"
  170. alignItems="center"
  171. sx={{
  172. fontSize: '12px'
  173. }}
  174. >
  175. <Grid container>
  176. <Grid>
  177. <IconButton
  178. aria-label="add to favorites"
  179. onClick={() => (console.log('click like main post'))}
  180. >
  181. <FavoriteBorderRounded />
  182. </IconButton>
  183. </Grid>
  184. <Grid>
  185. <IconButton
  186. onClick={addCommentFocus}
  187. >
  188. <ChatBubbleOutline />
  189. </IconButton>
  190. </Grid>
  191. <Grid>
  192. <PopupState variant="popover" popupId="demo-popup-popover">
  193. {(popupState) => (
  194. <div>
  195. <IconButton
  196. aria-label="share"
  197. {...bindTrigger(popupState)}
  198. >
  199. <SendRounded
  200. style={{ transform: 'translate(3px, -4px) rotate(-30deg)' }}
  201. />
  202. </IconButton>
  203. <Popover
  204. {...bindPopover(popupState)}
  205. anchorOrigin={{
  206. vertical: 'center',
  207. horizontal: 'right',
  208. }}
  209. transformOrigin={{
  210. vertical: 'center',
  211. horizontal: 'left',
  212. }}
  213. >
  214. {/* <Tooltip title={toolTipText} placement='bottom-start'> */}
  215. <Tooltip title={text} placement='bottom-start'>
  216. <Typography
  217. sx={{ p: 2 }}
  218. onClick={copyShareLink}
  219. >
  220. {window.location.href}
  221. </Typography>
  222. </Tooltip>
  223. </Popover>
  224. </div>
  225. )}
  226. </PopupState>
  227. </Grid>
  228. </Grid>
  229. <Grid>
  230. <IconButton>
  231. <TurnedInNot />
  232. </IconButton>
  233. </Grid>
  234. </Grid>
  235. <Grid container>
  236. <Typography
  237. variant="subtitle1"
  238. color="text.secondary"
  239. sx={{
  240. padding: 1
  241. }}
  242. >
  243. Нравится: {postData.likesCount ? postData.likesCount : '0'}
  244. </Typography>
  245. </Grid>
  246. </Box>
  247. </CardActions>
  248. <Divider />
  249. <CardActions>
  250. <CommentField />
  251. </CardActions>
  252. </Card>
  253. </CommentContext.Provider >
  254. )
  255. }