-
Notifications
You must be signed in to change notification settings - Fork 2
/
ViewPost.jsx
183 lines (170 loc) · 8.31 KB
/
ViewPost.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import React from 'react';
import { connect } from 'react-redux';
import { loggedIn } from '../../actions/UserActions';
import { getPost, getLikes, addHeart, addStar, addHand, addBookmark } from '../../actions/BlogPostActions';
import { getAllComments, addComment } from '../../actions/CommentActions';
import CommentCard from '../containers/CommentCard';
import { convertToRaw, convertFromRaw } from 'draft-js';
import {stateToHTML} from 'draft-js-export-html';
// import $ from 'jquery';
import MetaTags from 'react-meta-tags';
class ViewPost extends React.Component {
constructor(props) {
super(props)
this.addUserComment = this.addUserComment.bind(this);
}
componentDidMount(){
this.props.loggedIn();
this.props.getPost();
this.props.getLikes();
this.props.getAllComments();
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-116467756-1');
}
addHeartCount = () => {
let postId = this.props.post.post.id
let userId = this.props.id
let counter = 1
this.props.addHeart(counter, postId, userId);
}
addStarCount() {
let postId = this.props.post.post.id
let userId = this.props.id
let counter = 1
this.props.addStar(counter, postId, userId);
}
addHandCount() {
let postId = this.props.post.post.id
let userId = this.props.id
let counter = 1
this.props.addHand(counter, postId, userId);
}
addUserBookmark() {
let postId = this.props.post.post.id
let userId = this.props.id
this.props.addBookmark(postId, userId);
}
addUserComment(event) {
event.preventDefault();
let post_id = document.getElementById("comment-post-id").value
let user_id = document.getElementById("comment-user-id").value
let text = document.getElementById("text-area").value
this.props.addComment(post_id, user_id, text);
}
render() {
const renderedComments =
this.props.comments.length !== 0 && this.props.comments !== undefined ?
this.props.comments.map(comment => <CommentCard id={comment[0].comment.id} comment={comment[0]} />) : "No comments yet on this post. Do you want to add to this conversation?"
// Setup the rendering of the post body from editorState
let html =
this.props.post !== undefined && this.props.post.length !== 0 ?
stateToHTML(this.props.editorState.getCurrentContent()) : null
// set function to render the html without escaping the tags
function createMarkup() {
return {__html: html};
}
let dateOptions = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
let date = this.props.post !== undefined && this.props.post.length !== 0 ?
new Date(this.props.post.post.created_at) : null
return (
<React.Fragment>
{
this.props.post !== undefined && this.props.post.length !== 0
?
<div className="post-container">
<MetaTags>
<meta property="og:title" content={this.props.post.post.title}/>
<meta property="og:description" content={"Check out" + ' ' + this.props.post.author.first_name + ' ' + this.props.post.author.last_name + "'s last post on Rechov Aliyah"}/>
<meta property="og:image" content="/assets/images/rechov-aliyah-header-img.png"/>
<meta name="twitter:title" content={this.props.post.post.title}/>
<meta name="twitter:description" content={"Check out" + ' ' + this.props.post.author.first_name + ' ' + this.props.post.author.last_name + "'s last post on Rechov Aliyah"}/>
<meta name="twitter:image" content="/assets/images/rechov-aliyah-header-img.png"/>
<meta name="twitter:card" content="summary_large_image"/>
</MetaTags>
<div id="post-title">
<h1>{this.props.post.post.title}</h1>
</div>
<div id="author-info">
<h2>
<span id="author-img">
<img src={this.props.post.post.user.headshot_url ? this.props.post.post.user.headshot_url : "/assets/default-author-headshot.png"} alt="author headshot" />
</span>
{this.props.post.author.first_name + ' ' + this.props.post.author.last_name}
<span id="post-author-twitter"><a href={"https://www.twitter.com/" + this.props.post.author.twtter}><img src="/assets/icons/twitter-icon.png" /></a></span>
{date.toLocaleDateString("en-US",dateOptions)}
</h2>
</div>
<div id="post-body" dangerouslySetInnerHTML={createMarkup()}></div>
<div id="post-actions">
<button id="button-heart" data-type="hearts" onClick={() => this.addHeartCount()}>
<img src="/assets/icons/heart-icon.jpeg" id="reaction-img" />
<span id="like-number">{this.props.post.post.postlike !== undefined && this.props.post.post.postlike.length !== 0 ? this.props.post.post.postlike.hearts : 0}</span>
</button>
<button id="button-star" data-type="stars" onClick={() => this.addStarCount()}>
<img src="/assets/icons/star-icon.png" id="reaction-img" />
<span id="like-number">{this.props.post.post.postlike !== undefined && this.props.post.post.postlike.length !== 0 ? this.props.post.post.postlike.likes : 0}</span>
</button>
<button id="button-hands" data-type="hands" onClick={() => this.addHandCount()}>
<img src="/assets/icons/hands-icon.png" id="reaction-img" />
<span id="like-number">{this.props.post.post.postlike !== undefined && this.props.post.post.postlike.length !== 0 ? this.props.post.post.postlike.hands : 0}</span>
</button>
<button id="button-bookmark" onClick={() => this.addUserBookmark()}>
<img src="/assets/icons/bookmark-icon-small.png" id="reaction-img" />
</button>
</div>
<div id="comments-container-container">
{ this.props.signed_in ?
<div id="comments-container">
<form className="new_comment" id="new_comment" onSubmit={this.addUserComment}>
<input type="hidden" name="user_id" id="comment-user-id" value={this.props.id} />
<input type="hidden" name="post_id" id="comment-post-id" value={this.props.post.post.id} />
<textarea placeholder="Contribute to this conversation" id="text-area" name="text" required="required"/>
<input type="submit" name="submit" value="Submit" id="submit-button" onClick={this.addUserComment} />
</form>
</div>
:
<div id="comments-container">
<div id="post-title">Please login to share a comment</div>
<form method="post" action="/users/sign_in">
<label htmlFor="email">Email</label><input type="email" name="user[email]" size="30" />
<br />
<label htmlFor="password">Password</label><input type="password" name="user[password]" size="30" />
<br />
<div id="submit-btn"><input type="submit" name="commit" value="Log in" data-disable-with="Log in" /></div>
</form>
</div>
}
<div id="single-comment">
{renderedComments}
</div>
</div>
</div>
:
"Loading..."
}
</React.Fragment>
)
}
}
const mapStateToProps = state => {
return {
signed_in: state.users.signed_in,
email: state.users.email,
first_name: state.users.first_name,
last_name: state.users.last_name,
user_location: state.users.user_location,
twitter: state.users.twitter,
id: state.users.id,
picture: state.users.picture,
headshot: state.users.headshot,
post: state.posts.current_post,
// stars: state.posts.current_post.post.postlike.likes,
// hearts: state.posts.current_post.post.postlike.hearts,
// hands: state.posts.current_post.post.postlike.hands,
editorState: state.posts.editorState,
comments: state.comments.comments
}
}
export default connect(mapStateToProps, { loggedIn, getPost, getLikes, addHeart, addStar, addHand, addBookmark, getAllComments, addComment })(ViewPost)