import React from 'react'; import { InjectedIntlProps, injectIntl } from 'react-intl'; import ListItem from '@material-ui/core/ListItem'; import Typography from '@material-ui/core/Typography'; // import DeleteIcon from '@material-ui/icons/Delete'; import CircularProgress from '@material-ui/core/CircularProgress'; import moment from 'moment'; import { compose } from 'redux'; import { connect } from 'react-redux'; import messages from 'CommentSection/messages'; import { IComment, } from 'CommentSection/types'; import ListItemText from '@material-ui/core/ListItemText'; import Button from '@material-ui/core/Button'; import { AstraTheme } from 'Themes/themes'; import { deleteCommentBeginAction, setReplyUser, } from 'CommentSection/actions'; import ReactMarkdown from 'react-markdown'; import Linkify from 'react-linkify'; import { commentToolButtonStyle } from 'CommentSection/components/styles'; import 'CommentSection/components/reactMarkdownOverride.css'; export interface ICommentItemProps { comment: IComment; divider: boolean; key: string; } export interface IDispatchProps { deleteComment(comment: IComment): void; replyComment(userString: string): void; } const momentLocale = moment.locale(); moment.updateLocale(momentLocale, { calendar: { lastWeek: 'l, LT', sameElse: 'l, LT', }, }); const prettyCommentStyle = { fontFamily: AstraTheme.typography.fontFamily, }; export const OwnerTools = ({ handleOnDelete, intl }) => { return (
); }; export const NonOwnerTools = ({ handleOnReply, intl }) => { return (
); }; export const DeletedView = ({ undoDelete, wasDeleted, intl }) => { return (
{intl.formatMessage(messages.undoWarning)}
); }; export const CircularCountdown = () => { const [progress, setProgress] = React.useState(0); React.useEffect(() => { function tick() { setProgress(oldProgress => (oldProgress >= 100 ? oldProgress : oldProgress + 1)); } const timer = setInterval(tick, 20); return () => { clearInterval(timer); }; }); return ( ); }; let timer; export class CommentItem extends React.Component { initialComment:IComment = { id: '', author: '', authorId: '', message: '', createdDate: '', isOwner: true, }; state = { toBeDeleted: false, wasDeleted: false, toBeDeletedComment: this.initialComment }; componentWillUpdate = (prevProps) => { if (prevProps.comment.id !== this.props.comment.id) { this.setState({ toBeDeleted: false, wasDeleted: false }); } } onDelete = () => { // we want to make sure we delete the right one, even if props update this.setState({ toBeDeleted: true, toBeDeletedComment: this.props.comment }); timer = setTimeout( () => { this.setState({ wasDeleted: true }); this.props.deleteComment(this.state.toBeDeletedComment); }, 3500, ); } undoDelete = () => { clearTimeout(timer); this.setState({ toBeDeleted: false }); } onReply = () => { this.props.replyComment(`@[${this.props.comment.author}](${this.props.comment.authorId})`); } public render() { const { comment, divider, key, } = this.props; const prettyComment = comment.message.replace(/\@\[(.*?)\]\((.*?)\)/g, '**$1**'); return ( {this.state.toBeDeleted && ( )} {!this.state.toBeDeleted && (
{comment.author} {moment(comment.createdDate).calendar()}
{comment.isOwner && ( )} {!comment.isOwner && ( )}
)}
); } } export const mapDispatchToProps = (dispatch) => { return { deleteComment: (comment) => { console.log(comment); dispatch(deleteCommentBeginAction(comment)); }, replyComment: userString => dispatch(setReplyUser(userString)), }; }; const withConnect = connect( null, mapDispatchToProps, ); export const CommentItemWithIntl = injectIntl(CommentItem, { withRef: true }); export default compose( withConnect, )(CommentItemWithIntl);