From 5be29b5f2c88a1b5cc2454442d214e65b89d86c7 Mon Sep 17 00:00:00 2001 From: DinaraDervel Date: Sat, 19 Apr 2025 00:52:37 -0600 Subject: [PATCH 1/3] done homework5 --- src/api/index.js | 3 ++ src/app/article/index.js | 12 +++++- src/components/comment-form/index.js | 56 ++++++++++++++++++++++++++ src/components/comment-form/style.css | 31 ++++++++++++++ src/components/comment-item/index.js | 53 ++++++++++++++++++++++++ src/components/comment-item/style.css | 54 +++++++++++++++++++++++++ src/components/comments-tree/index.js | 53 ++++++++++++++++++++++++ src/components/comments-tree/style.css | 32 +++++++++++++++ src/config.js | 6 +++ src/hooks/use-translate.js | 27 +++++++++++-- src/i18n/index.js | 35 ++++++++++++++++ src/index.js | 8 ++-- src/services.js | 12 ++++++ src/store-redux/comments/actions.js | 51 +++++++++++++++++++++++ src/store-redux/comments/reducer.js | 34 ++++++++++++++++ src/store-redux/exports.js | 1 + src/utils/format-date.js | 21 ++++++++++ 17 files changed, 479 insertions(+), 10 deletions(-) create mode 100644 src/components/comment-form/index.js create mode 100644 src/components/comment-form/style.css create mode 100644 src/components/comment-item/index.js create mode 100644 src/components/comment-item/style.css create mode 100644 src/components/comments-tree/index.js create mode 100644 src/components/comments-tree/style.css create mode 100644 src/i18n/index.js create mode 100644 src/store-redux/comments/actions.js create mode 100644 src/store-redux/comments/reducer.js create mode 100644 src/utils/format-date.js diff --git a/src/api/index.js b/src/api/index.js index a5f073295..63e7b2708 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -9,6 +9,7 @@ class APIService { this.defaultHeaders = { 'Content-Type': 'application/json', }; + this._lastRequests = []; } /** @@ -21,6 +22,7 @@ class APIService { */ async request({ url, method = 'GET', headers = {}, ...options }) { if (!url.match(/^(http|\/\/)/)) url = this.config.baseUrl + url; + const res = await fetch(url, { method, headers: { ...this.defaultHeaders, ...headers }, @@ -41,6 +43,7 @@ class APIService { delete this.defaultHeaders[name]; } } + } export default APIService; diff --git a/src/app/article/index.js b/src/app/article/index.js index 54f037b64..dac37348f 100644 --- a/src/app/article/index.js +++ b/src/app/article/index.js @@ -13,25 +13,34 @@ import TopHead from '../../containers/top-head'; import { useDispatch, useSelector } from 'react-redux'; import shallowequal from 'shallowequal'; import articleActions from '../../store-redux/article/actions'; +import commentsActions from '../../store-redux/comments/actions'; import HeadLayout from '../../components/head-layout'; +import CommentList from '../../components/comments-tree'; +import useSelectorStore from '../../hooks/use-selector'; function Article() { const store = useStore(); + const selectStore = useSelectorStore(state => ({ + user: state.session.user, + exists: state.session.exists, + })); const dispatch = useDispatch(); - // Параметры из пути /articles/:id + // Параметры из пути /articles/:id const params = useParams(); useInit(() => { //store.actions.article.load(params.id); dispatch(articleActions.load(params.id)); + dispatch(commentsActions.load(params.id)); }, [params.id]); const select = useSelector( state => ({ article: state.article.data, waiting: state.article.waiting, + comments: state.comments.data, }), shallowequal, ); // Нужно указать функцию для сравнения свойства объекта, так как хуком вернули объект @@ -55,6 +64,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..5ab7422e5 --- /dev/null +++ b/src/components/comment-form/index.js @@ -0,0 +1,56 @@ +import { useState } from 'react'; +import PropTypes from 'prop-types'; +import { useDispatch, useSelector } from 'react-redux'; +import commentsActions from '../../store-redux/comments/actions'; +import Button from '../button'; +import { cn as bem } from '@bem-react/classname'; +import './style.css'; + +function CommentForm({ parentId = null, parentName = null, onCancel = () => { } }) { + const dispatch = useDispatch(); + const [text, setText] = useState(''); + const productId = useSelector(state => state.article.data?._id); + + const handleSubmit = e => { + e.preventDefault(); + if (!text.trim()) return; + + dispatch(commentsActions.add({ + text, + parent: { + _id: parentId || productId, + _type: parentId ? 'comment' : 'article', + }, + })); + + setText(''); + if (onCancel) onCancel(); // закрыть форму ответа + }; + + const cn = bem('CommentForm'); + return ( +
+
Новый {parentId ? 'ответ' : 'комментарий'}
+