From e31f0c5b41f130342d10b868b3531508efb097ba Mon Sep 17 00:00:00 2001 From: VNagorniy Date: Fri, 18 Apr 2025 16:59:13 +0300 Subject: [PATCH 1/2] task1/feat: add comments-component in arcticle component --- src/app/article/index.js | 2 + src/components/comment-form/index.js | 57 +++++++++++++++ src/components/comment-form/style.css | 27 ++++++++ src/components/comment/index.js | 52 ++++++++++++++ src/components/comment/style.css | 37 ++++++++++ src/containers/comments/index.js | 99 +++++++++++++++++++++++++++ src/containers/comments/style.css | 28 ++++++++ src/store-redux/comments/actions.js | 55 +++++++++++++++ src/store-redux/comments/reducer.js | 39 +++++++++++ src/store-redux/exports.js | 1 + 10 files changed, 397 insertions(+) create mode 100644 src/components/comment-form/index.js create mode 100644 src/components/comment-form/style.css create mode 100644 src/components/comment/index.js create mode 100644 src/components/comment/style.css create mode 100644 src/containers/comments/index.js create mode 100644 src/containers/comments/style.css create mode 100644 src/store-redux/comments/actions.js create mode 100644 src/store-redux/comments/reducer.js diff --git a/src/app/article/index.js b/src/app/article/index.js index 54f037b64..f6ed6f373 100644 --- a/src/app/article/index.js +++ b/src/app/article/index.js @@ -14,6 +14,7 @@ import { useDispatch, useSelector } from 'react-redux'; import shallowequal from 'shallowequal'; import articleActions from '../../store-redux/article/actions'; import HeadLayout from '../../components/head-layout'; +import Comments from '../../containers/comments'; function Article() { const store = useStore(); @@ -55,6 +56,7 @@ function Article() { + diff --git a/src/components/comment-form/index.js b/src/components/comment-form/index.js new file mode 100644 index 000000000..49e362780 --- /dev/null +++ b/src/components/comment-form/index.js @@ -0,0 +1,57 @@ +import { memo, useEffect, useState } from 'react'; +import PropTypes from 'prop-types'; +import { cn as bem } from '@bem-react/classname'; +import './style.css'; +import Button from '../button'; +import { useDispatch } from 'react-redux'; +import commentsActions from '../../store-redux/comments/actions'; + +function CommentForm({ parentId, parentType = 'article', replyToUserId }) { + const [text, setText] = useState(''); + const dispatch = useDispatch(); + const cn = bem('CommentForm'); + + // При монтировании компонента добавляем префикс с адресатом + useEffect(() => { + if (parentType === 'comment' && replyToUserId) { + setText(`Мой ответ для User №${replyToUserId}\n`); + } + }, [parentType, replyToUserId]); + + const onSubmit = e => { + e.preventDefault(); + if (text.trim()) { + dispatch(commentsActions.add(text, parentId, parentType)); + setText(''); + } + }; + + const onCancel = () => { + dispatch(commentsActions.setReply(null)); + }; + + return ( +
+

{parentType === 'article' ? 'Новый комментарий' : 'Новый ответ'}

+