diff --git a/src/views/chicken-circle/comps/comment-list/index.less b/src/views/chicken-circle/comps/comment-list/index.less index 23f1a3b..c679a68 100644 --- a/src/views/chicken-circle/comps/comment-list/index.less +++ b/src/views/chicken-circle/comps/comment-list/index.less @@ -33,6 +33,7 @@ display: flex; align-items: flex-start; margin-bottom: 20px; + margin-top: 10px; .text-area-outer-box{ border: 1px solid lightgray; border-radius: 8px; @@ -46,13 +47,23 @@ } } .comment-list-wrapper{ - .comment-list-item{ + .comment-item-wrapper{ margin-top: 30px; - .ope-btn-group{ - gap: 16px; - color: gray; - .reply-btn{ + width: 100%; + .comment-detail-wrapper{ + flex: 1; + } + .comment-content{ + margin: 10px 0; + } + .comment-bottom-wrapper{ + color: rgb(211, 211, 211); + .bottom-btn{ + margin-left: 10px; cursor: pointer; + &:hover, &.active{ + color: #1677ff; + } } } } diff --git a/src/views/chicken-circle/comps/comment-list/index.tsx b/src/views/chicken-circle/comps/comment-list/index.tsx index d49d394..8012df9 100644 --- a/src/views/chicken-circle/comps/comment-list/index.tsx +++ b/src/views/chicken-circle/comps/comment-list/index.tsx @@ -1,26 +1,16 @@ -import { Button, Input } from 'antd' +import { Button, Input, Upload, Image } from 'antd' import { useState, useEffect, FC } from 'react' import { useSelector } from 'react-redux' import { commentSave, getCommentList } from '../../service' +import { CommentOutlined, FileImageOutlined, PlusOutlined, SmileOutlined } from '@ant-design/icons' import './index.less' -import { CommentOutlined, FileImageOutlined, SmileOutlined } from '@ant-design/icons' -const CommentList: FC = props => { +const CommentInput = ({ momentId, getList, replyType, targetId = '' }) => { + const [comment, setComment] = useState('') const { userInfo } = useSelector(store => store.userInfo) - const { momentId } = props - const [replyList, setReplyList] = useState([]) - const [comment, setComment] = useState('') - const getList = async () => { - const res = await getCommentList({ id: momentId }) - if (res.success && res.data) { - setReplyList(res.data) - } else { - setReplyList([]) - } - } - useEffect(() => { - getList() - }, []) + const userInfoStorage = localStorage.getItem('userInfo') + const { tokenValue = '' } = userInfoStorage ? JSON.parse(userInfoStorage) : {} + const [imgList, setImgList] = useState([]) const changeComment = e => { setComment(e.target.value) @@ -29,83 +19,232 @@ const CommentList: FC = props => { const saveComment = () => { const params = { momentId, - replyType: 2, + replyType, content: comment, - targetId: 12 + targetId + } + if (imgList.length) { + params.picUrlList = imgList.map(item => item.response.data) } commentSave(params).then(() => { + setComment('') + setImgList([]) getList() }) } + + const uploadButton = ( + + ) + const handleChange = ({ fileList }) => { + setImgList(fileList) + } return ( -
-
-
评论 {replyList.length}
-
- -
-
- +
+ +
+
+ + + {imgList.length >= 8 || imgList.length === 0 ? null : uploadButton} + +
+
+
+
+ +
+
+ +
+ + {/* 图片 */} +
+
+ {/* */} +
-
-
-
- -
-
- -
-
-
-
1/1000
- +
+
+ {comment.length}/1000
+
+
+ ) +} + +function formatDistanceToNow(date) { + if (!date) return + const delta = Math.abs(Date.now() - date) + + if (delta < 30 * 1000) { + return '刚刚' + } else if (delta < 5 * 60 * 1000) { + return Math.round(delta / 1000) + '秒前' + } else if (delta < 60 * 60 * 1000) { + return Math.round(delta / 60000) + '分钟前' + } else if (delta < 24 * 60 * 60 * 1000) { + return Math.round(delta / 3600000) + '小时前' + } else if (delta < 7 * 24 * 60 * 60 * 1000) { + return Math.round(delta / 86400000) + '天前' + } else { + return new Date(date).toLocaleDateString() + return '很久之前' + } +} + +const CommentItem = ({ + momentId, + id, + avatar, + userName, + content, + createdTime, + replyType, + getList, + children, + picUrlList +}) => { + const [active, setActive] = useState(false) + const toggleActive = () => { + setActive(!active) + } + return ( +
+
+ 头像 +
+
{userName}
+
{content}
+ {picUrlList?.length && ( + +
+ {picUrlList.map((t: string) => ( + + ))} +
+
+ )} +
+
{formatDistanceToNow(createdTime) || '12小时前'}
+
+ + {replyType === 1 ? '评论' : '回复'} +
+
+ {active && ( + + )} + {children?.length + ? children?.map(item => { + return + }) + : ''} +
+
+
+ ) +} + +function flattenNestedObjects(items) { + const result = [] + + function traverse(items) { + items.forEach(item => { + // 创建一个新对象来存储当前项的属性(除了 children) + const flatItem = {} + for (const key in item) { + if (key !== 'children') { + flatItem[key] = item[key] + } + } + // 将扁平化的对象添加到结果数组中 + result.push(flatItem) + + // 如果还有 children,则递归调用 traverse + if (item.children) { + traverse(item.children) + } + }) + } + + // 从顶层对象开始遍历 + traverse(items) + + return result +} + +const CommentList: FC = props => { + const { momentId, replyCount } = props + const [replyList, setReplyList] = useState([]) + const getList = async () => { + const res = await getCommentList({ id: momentId }) + if (res.success && res.data) { + const data = res.data.map(item => { + return { + ...item, + children: flattenNestedObjects(item.children || []) + } + }) + setReplyList(data) + } else { + setReplyList([]) + } + } + useEffect(() => { + getList() + }, []) + + return ( +
+
+
评论 {replyCount}
+
{replyList.map((item: Record) => { - return ( -
- -
-
{item.userName}
-
{item.content}
-
-
12小时前
-
- -  评论 -
-
- {item.children?.length && - item.children.map(child => { - return ( -
- -
-
{child.userName}
-
{child.content}
-
-
12小时前
-
- -  评论 -
-
-
-
- ) - })} -
-
- ) + return })}
diff --git a/src/views/chicken-circle/index.tsx b/src/views/chicken-circle/index.tsx index 3febe88..9a329a8 100644 --- a/src/views/chicken-circle/index.tsx +++ b/src/views/chicken-circle/index.tsx @@ -106,6 +106,8 @@ const Circle = () => { } const res = await saveMoment(params) if (res.success) { + setComment('') + setImgList([]) getMomentList() return message.success('发布成功') } @@ -154,6 +156,7 @@ const Circle = () => { className='top-text-area' onFocus={() => toggleFocus(false)} onBlur={() => toggleFocus(true)} + value={comment} /> { description={item.content} /> {item.picUrlList?.length && ( - +
{item.picUrlList.map((t: string) => ( @@ -237,7 +240,18 @@ const Circle = () => { )} - {currentReplyCommentId === item.id && } + {currentReplyCommentId === item.id && ( + + )} ) })}