2019-08-31 04:07:25 +05:00
|
|
|
import React, { useCallback, useRef } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
import { Comment } from 'semantic-ui-react';
|
2019-10-03 03:02:46 +05:00
|
|
|
import { Markdown } from '../../../lib/custom-ui';
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
import CommentEdit from './CommentEdit';
|
2019-08-31 04:07:25 +05:00
|
|
|
import User from '../../User';
|
|
|
|
import DeletePopup from '../../DeletePopup';
|
|
|
|
|
2020-05-29 19:31:19 +05:00
|
|
|
import styles from './ItemComment.module.scss';
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
const ItemComment = React.memo(
|
2021-06-24 01:05:22 +05:00
|
|
|
({ data, createdAt, isPersisted, user, canEdit, onUpdate, onDelete }) => {
|
2019-08-31 04:07:25 +05:00
|
|
|
const [t] = useTranslation();
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
const commentEdit = useRef(null);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
const handleEditClick = useCallback(() => {
|
2020-08-04 01:32:46 +05:00
|
|
|
commentEdit.current.open();
|
2019-08-31 04:07:25 +05:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Comment>
|
|
|
|
<span className={styles.user}>
|
2020-04-21 05:04:34 +05:00
|
|
|
<User name={user.name} avatarUrl={user.avatarUrl} />
|
2019-08-31 04:07:25 +05:00
|
|
|
</span>
|
|
|
|
<div className={classNames(styles.content)}>
|
|
|
|
<div className={styles.title}>
|
|
|
|
<span className={styles.author}>{user.name}</span>
|
|
|
|
<span className={styles.date}>
|
|
|
|
{t('format:longDateTime', {
|
|
|
|
postProcess: 'formatDate',
|
|
|
|
value: createdAt,
|
|
|
|
})}
|
|
|
|
</span>
|
|
|
|
</div>
|
2020-08-04 01:32:46 +05:00
|
|
|
<CommentEdit ref={commentEdit} defaultData={data} onUpdate={onUpdate}>
|
2019-08-31 04:07:25 +05:00
|
|
|
<>
|
2022-04-22 02:06:12 +05:00
|
|
|
<div className={styles.text}>
|
|
|
|
<Markdown linkTarget="_blank">{data.text}</Markdown>
|
|
|
|
</div>
|
2021-06-24 01:05:22 +05:00
|
|
|
{canEdit && (
|
|
|
|
<Comment.Actions>
|
2019-08-31 04:07:25 +05:00
|
|
|
<Comment.Action
|
|
|
|
as="button"
|
|
|
|
content={t('action.edit')}
|
|
|
|
disabled={!isPersisted}
|
|
|
|
onClick={handleEditClick}
|
|
|
|
/>
|
|
|
|
<DeletePopup
|
|
|
|
title={t('common.deleteComment', {
|
|
|
|
context: 'title',
|
|
|
|
})}
|
|
|
|
content={t('common.areYouSureYouWantToDeleteThisComment')}
|
|
|
|
buttonContent={t('action.deleteComment')}
|
|
|
|
onConfirm={onDelete}
|
|
|
|
>
|
|
|
|
<Comment.Action
|
|
|
|
as="button"
|
|
|
|
content={t('action.delete')}
|
|
|
|
disabled={!isPersisted}
|
|
|
|
/>
|
|
|
|
</DeletePopup>
|
2021-06-24 01:05:22 +05:00
|
|
|
</Comment.Actions>
|
|
|
|
)}
|
2019-08-31 04:07:25 +05:00
|
|
|
</>
|
2020-08-04 01:32:46 +05:00
|
|
|
</CommentEdit>
|
2019-08-31 04:07:25 +05:00
|
|
|
</div>
|
|
|
|
</Comment>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
ItemComment.propTypes = {
|
|
|
|
data: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
|
|
|
|
createdAt: PropTypes.instanceOf(Date).isRequired,
|
|
|
|
isPersisted: PropTypes.bool.isRequired,
|
|
|
|
user: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
|
2021-06-24 01:05:22 +05:00
|
|
|
canEdit: PropTypes.bool.isRequired,
|
2019-08-31 04:07:25 +05:00
|
|
|
onUpdate: PropTypes.func.isRequired,
|
|
|
|
onDelete: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ItemComment;
|