Skip to content

Commit fbe84e1

Browse files
baysarov077kuduzow
andauthored
Теперь последний комментарий под постом выводится с ограниченной длиной (#329)
* Теперь последний комментарий под постом выводится с ограниченной длиной * Вынес код проверки длинны текста в отдельную функцию и исправил условия сокращения строки * Провел рефакторинг кода * Изменил функцию truncateLongText и добавил условие на проверку последнего комментария * Update src/features/comments/CommentView.jsx --------- Co-authored-by: Kuduzow Akhmad <ammya@ya.ru>
1 parent fa17167 commit fbe84e1

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/app/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ export const SCROLL_TO_TOP_SHOW = 500;
1818
export const TAG_MAX_LENGTH = 15;
1919

2020
export const MAX_NUMBER_OF_TAGS = 5;
21+
22+
export const MAX_LAST_COMMENT_LENGTH = 140;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { MAX_LAST_COMMENT_LENGTH } from 'app/constants';
2+
3+
export const truncateLongText = (value) => {
4+
let str = value;
5+
if (str.length > MAX_LAST_COMMENT_LENGTH) {
6+
str = str.slice(0, MAX_LAST_COMMENT_LENGTH);
7+
const lastSpaceIndex = str.lastIndexOf(' ');
8+
str = str.slice(0, lastSpaceIndex > -1 ? lastSpaceIndex : MAX_LAST_COMMENT_LENGTH);
9+
str += '...';
10+
}
11+
return str;
12+
};

src/features/comments/CommentView.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { useDispatch, useSelector } from 'react-redux';
99
import { selectProfile } from 'features/profile/profileSlice';
1010
import FavoritePopoverContent from 'components/FavoritePopoverContent';
1111
import { Popover } from 'antd';
12+
import { truncateLongText } from 'common/utils/truncateLongText';
1213
import { CommentsActions } from './comment-actions/CommentsActions';
1314
import { likeCommentById, unlikeCommentById } from './commentsSlice';
1415

@@ -121,7 +122,10 @@ export const CommentView = ({ comment, lastComment }) => {
121122
<span>{comment.author?.name}</span>
122123
<StyledTime>{dayjs(comment.createdAt).fromNow()}</StyledTime>
123124
</div>
124-
<Viewer theme="iqa" initialValue={comment.text} />
125+
<Viewer
126+
theme="iqa"
127+
initialValue={lastComment ? truncateLongText(comment.text) : comment.text}
128+
/>
125129
{!lastComment && (
126130
<StyledCommentActions>
127131
<CommentsActions commentId={comment._id} />

0 commit comments

Comments
 (0)