-
Notifications
You must be signed in to change notification settings - Fork 0
/
Comments.jsx
339 lines (320 loc) · 11.7 KB
/
Comments.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* eslint-disable no-unused-expressions */
import React, { Component } from "react";
import {
Dialog,
DialogTitle,
DialogContent,
DialogContentText,
withStyles,
} from "@material-ui/core";
import { connect } from "react-redux";
import "./Comments.scss";
import { Grid, TextField, Button } from "@material-ui/core";
import Comment from "./comment.jsx";
import { dummyComments } from "./dummycomments";
//---------------NOTE-----------------
// this whole comment component is strictly depending on the format pattern of comments array (see dummyComments.js to see format)
// in order for this component to work you make sure that you are providing the exact same format pattern of comments Array
// if you have a different format then make sure you understand all the flow so that you can change it according to your needs
class Comments extends Component {
constructor(props) {
super(props);
this.state = {
dialogOpen: false,
dialogLikes: [],
commentsArray: [],
ReplyCommentID: "",
commentText: "",
};
this.textInput = React.createRef();
this.focusTextInput = this.focusTextInput.bind(this);
}
componentWillMount() {
this.setState({ commentsArray: dummyComments });
}
postHeart = (ParentCmtID, ReplyCmtID, ChangeHeart) => {
// send the post api to backend with [parentCmtID] or [ReplyCmtID] or both depend on the comment/reply
// when the backend is successfully updated then you can change your frontEnd here by calling this function
// frontEnd and Backend will at the same state
this.change_FrontEnd_State_After_PostHeart(
ParentCmtID,
ReplyCmtID,
ChangeHeart
);
};
postComment = () => {
// send the api to backend to push new comment in parrent comments. if its a reply then send this.state.ReplyCommentID
// to push new reply to that specific parent comment
// when the backend is successfully updated then you can change your frontEnd here by calling this function
// frontEnd and Backend will at the same state
this.change_FrontEnd_State_After_PostComment();
};
handleDialogeClose = () =>
this.setState({ dialogOpen: false, dialogLikes: [] });
focusTextInput() {
this.textInput.current.focus();
}
handleReply = (cmtID) => {
// it will set the state to comment id for which we are currently replying
this.focusTextInput();
this.setState({ ReplyCommentID: cmtID });
};
handleCommentText = (event) => {
this.setState({ commentText: event.target.value });
};
popUserFromLikedArray = (userId, likedArray) => {
return likedArray.filter((user) => userId !== user[Object.keys(user)[0]]);
};
change_FrontEnd_State_After_PostHeart = (
ParentCmtID,
ReplyCmtID,
ChangeHeart
) => {
// commentsCopy here is deep copy of commentsArray state
let commentsCopy = copyCommentsArray(this.state.commentsArray);
let index, oldLikes, UserAlreadyLiked;
// if replyCmtID exist it means user has liked a reply of some comment so [if] clause will run
// if replyCmtID is null it means user has liked a parent comment and [else] clause will run
if (ReplyCmtID) {
// it will find the index of the parent comment and then its replied comment
// index will be array here contains index of parent comment and reply comment [parentCommentIndex, replyCommentIndex]
index = this.findCommentIndex(ParentCmtID, ReplyCmtID);
// get old likes numbers
oldLikes = commentsCopy[index[0]].replies[index[1]].likes.numbers;
// checking if user has already liked this comment
UserAlreadyLiked = this.checkAlreadyLiked(this.props.userId, index);
// if user has already liked then we have to minus the number and remove user from likedArray
// if user has not already liked then we will add the number and add user into likedArray
if (UserAlreadyLiked) {
commentsCopy[index[0]].replies[index[1]].likes.numbers = oldLikes - 1;
commentsCopy[index[0]].replies[
index[1]
].likes.usersLiked = this.popUserFromLikedArray(
this.props.userId,
commentsCopy[index[0]].replies[index[1]].likes.usersLiked
);
} else {
commentsCopy[index[0]].replies[index[1]].likes.numbers = oldLikes + 1;
commentsCopy[index[0]].replies[index[1]].likes.usersLiked.push({
[this.props.userName]: this.props.userId,
});
}
// change the heart icon its a callback function that is executing in commentStructure.jsx
ChangeHeart();
// update the new commentArray
this.setState({ commentsArray: commentsCopy });
} else {
index = this.findCommentIndex(ParentCmtID);
oldLikes = commentsCopy[index].likes.numbers;
UserAlreadyLiked = this.checkAlreadyLiked(this.props.userId, index);
if (UserAlreadyLiked) {
commentsCopy[index].likes.numbers = oldLikes - 1;
commentsCopy[index].likes.usersLiked = this.popUserFromLikedArray(
this.props.userId,
commentsCopy[index].likes.usersLiked
);
} else {
commentsCopy[index].likes.numbers = oldLikes + 1;
commentsCopy[index].likes.usersLiked.push({
[this.props.userName]: this.props.userId,
});
}
ChangeHeart();
this.setState({ commentsArray: commentsCopy });
}
};
change_FrontEnd_State_After_PostComment = () => {
// The commentId here is dummy use some library to randomly generate commentId
const newComment = {
commentId: "12jhg762qwXOpajgDcaf",
username: this.props.userName,
userId: this.props.userId,
avatar: "",
likes: {
numbers: 0,
usersLiked: [],
},
comment: this.state.commentText,
};
// if its a parent comment then we will add replies key in newComment
this.state.ReplyCommentID === "" ? (newComment["replies"] = []) : null;
// deep copy of commentsArray
let commentsCopy = copyCommentsArray(this.state.commentsArray);
// if its not a reply comment then newComment will be added to the array of parent Comments
// it its a reply comment then newComment will be added to the array of reply comments of that specific parent comment
if (this.state.ReplyCommentID === "") {
commentsCopy.push(newComment);
} else {
const index = this.findCommentIndex(this.state.ReplyCommentID);
commentsCopy[index].replies.push(newComment);
}
// updating the new commentArray
this.setState({ commentsArray: commentsCopy, commentText: "" });
};
checkAlreadyLiked = (userId, Commentindex) => {
let usersLikedArray;
if (Commentindex.length !== undefined) {
usersLikedArray = this.state.commentsArray[Commentindex[0]].replies[
Commentindex[1]
].likes.usersLiked;
for (let c = 0; c < usersLikedArray.length; c++) {
if (userId === usersLikedArray[c][Object.keys(usersLikedArray[c])[0]]) {
return true;
}
}
return false;
} else {
usersLikedArray = this.state.commentsArray[Commentindex].likes.usersLiked;
for (let i = 0; i < usersLikedArray.length; i++) {
if (userId === usersLikedArray[i][Object.keys(usersLikedArray[i])[0]]) {
return true;
}
}
return false;
}
};
findCommentIndex = (CommentIDToFind, ReplyCommentIDToFind) => {
if (ReplyCommentIDToFind) {
let parentIndex = this.findCommentIndex(CommentIDToFind);
let parentRepliesArr = this.state.commentsArray[parentIndex].replies;
for (let r = 0; r < parentRepliesArr.length; r++) {
if (parentRepliesArr[r].commentId === ReplyCommentIDToFind) {
return [parentIndex, r];
}
}
} else {
for (let j = 0; j < this.state.commentsArray.length; j++) {
if (this.state.commentsArray[j].commentId === CommentIDToFind) {
return j;
}
}
}
};
render() {
const { classes } = this.props;
return (
<div style={{ marginBottom: 100 }}>
{this.state.commentsArray.length > 0 ? (
// all the comments are iterated and rendered here through Comment.jsx component
// Comment.jsx component represent a single parent comment which will contain its replies too if they have any replies
this.state.commentsArray.map((comment, index) => {
return (
<Comment
key={index}
CurrentUser={this.props.userId}
commentId={comment.commentId}
username={comment.username}
userId={comment.userId}
avatar={comment.avatar}
likes={comment.likes}
text={comment.comment}
replies={comment.replies}
handleReply={(cmtID) => this.handleReply(cmtID)}
handleHeart={(ParentCmtID, ReplyCmtID, ChangeHeart) =>
this.postHeart(ParentCmtID, ReplyCmtID, ChangeHeart)
}
showLikes={(likes) =>
this.setState({ dialogLikes: likes, dialogOpen: true })
}
/>
);
})
) : (
<p>no commmennts</p>
)}
<div className="CM-WriteComment-Container">
<Grid container xs={11} className="CM-WriteComment-div">
<Grid
item
xs={12}
sm={12}
md={9}
className="CM-WriteComment-Input-div"
>
<TextField
id="outlined-textarea"
label=""
placeholder="Write a comment"
multiline
variant="outlined"
fullWidth
className="CM-WriteComment-Input"
InputProps={{ classes: { focused: classes.inputFocus } }}
inputRef={this.textInput}
onChange={this.handleCommentText}
value={this.state.commentText}
/>
</Grid>
<Grid
item
xs={12}
sm={12}
md={3}
className="CM-WriteComment-Btn-div"
>
<Button
id="CM-WriteComment-Btn"
variant="contained"
fullWidth
onClick={this.postComment}
>
Post Comment
</Button>
</Grid>
</Grid>
</div>
<Dialog
open={this.state.dialogOpen}
onClose={() => this.handleDialogeClose()}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
className="Likes-dialog"
classes={{ paper: classes.paper }}
>
<DialogTitle id="alert-dialog-title">
{"People who liked"}
</DialogTitle>
<DialogContent>
{this.state.dialogLikes.map((name, index) => {
return (
<DialogContentText
key={index}
id="alert-dialog-description"
tabIndex={-1}
>
{Object.keys(name)[0]}
</DialogContentText>
);
})}
</DialogContent>
</Dialog>
</div>
);
}
}
const style = (theme) => ({
paper: {
maxHeight: "80vh !important",
},
});
const mapStateToProps = (state) => {
return {
userId: state.setUserType.userId,
userName: state.setUserType.userName,
};
};
export default React.memo(
connect(mapStateToProps, null)(withStyles(style)(Comments))
);
export const copyCommentsArray = (commentsArray) => {
return commentsArray.map((c) => ({
...c,
replies: c.replies.map((r) => ({
...r,
likes: {
numbers: r.likes.numbers,
usersLiked: r.likes.usersLiked.map((l) => ({ ...l })),
},
})),
}));
};