1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-05-04 00:56:40 +07:00

refactored node comments to component

This commit is contained in:
Fedor Katurov 2021-11-06 21:14:50 +07:00
parent b44266437d
commit ab15a10d01
13 changed files with 208 additions and 111 deletions
src/components
boris/BorisComments
comment
Comment
CommentContent
node
NodeAuthorBlock
NodeBottomBlock
NodeComments
NodeCommentsBlock

View file

@ -5,21 +5,30 @@ import { NodeCommentForm } from '~/components/node/NodeCommentForm';
import { NodeNoComments } from '~/components/node/NodeNoComments';
import { NodeComments } from '~/components/node/NodeComments';
import { Footer } from '~/components/main/Footer';
import { Card } from '~/components/containers/Card';
import { useShallowSelect } from '~/utils/hooks/useShallowSelect';
import { selectAuthUser } from '~/redux/auth/selectors';
import { IComment, INode } from '~/redux/types';
import { IComment, IFile, INode } from '~/redux/types';
import { IUser } from '~/redux/auth/types';
interface IProps {
isLoadingComments: boolean;
commentCount: number;
node: INode;
user: IUser;
commentCount: number;
comments: IComment[];
isLoadingComments: boolean;
onShowImageModal: (images: IFile[], index: number) => void;
onLoadMoreComments: () => void;
onDeleteComment: (id: IComment['id'], isLocked: boolean) => void;
}
const BorisComments: FC<IProps> = ({ isLoadingComments, node, commentCount, comments }) => {
const user = useShallowSelect(selectAuthUser);
const BorisComments: FC<IProps> = ({
node,
user,
isLoadingComments,
commentCount,
comments,
onLoadMoreComments,
onDeleteComment,
onShowImageModal,
}) => {
return (
<>
<Group className={styles.grid}>
@ -28,7 +37,15 @@ const BorisComments: FC<IProps> = ({ isLoadingComments, node, commentCount, comm
{isLoadingComments ? (
<NodeNoComments is_loading count={7} />
) : (
<NodeComments comments={comments} count={commentCount} user={user} order="ASC" />
<NodeComments
comments={comments}
count={commentCount}
user={user}
order="ASC"
onDeleteComment={onDeleteComment}
onLoadMoreComments={onLoadMoreComments}
onShowImageModal={onShowImageModal}
/>
)}
</Group>

View file

@ -1,10 +1,9 @@
import React, { FC, HTMLAttributes, memo } from 'react';
import { CommentWrapper } from '~/components/containers/CommentWrapper';
import { IComment, ICommentGroup } from '~/redux/types';
import { IComment, ICommentGroup, IFile } from '~/redux/types';
import { CommentContent } from '~/components/comment/CommentContent';
import styles from './styles.module.scss';
import { CommendDeleted } from '../../node/CommendDeleted';
import * as MODAL_ACTIONS from '~/redux/modal/actions';
import classNames from 'classnames';
import { NEW_COMMENT_CLASSNAME } from '~/constants/comment';
@ -15,7 +14,7 @@ type IProps = HTMLAttributes<HTMLDivElement> & {
isSame?: boolean;
canEdit?: boolean;
onDelete: (id: IComment['id'], isLocked: boolean) => void;
modalShowPhotoswipe: typeof MODAL_ACTIONS.modalShowPhotoswipe;
onShowImageModal: (images: IFile[], index: number) => void;
};
const Comment: FC<IProps> = memo(
@ -27,7 +26,7 @@ const Comment: FC<IProps> = memo(
className,
canEdit,
onDelete,
modalShowPhotoswipe,
onShowImageModal,
...props
}) => {
return (
@ -53,7 +52,7 @@ const Comment: FC<IProps> = memo(
key={comment.id}
can_edit={!!canEdit}
onDelete={onDelete}
modalShowPhotoswipe={modalShowPhotoswipe}
onShowImageModal={onShowImageModal}
/>
);
})}

View file

@ -8,23 +8,22 @@ import { UPLOAD_TYPES } from '~/redux/uploads/constants';
import reduce from 'ramda/es/reduce';
import { AudioPlayer } from '~/components/media/AudioPlayer';
import classnames from 'classnames';
import classNames from 'classnames';
import { PRESETS } from '~/constants/urls';
import { COMMENT_BLOCK_RENDERERS } from '~/constants/comment';
import { CommentMenu } from '../CommentMenu';
import * as MODAL_ACTIONS from '~/redux/modal/actions';
import { CommentForm } from '~/components/comment/CommentForm';
import { useShallowSelect } from '~/utils/hooks/useShallowSelect';
import { selectNode } from '~/redux/node/selectors';
import classNames from 'classnames';
interface IProps {
comment: IComment;
can_edit: boolean;
onDelete: (id: IComment['id'], isLocked: boolean) => void;
modalShowPhotoswipe: typeof MODAL_ACTIONS.modalShowPhotoswipe;
onShowImageModal: (images: IFile[], index: number) => void;
}
const CommentContent: FC<IProps> = memo(({ comment, can_edit, onDelete, modalShowPhotoswipe }) => {
const CommentContent: FC<IProps> = memo(({ comment, can_edit, onDelete, onShowImageModal }) => {
const [isEditing, setIsEditing] = useState(false);
const { current } = useShallowSelect(selectNode);
@ -89,7 +88,7 @@ const CommentContent: FC<IProps> = memo(({ comment, can_edit, onDelete, modalSho
className={classNames(styles.images, { [styles.multiple]: groupped.image.length > 1 })}
>
{groupped.image.map((file, index) => (
<div key={file.id} onClick={() => modalShowPhotoswipe(groupped.image, index)}>
<div key={file.id} onClick={() => onShowImageModal(groupped.image, index)}>
<img src={getURL(file, PRESETS['600'])} alt={file.name} />
</div>
))}

View file

@ -1,26 +1,24 @@
import React, { FC, useCallback } from 'react';
import { INode } from '~/redux/types';
import styles from './styles.module.scss';
import { Avatar } from '~/components/common/Avatar';
import { openUserProfile } from '~/utils/user';
import { useUserDescription } from '~/utils/hooks/user/useUserDescription';
import { INodeUser } from '~/redux/types';
interface Props {
node?: INode;
user?: INodeUser;
}
const NodeAuthorBlock: FC<Props> = ({ node }) => {
const onOpenProfile = useCallback(() => openUserProfile(node?.user?.username), [
node?.user?.username,
]);
const NodeAuthorBlock: FC<Props> = ({ user }) => {
const onOpenProfile = useCallback(() => openUserProfile(user?.username), [user?.username]);
const description = useUserDescription(node?.user);
const description = useUserDescription(user);
if (!node?.user) {
if (!user) {
return null;
}
const { fullname, username, photo } = node.user;
const { fullname, username, photo } = user;
return (
<div className={styles.block} onClick={onOpenProfile}>

View file

@ -6,16 +6,18 @@ import { NodeCommentsBlock } from '~/components/node/NodeCommentsBlock';
import { NodeCommentForm } from '~/components/node/NodeCommentForm';
import { NodeRelatedBlock } from '~/components/node/NodeRelatedBlock';
import { useNodeBlocks } from '~/utils/hooks/node/useNodeBlocks';
import { IComment, INode } from '~/redux/types';
import { useUser } from '~/utils/hooks/user/userUser';
import { IComment, IFile, INode } from '~/redux/types';
import { NodeTagsBlock } from '~/components/node/NodeTagsBlock';
import { INodeRelated } from '~/redux/node/types';
import StickyBox from 'react-sticky-box/dist/esnext';
import styles from './styles.module.scss';
import { NodeAuthorBlock } from '~/components/node/NodeAuthorBlock';
import { IUser } from '~/redux/auth/types';
interface IProps {
node: INode;
user: IUser;
isUser: boolean;
canEdit: boolean;
isLoading: boolean;
commentsOrder: 'ASC' | 'DESC';
@ -24,21 +26,28 @@ interface IProps {
isLoadingComments: boolean;
related: INodeRelated;
lastSeenCurrent?: string;
onShowImageModal: (images: IFile[], index: number) => void;
onLoadMoreComments: () => void;
onDeleteComment: (id: IComment['id'], isLocked: boolean) => void;
}
const NodeBottomBlock: FC<IProps> = ({
node,
user,
canEdit,
isLoading,
isUser,
isLoadingComments,
comments,
commentsCount,
commentsOrder,
related,
lastSeenCurrent,
onLoadMoreComments,
onDeleteComment,
onShowImageModal,
}) => {
const { inline } = useNodeBlocks(node, isLoading);
const { is_user } = useUser();
if (node.deleted_at) {
return <NodeDeletedBadge />;
@ -58,17 +67,21 @@ const NodeBottomBlock: FC<IProps> = ({
comments={comments}
count={commentsCount}
order={commentsOrder}
user={user}
node={node}
onShowImageModal={onShowImageModal}
onLoadMoreComments={onLoadMoreComments}
onDeleteComment={onDeleteComment}
/>
{is_user && !isLoading && <NodeCommentForm nodeId={node.id} />}
{isUser && !isLoading && <NodeCommentForm nodeId={node.id} />}
</Group>
<div className={styles.panel}>
<StickyBox className={styles.sticky} offsetTop={72}>
<div className={styles.left}>
<div className={styles.left_item}>
<NodeAuthorBlock node={node} />
<NodeAuthorBlock user={node?.user} />
</div>
<div className={styles.left_item}>
<NodeTagsBlock node={node} canEdit={canEdit} isLoading={isLoading} />

View file

@ -1,17 +1,13 @@
import React, { FC, memo, useCallback, useMemo } from 'react';
import React, { FC, memo, useMemo } from 'react';
import { Comment } from '../../comment/Comment';
import styles from './styles.module.scss';
import { IComment, ICommentGroup, IFile } from '~/redux/types';
import { groupCommentsByUser } from '~/utils/fn';
import { IUser } from '~/redux/auth/types';
import { canEditComment } from '~/utils/node';
import { nodeLoadMoreComments, nodeLockComment } from '~/redux/node/actions';
import { INodeState } from '~/redux/node/reducer';
import { COMMENTS_DISPLAY } from '~/redux/node/constants';
import { plural } from '~/utils/dom';
import { modalShowPhotoswipe } from '~/redux/modal/actions';
import { useDispatch } from 'react-redux';
import { useGrouppedComments } from '~/utils/hooks/node/useGrouppedComments';
interface IProps {
@ -20,25 +16,26 @@ interface IProps {
user: IUser;
order?: 'ASC' | 'DESC';
lastSeenCurrent?: string;
onShowImageModal: (images: IFile[], index: number) => void;
onLoadMoreComments: () => void;
onDeleteComment: (id: IComment['id'], isLocked: boolean) => void;
}
const NodeComments: FC<IProps> = memo(
({ comments, user, count = 0, order = 'DESC', lastSeenCurrent }) => {
const dispatch = useDispatch();
({
comments,
user,
count = 0,
order = 'DESC',
onLoadMoreComments,
onDeleteComment,
onShowImageModal,
lastSeenCurrent,
}) => {
const left = useMemo(() => Math.max(0, count - comments.length), [comments, count]);
const groupped: ICommentGroup[] = useGrouppedComments(comments, order, lastSeenCurrent);
const onDelete = useCallback(
(id: IComment['id'], locked: boolean) => dispatch(nodeLockComment(id, locked)),
[dispatch]
);
const onLoadMoreComments = useCallback(() => dispatch(nodeLoadMoreComments()), [dispatch]);
const onShowPhotoswipe = useCallback(
(images: IFile[], index: number) => dispatch(modalShowPhotoswipe(images, index)),
[dispatch]
);
const more = useMemo(
() =>
left > 0 && (
@ -60,8 +57,8 @@ const NodeComments: FC<IProps> = memo(
key={group.ids.join()}
group={group}
canEdit={canEditComment(group, user)}
onDelete={onDelete}
modalShowPhotoswipe={onShowPhotoswipe}
onDelete={onDeleteComment}
onShowImageModal={onShowImageModal}
isSame={group.user.id === user.id}
/>
))}

View file

@ -1,29 +1,36 @@
import React, { FC } from 'react';
import { NodeNoComments } from '~/components/node/NodeNoComments';
import { NodeComments } from '~/components/node/NodeComments';
import { IComment, INode } from '~/redux/types';
import { IComment, IFile, INode } from '~/redux/types';
import { useNodeBlocks } from '~/utils/hooks/node/useNodeBlocks';
import { useUser } from '~/utils/hooks/user/userUser';
import { IUser } from '~/redux/auth/types';
interface IProps {
order: 'ASC' | 'DESC';
node: INode;
user: IUser;
comments: IComment[];
count: number;
lastSeenCurrent?: string;
isLoading: boolean;
isLoadingComments: boolean;
onShowImageModal: (images: IFile[], index: number) => void;
onLoadMoreComments: () => void;
onDeleteComment: (id: IComment['id'], isLocked: boolean) => void;
}
const NodeCommentsBlock: FC<IProps> = ({
isLoading,
isLoadingComments,
node,
user,
comments,
count,
lastSeenCurrent,
isLoading,
isLoadingComments,
onLoadMoreComments,
onDeleteComment,
onShowImageModal,
}) => {
const user = useUser();
const { inline } = useNodeBlocks(node, isLoading);
return isLoading || isLoadingComments || (!comments.length && !inline) ? (
@ -35,6 +42,9 @@ const NodeCommentsBlock: FC<IProps> = ({
user={user}
order="DESC"
lastSeenCurrent={lastSeenCurrent}
onShowImageModal={onShowImageModal}
onLoadMoreComments={onLoadMoreComments}
onDeleteComment={onDeleteComment}
/>
);
};