|
@ -1,4 +1,5 @@ |
|
|
import React, { Component } from 'react'; |
|
|
import React, { Component } from 'react'; |
|
|
|
|
|
import PropTypes from 'prop-types'; |
|
|
import { connect } from 'react-redux'; |
|
|
import { connect } from 'react-redux'; |
|
|
|
|
|
|
|
|
import { Button, Divider, Form, Grid, Icon, TextArea } from 'semantic-ui-react'; |
|
|
import { Button, Divider, Form, Grid, Icon, TextArea } from 'semantic-ui-react'; |
|
@ -10,8 +11,9 @@ import ReactMarkdown from 'react-markdown'; |
|
|
import { createPost } from '../redux/actions/transactionsActions'; |
|
|
import { createPost } from '../redux/actions/transactionsActions'; |
|
|
|
|
|
|
|
|
class NewPost extends Component { |
|
|
class NewPost extends Component { |
|
|
constructor(props, context) { |
|
|
constructor(props) { |
|
|
super(props); |
|
|
super(props); |
|
|
|
|
|
const { subject } = props; |
|
|
|
|
|
|
|
|
this.handleInputChange = this.handleInputChange.bind(this); |
|
|
this.handleInputChange = this.handleInputChange.bind(this); |
|
|
this.handlePreviewToggle = this.handlePreviewToggle.bind(this); |
|
|
this.handlePreviewToggle = this.handlePreviewToggle.bind(this); |
|
@ -20,7 +22,7 @@ class NewPost extends Component { |
|
|
this.newPostOuterRef = React.createRef(); |
|
|
this.newPostOuterRef = React.createRef(); |
|
|
|
|
|
|
|
|
this.state = { |
|
|
this.state = { |
|
|
postSubjectInput: this.props.subject ? this.props.subject : '', |
|
|
postSubjectInput: subject ? subject : '', |
|
|
postContentInput: '', |
|
|
postContentInput: '', |
|
|
postSubjectInputEmptySubmit: false, |
|
|
postSubjectInputEmptySubmit: false, |
|
|
postContentInputEmptySubmit: false, |
|
|
postContentInputEmptySubmit: false, |
|
@ -29,24 +31,41 @@ class NewPost extends Component { |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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() { |
|
|
async validateAndPost() { |
|
|
if (this.state.postSubjectInput === '' || this.state.postContentInput |
|
|
const { postSubjectInput, postContentInput } = this.state; |
|
|
|
|
|
const { topicID, onPostCreated, dispatch } = this.props; |
|
|
|
|
|
|
|
|
|
|
|
if (postSubjectInput === '' || postContentInput |
|
|
=== '') { |
|
|
=== '') { |
|
|
this.setState({ |
|
|
this.setState({ |
|
|
postSubjectInputEmptySubmit: this.state.postSubjectInput === '', |
|
|
postSubjectInputEmptySubmit: postSubjectInput === '', |
|
|
postContentInputEmptySubmit: this.state.postContentInput === '' |
|
|
postContentInputEmptySubmit: postContentInput === '' |
|
|
}); |
|
|
}); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
this.props.dispatch( |
|
|
dispatch( |
|
|
createPost(this.props.topicID, |
|
|
createPost(topicID, |
|
|
{ |
|
|
{ |
|
|
postSubject: this.state.postSubjectInput, |
|
|
postSubject: postSubjectInput, |
|
|
postMessage: this.state.postContentInput |
|
|
postMessage: postContentInput |
|
|
}), |
|
|
}), |
|
|
); |
|
|
); |
|
|
this.props.onPostCreated(); |
|
|
onPostCreated(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
handleInputChange(event) { |
|
|
handleInputChange(event) { |
|
@ -56,29 +75,25 @@ class NewPost extends Component { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
handlePreviewToggle() { |
|
|
handlePreviewToggle() { |
|
|
this.setState((prevState, props) => ({ |
|
|
this.setState(prevState => ({ |
|
|
previewEnabled: !prevState.previewEnabled, |
|
|
previewEnabled: !prevState.previewEnabled, |
|
|
previewDate: this.getDate() |
|
|
previewDate: this.getDate() |
|
|
})); |
|
|
})); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
getDate() { |
|
|
|
|
|
const currentdate = new Date(); |
|
|
|
|
|
return (`${currentdate.getMonth() + 1} ${ |
|
|
|
|
|
currentdate.getDate()}, ${ |
|
|
|
|
|
currentdate.getFullYear()}, ${ |
|
|
|
|
|
currentdate.getHours()}:${ |
|
|
|
|
|
currentdate.getMinutes()}:${ |
|
|
|
|
|
currentdate.getSeconds()}`);
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
render() { |
|
|
render() { |
|
|
|
|
|
const { |
|
|
|
|
|
previewDate, postSubjectInputEmptySubmit, postSubjectInput, postContentInputEmptySubmit, |
|
|
|
|
|
postContentInput, previewEnabled |
|
|
|
|
|
} = this.state; |
|
|
|
|
|
const { postIndex, avatarUrl, user, onCancelClick } = this.props; |
|
|
|
|
|
|
|
|
return ( |
|
|
return ( |
|
|
<div className="post" ref={this.newPostOuterRef}> |
|
|
<div className="post" ref={this.newPostOuterRef}> |
|
|
<Divider horizontal> |
|
|
<Divider horizontal> |
|
|
<span className="grey-text"> |
|
|
<span className="grey-text"> |
|
|
# |
|
|
# |
|
|
{this.props.postIndex} |
|
|
{postIndex} |
|
|
</span> |
|
|
</span> |
|
|
</Divider> |
|
|
</Divider> |
|
|
<Grid> |
|
|
<Grid> |
|
@ -87,39 +102,39 @@ class NewPost extends Component { |
|
|
<UserAvatar |
|
|
<UserAvatar |
|
|
size="52" |
|
|
size="52" |
|
|
className="inline user-avatar" |
|
|
className="inline user-avatar" |
|
|
src={this.props.avatarUrl} |
|
|
src={avatarUrl} |
|
|
name={this.props.user.username} |
|
|
name={user.username} |
|
|
/> |
|
|
/> |
|
|
</Grid.Column> |
|
|
</Grid.Column> |
|
|
<Grid.Column width={15}> |
|
|
<Grid.Column width={15}> |
|
|
<div className=""> |
|
|
<div className=""> |
|
|
<div className="stretch-space-between"> |
|
|
<div className="stretch-space-between"> |
|
|
<span><strong>{this.props.user.username}</strong></span> |
|
|
<span><strong>{user.username}</strong></span> |
|
|
<span className="grey-text"> |
|
|
<span className="grey-text"> |
|
|
{this.state.previewEnabled |
|
|
{previewEnabled |
|
|
&& <TimeAgo date={this.state.previewDate} /> |
|
|
&& <TimeAgo date={previewDate} /> |
|
|
} |
|
|
} |
|
|
</span> |
|
|
</span> |
|
|
</div> |
|
|
</div> |
|
|
<div className="stretch-space-between"> |
|
|
<div className="stretch-space-between"> |
|
|
<span> |
|
|
<span> |
|
|
<strong> |
|
|
<strong> |
|
|
{this.state.previewEnabled |
|
|
{previewEnabled |
|
|
&& (`Subject: ${ |
|
|
&& (`Subject: ${ |
|
|
this.state.postSubjectInput}`)
|
|
|
postSubjectInput}`)
|
|
|
} |
|
|
} |
|
|
</strong> |
|
|
</strong> |
|
|
</span> |
|
|
</span> |
|
|
</div> |
|
|
</div> |
|
|
<div className="post-content"> |
|
|
<div className="post-content"> |
|
|
<div style={{ |
|
|
<div style={{ |
|
|
display: this.state.previewEnabled |
|
|
display: previewEnabled |
|
|
? 'block' |
|
|
? 'block' |
|
|
: 'none' |
|
|
: 'none' |
|
|
}} |
|
|
}} |
|
|
> |
|
|
> |
|
|
<ReactMarkdown |
|
|
<ReactMarkdown |
|
|
source={this.state.postContentInput} |
|
|
source={postContentInput} |
|
|
className="markdown-preview" |
|
|
className="markdown-preview" |
|
|
/> |
|
|
/> |
|
|
</div> |
|
|
</div> |
|
@ -127,14 +142,14 @@ class NewPost extends Component { |
|
|
<Form.Input |
|
|
<Form.Input |
|
|
key="postSubjectInput" |
|
|
key="postSubjectInput" |
|
|
style={{ |
|
|
style={{ |
|
|
display: this.state.previewEnabled |
|
|
display: previewEnabled |
|
|
? 'none' |
|
|
? 'none' |
|
|
: '' |
|
|
: '' |
|
|
}} |
|
|
}} |
|
|
name="postSubjectInput" |
|
|
name="postSubjectInput" |
|
|
error={this.state.postSubjectInputEmptySubmit} |
|
|
error={postSubjectInputEmptySubmit} |
|
|
type="text" |
|
|
type="text" |
|
|
value={this.state.postSubjectInput} |
|
|
value={postSubjectInput} |
|
|
placeholder="Subject" |
|
|
placeholder="Subject" |
|
|
id="postSubjectInput" |
|
|
id="postSubjectInput" |
|
|
onChange={this.handleInputChange} |
|
|
onChange={this.handleInputChange} |
|
@ -142,15 +157,15 @@ class NewPost extends Component { |
|
|
<TextArea |
|
|
<TextArea |
|
|
key="postContentInput" |
|
|
key="postContentInput" |
|
|
style={{ |
|
|
style={{ |
|
|
display: this.state.previewEnabled |
|
|
display: previewEnabled |
|
|
? 'none' |
|
|
? 'none' |
|
|
: '' |
|
|
: '' |
|
|
}} |
|
|
}} |
|
|
name="postContentInput" |
|
|
name="postContentInput" |
|
|
className={this.state.postContentInputEmptySubmit |
|
|
className={postContentInputEmptySubmit |
|
|
? 'form-textarea-required' |
|
|
? 'form-textarea-required' |
|
|
: ''} |
|
|
: ''} |
|
|
value={this.state.postContentInput} |
|
|
value={postContentInput} |
|
|
placeholder="Post" |
|
|
placeholder="Post" |
|
|
id="postContentInput" |
|
|
id="postContentInput" |
|
|
onChange={this.handleInputChange} |
|
|
onChange={this.handleInputChange} |
|
@ -177,11 +192,11 @@ class NewPost extends Component { |
|
|
onClick={this.handlePreviewToggle} |
|
|
onClick={this.handlePreviewToggle} |
|
|
color="yellow" |
|
|
color="yellow" |
|
|
> |
|
|
> |
|
|
{this.state.previewEnabled ? 'Edit' : 'Preview'} |
|
|
{previewEnabled ? 'Edit' : 'Preview'} |
|
|
</Button> |
|
|
</Button> |
|
|
<Button |
|
|
<Button |
|
|
type="button" |
|
|
type="button" |
|
|
onClick={this.props.onCancelClick} |
|
|
onClick={onCancelClick} |
|
|
color="red" |
|
|
color="red" |
|
|
> |
|
|
> |
|
|
Cancel |
|
|
Cancel |
|
@ -196,12 +211,19 @@ class NewPost extends Component { |
|
|
</div> |
|
|
</div> |
|
|
); |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
componentDidMount() { |
|
|
|
|
|
this.newPostOuterRef.current.scrollIntoView(true); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
NewPost.propTypes = { |
|
|
|
|
|
subject: PropTypes.string, |
|
|
|
|
|
topicID: PropTypes.number.isRequired, |
|
|
|
|
|
postIndex: PropTypes.number.isRequired, |
|
|
|
|
|
avatarUrl: PropTypes.string, |
|
|
|
|
|
user: PropTypes.object.isRequired, |
|
|
|
|
|
onCancelClick: PropTypes.func.isRequired, |
|
|
|
|
|
dispatch: PropTypes.func.isRequired, |
|
|
|
|
|
onPostCreated: PropTypes.func.isRequired |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
const mapStateToProps = state => ({ |
|
|
const mapStateToProps = state => ({ |
|
|
orbitDB: state.orbitDB, |
|
|
orbitDB: state.orbitDB, |
|
|
user: state.user |
|
|
user: state.user |
|
|