diff --git a/src/api/index.js b/src/api/index.js index a5f073295..73b092b1e 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -19,11 +19,16 @@ class APIService { * @param options * @returns {Promise<{}>} */ + async request({ url, method = 'GET', headers = {}, ...options }) { if (!url.match(/^(http|\/\/)/)) url = this.config.baseUrl + url; + + // Получаем текущий язык непосредственно перед запросом + const lang = this.services.I18n.getLang(); + const res = await fetch(url, { method, - headers: { ...this.defaultHeaders, ...headers }, + headers: { ...this.defaultHeaders, ...headers, 'X-Lang': lang }, ...options, }); return { data: await res.json(), status: res.status, headers: res.headers }; @@ -34,6 +39,7 @@ class APIService { * @param name {String} Название заголовка * @param value {String|null} Значение заголовка */ + setHeader(name, value = null) { if (value) { this.defaultHeaders[name] = value; diff --git a/src/app/article/index.js b/src/app/article/index.js index 54f037b64..b84037f07 100644 --- a/src/app/article/index.js +++ b/src/app/article/index.js @@ -1,4 +1,4 @@ -import { memo, useCallback } from 'react'; +import { memo, useCallback, useMemo } from 'react'; import { useParams } from 'react-router-dom'; import useStore from '../../hooks/use-store'; import useTranslate from '../../hooks/use-translate'; @@ -11,12 +11,17 @@ import ArticleCard from '../../components/article-card'; import LocaleSelect from '../../containers/locale-select'; import TopHead from '../../containers/top-head'; import { useDispatch, useSelector } from 'react-redux'; +import useServices from '../../hooks/use-services'; import shallowequal from 'shallowequal'; import articleActions from '../../store-redux/article/actions'; +import commentActions from '../../store-redux/comments/actions'; import HeadLayout from '../../components/head-layout'; +import Comments from '../../containers/comments'; function Article() { const store = useStore(); + const { I18n } = useServices(); + const currentLang = I18n.getLang(); const dispatch = useDispatch(); // Параметры из пути /articles/:id @@ -26,17 +31,20 @@ function Article() { useInit(() => { //store.actions.article.load(params.id); dispatch(articleActions.load(params.id)); - }, [params.id]); + dispatch(commentActions.load(params.id)) + }, [params.id, currentLang]); const select = useSelector( state => ({ article: state.article.data, waiting: state.article.waiting, + comments: state.comments.comments, + commentsWaiting: state.comments.waiting, }), shallowequal, ); // Нужно указать функцию для сравнения свойства объекта, так как хуком вернули объект - const { t } = useTranslate(); + const t = useTranslate(); const callbacks = { // Добавление в корзину @@ -56,6 +64,10 @@ function Article() { + + + + ); diff --git a/src/app/basket/index.js b/src/app/basket/index.js index 52a7badfd..1a45cfedf 100644 --- a/src/app/basket/index.js +++ b/src/app/basket/index.js @@ -2,7 +2,6 @@ import { memo, useCallback } from 'react'; import { useDispatch, useStore as useStoreRedux } from 'react-redux'; import useStore from '../../hooks/use-store'; import useSelector from '../../hooks/use-selector'; -import useInit from '../../hooks/use-init'; import useTranslate from '../../hooks/use-translate'; import ItemBasket from '../../components/item-basket'; import List from '../../components/list'; @@ -30,7 +29,7 @@ function Basket() { }, [store]), }; - const { t } = useTranslate(); + const t = useTranslate(); const renders = { itemBasket: useCallback( diff --git a/src/app/login/index.js b/src/app/login/index.js index 32b86daa4..e84071bae 100644 --- a/src/app/login/index.js +++ b/src/app/login/index.js @@ -16,7 +16,7 @@ import HeadLayout from '../../components/head-layout'; import Form from '../../components/form'; function Login() { - const { t } = useTranslate(); + const t = useTranslate(); const location = useLocation(); const navigate = useNavigate(); const store = useStore(); diff --git a/src/app/main/index.js b/src/app/main/index.js index d9fb1c6c2..0871567d3 100644 --- a/src/app/main/index.js +++ b/src/app/main/index.js @@ -1,4 +1,5 @@ import { memo } from 'react'; +import useServices from '../../hooks/use-services'; import useStore from '../../hooks/use-store'; import useTranslate from '../../hooks/use-translate'; import useInit from '../../hooks/use-init'; @@ -13,16 +14,18 @@ import HeadLayout from '../../components/head-layout'; function Main() { const store = useStore(); + const { I18n } = useServices(); + const currentLang = I18n.getLang(); useInit( async () => { await Promise.all([store.actions.catalog.initParams(), store.actions.categories.load()]); }, - [], + [currentLang], true, ); - const { t } = useTranslate(); + const t = useTranslate(); return ( <> diff --git a/src/app/profile/index.js b/src/app/profile/index.js index 166d127fb..85b41ff8e 100644 --- a/src/app/profile/index.js +++ b/src/app/profile/index.js @@ -24,7 +24,7 @@ function Profile() { waiting: state.profile.waiting, })); - const { t } = useTranslate(); + const t = useTranslate(); return ( <> @@ -37,7 +37,7 @@ function Profile() { - + diff --git a/src/components/article-card/index.js b/src/components/article-card/index.js index d636d9f88..1d2872a72 100644 --- a/src/components/article-card/index.js +++ b/src/components/article-card/index.js @@ -6,29 +6,29 @@ import Button from '../button'; import './style.css'; function ArticleCard(props) { - const { article, onAdd = () => {}, t = text => text } = props; + const { article, onAdd = () => { }, t = text => text } = props; const cn = bem('ArticleCard'); return (
{article.description}
-
Страна производитель:
+
{t('article.country')}
{article.madeIn?.title} ({article.madeIn?.code})
-
Категория:
+
{t('article.category')}
{article.category?.title}
-
Год выпуска:
+
{t('article.year')}
{article.edition}
-
Цена:
+
{t('article.price')}
{numberFormat(article.price)} ₽
+
); } @@ -18,6 +18,7 @@ Button.propTypes = { title: PropTypes.string, style: PropTypes.oneOf(['text', 'primary', 'delete', 'outline']), type: PropTypes.oneOf(['button', 'submit']), + className: PropTypes.string, }; export default memo(Button); diff --git a/src/components/comment-form/index.js b/src/components/comment-form/index.js new file mode 100644 index 000000000..a6f958ac3 --- /dev/null +++ b/src/components/comment-form/index.js @@ -0,0 +1,31 @@ +import React, { useState, forwardRef } from 'react'; +import './style.css'; +import PropTypes from 'prop-types'; +import Button from '../button'; + +const CommentForm = forwardRef(({ onSubmit, level = 0, t }, ref) => { + const [commentText, setCommentText] = useState(''); + + const marginLeft = level * 40; + + const handleSubmit = e => { + e.preventDefault(); + onSubmit(commentText) + } + + return ( +
+

{t('comments.formTitle')}

+