import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import { Button, Divider, Form, Grid, Icon, TextArea } from 'semantic-ui-react'; import TimeAgo from 'react-timeago'; import UserAvatar from 'react-user-avatar'; import ReactMarkdown from 'react-markdown'; import { createPost } from '../redux/actions/transactionsActions'; class NewPost extends Component { constructor(props) { super(props); const { subject } = props; this.handleInputChange = this.handleInputChange.bind(this); this.handlePreviewToggle = this.handlePreviewToggle.bind(this); this.validateAndPost = this.validateAndPost.bind(this); this.newPostOuterRef = React.createRef(); this.state = { postSubjectInput: subject ? subject : '', postContentInput: '', postSubjectInputEmptySubmit: false, postContentInputEmptySubmit: false, previewEnabled: false, previewDate: '' }; } componentDidMount() { this.newPostOuterRef.current.scrollIntoView(true); } getDate() { const currentdate = new Date(); return (`${currentdate.getMonth() + 1} ${ currentdate.getDate()}, ${ currentdate.getFullYear()}, ${ currentdate.getHours()}:${ currentdate.getMinutes()}:${ currentdate.getSeconds()}`); } async validateAndPost() { const { postSubjectInput, postContentInput } = this.state; const { topicID, onPostCreated, dispatch } = this.props; if (postSubjectInput === '' || postContentInput === '') { this.setState({ postSubjectInputEmptySubmit: postSubjectInput === '', postContentInputEmptySubmit: postContentInput === '' }); return; } dispatch( createPost(topicID, { postSubject: postSubjectInput, postMessage: postContentInput }), ); onPostCreated(); } handleInputChange(event) { this.setState({ [event.target.name]: event.target.value }); } handlePreviewToggle() { this.setState(prevState => ({ previewEnabled: !prevState.previewEnabled, previewDate: this.getDate() })); } render() { const { previewDate, postSubjectInputEmptySubmit, postSubjectInput, postContentInputEmptySubmit, postContentInput, previewEnabled } = this.state; const { postIndex, avatarUrl, user, onCancelClick } = this.props; return (
# {postIndex}
{user.username} {previewEnabled && }
{previewEnabled && (`Subject: ${ postSubjectInput}`) }