Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task 5 #417

Open
wants to merge 2 commits into
base: lecture-5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 51 additions & 13 deletions src/app/article/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ import { useParams } from 'react-router-dom';
import useStore from '../../hooks/use-store';
import useTranslate from '../../hooks/use-translate';
import useInit from '../../hooks/use-init';
import useSelector from '../../hooks/use-selector';
import PageLayout from '../../components/page-layout';
import Head from '../../components/head';
import Navigation from '../../containers/navigation';
import Spinner from '../../components/spinner';
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 { useDispatch, useSelector as useStoreRedux} from 'react-redux';
import shallowequal from 'shallowequal';
import articleActions from '../../store-redux/article/actions';
import commentsActions from '../../store-redux/comments/actions';
import Comments from '../../components/comments';
import listToTree from '../../utils/list-to-tree';
import treeToList from '../../utils/tree-to-list';

function Article() {
const store = useStore();
Expand All @@ -25,32 +30,65 @@ function Article() {
useInit(() => {
//store.actions.article.load(params.id);
dispatch(articleActions.load(params.id));
dispatch(commentsActions.load(params.id));
}, [params.id]);

const selectRedux = useStoreRedux(
state => ({
article: state.article.data,
waiting: state.article.waiting,
comments: state.comments.data,
waitingComments: state.comments.waiting,
}), shallowequal);

const select = useSelector(
state => ({
article: state.article.data,
waiting: state.article.waiting,
}),
shallowequal,
); // Нужно указать функцию для сравнения свойства объекта, так как хуком вернули объект
exists: state.session.exists,
user: state.session.user
}))

const { t } = useTranslate();

const callbacks = {
// Добавление в корзину
addToBasket: useCallback(_id => store.actions.basket.addToBasket(_id), [store]),
};
addComment: useCallback((text, id) => {
dispatch(commentsActions.add(text, id || params.id, id ? 'comment' : 'article'));
}, [dispatch, params.id])
}

const mappedComments = useMemo(() => {
return treeToList(
listToTree([
{
_id: params.id,
parent: null,
},
...(selectRedux.comments.items || []),
]),
(item, level) => ({ ...item, level: Math.max(0, level - 1) })
).slice(1);
}, [selectRedux.comments.items]);

return (
<PageLayout>
<TopHead />
<Head title={select.article.title}>
<LocaleSelect />
<TopHead/>
<Head title={selectRedux.article.title}>
<LocaleSelect/>
</Head>
<Navigation />
<Spinner active={select.waiting}>
<ArticleCard article={select.article} onAdd={callbacks.addToBasket} t={t} />
<Navigation/>
<Spinner active={selectRedux.waiting}>
<ArticleCard article={selectRedux.article} onAdd={callbacks.addToBasket} t={t}/>
</Spinner>
<Spinner active={selectRedux.waitingComments}>
<Comments
comments={mappedComments}
onSubmit={callbacks.addComment}
isUserAuth={select.exists}
currentUser={select.user}
pathToLogin={'/login'}
t={t}
/>
</Spinner>
</PageLayout>
);
Expand Down
42 changes: 42 additions & 0 deletions src/components/comment-input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { cn as bem } from "@bem-react/classname";
import { memo, useState } from "react";
import "./style.css";

const CommentInput = ({ onSubmit, onCancel, isModeReply = false }) => {
const cn = bem("CommentInput");
const [text, setText] = useState("");

const handleSubmit = () => {
if (text.trim().length === 0) {
setText("");
return;
}

onSubmit(text.trim());
setText("");
};

return (
<section className={cn()}>
<h3 className={cn("title")}>
{isModeReply ? "Новый ответ" : "Новый комментарий"}
</h3>
<textarea
className={cn("input")}
value={text}
onChange={(e) => setText(e.target.value)}
/>
<button className={cn("submit")} onClick={handleSubmit}>
Отправить
</button>

{isModeReply && (
<button className={cn("cancel")} onClick={onCancel}>
Отмена
</button>
)}
</section>
);
};

export default memo(CommentInput);
22 changes: 22 additions & 0 deletions src/components/comment-input/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.CommentInput-title {
margin: 0;
margin-bottom: 10px;
font-family: Arial;
font-size: 12px;
font-weight: 700;
line-height: 13.8px;
color: #000000;
}

.CommentInput-input {
margin-bottom: 10px;
border: 1px solid #666666;
background-color: #FFFFFF;
width: 100%;
min-height: 76px;
box-shadow: 0px 1px 4px 0px #6666661A inset;
}

.CommentInput-submit {
margin-right: 10px;
}
34 changes: 34 additions & 0 deletions src/components/comment-message/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { cn as bem } from "@bem-react/classname";
import React, { memo } from "react";
import { Link, useLocation } from "react-router-dom";
import "./style.css";

const CommentMessage = ({ onCancel, pathToLogin, isModeReply = false }) => {
const cn = bem("CommentMessage");
const location = useLocation();

return isModeReply ? (
<div>
<Link to={pathToLogin} state={{ back: location.pathname }} className={cn("enter")}>
Войдите
</Link>
, чтобы иметь возможность комментировать. {" "}
<button
type="button"
className={cn("cancel")}
onClick={onCancel}
>
Отмена
</button>
</div>
) : (
<div>
<Link to={pathToLogin} state={{ back: location.pathname }} className={cn("enter")}>
Войдите
</Link>
, чтобы иметь возможность комментировать
</div>
);
};

export default memo(CommentMessage);
21 changes: 21 additions & 0 deletions src/components/comment-message/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.CommentMessage-enter {
color: #0087E9;
font-family: Arial;
font-size: 16px;
font-weight: 400;
line-height: 18.4px;
text-decoration: underline;
}

.CommentMessage-cancel {
padding: 0;
color: #666666;
font-family: Arial;
font-size: 16px;
font-weight: 400;
line-height: 18.4px;
text-decoration-line: underline;
cursor: pointer;
border: none;
background: none;
}
34 changes: 34 additions & 0 deletions src/components/comment/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { cn as bem } from "@bem-react/classname";
import { memo } from "react";
import "./style.css";

const Comment = ({
authorName,
text,
dateCreate,
onReply,
styles,
isAuthorByCurrentUser,
}) => {
const cn = bem("Comment");

return (
<div className={cn()} style={styles}>
<div>
<span
className={cn("name", { "current-name": isAuthorByCurrentUser })}
>
{authorName}
</span>
<span className={cn("date")}>{(dateCreate)}</span>
</div>

<p className={cn("text")}>{text}</p>
<button className={cn("button")} onClick={onReply}>
Ответить
</button>
</div>
);
};

export default memo(Comment);
54 changes: 54 additions & 0 deletions src/components/comment/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
.Comment {
display: flex;
flex-direction: column;
gap: 10px;
}

.Comment-name {
font-family: Arial;
font-size: 12px;
font-weight: 700;
line-height: 13.8px;
color: #000000;
}

.Comment-name_current-name {
font-family: Arial;
font-size: 12px;
font-weight: 700;
line-height: 13.8px;
color: #666666;
}

.Comment-date {
font-family: Arial;
font-size: 12px;
font-weight: 400;
line-height: 13.8px;
color: #666666;
margin-left: 10px;
}

.Comment-text {
margin: 0;
font-family: Arial;
font-size: 14px;
font-weight: 400;
line-height: 16.1px;
color: #000000;
overflow-wrap: break-word;
hyphens: auto;
}

.Comment-button {
align-self: self-start;
padding: 0;
font-family: Arial;
font-size: 12px;
font-weight: 400;
line-height: 13.8px;
color: #0087E9;
cursor: pointer;
border: none;
background: none;
}
Loading
Loading