-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.js
153 lines (122 loc) · 5.28 KB
/
feed.js
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
function storyDOMObject(storyJSON, user) {
const card = document.createElement('div');
card.setAttribute('id', storyJSON._id);
card.className = 'story card';
const cardBody = document.createElement('div');
cardBody.className = 'card-body';
card.appendChild(cardBody);
const creatorSpan = document.createElement('a');
creatorSpan.className = 'story-creator card-title';
creatorSpan.innerHTML = storyJSON.creator_name;
creatorSpan.setAttribute('href', '/u/profile?' + storyJSON.creator_id);
cardBody.appendChild(creatorSpan);
const contentSpan = document.createElement('p');
contentSpan.className = 'story-content card-text';
contentSpan.innerHTML = storyJSON.content;
cardBody.appendChild(contentSpan);
const cardFooter = document.createElement('div');
cardFooter.className = 'card-footer';
card.appendChild(cardFooter);
const commentsDiv = document.createElement('div');
commentsDiv.setAttribute('id', storyJSON._id + '-comments');
commentsDiv.className = 'story-comments';
cardFooter.appendChild(commentsDiv);
if (user._id !== undefined)
cardFooter.appendChild(newCommentDOMObject(storyJSON._id));
return card;
}
function commentDOMObject(commentJSON) {
commentDiv = document.createElement('div');
commentDiv.setAttribute('id', commentJSON._id);
commentDiv.className = 'comment mb-2';
commentCreatorSpan = document.createElement('a');
commentCreatorSpan.className = 'comment-creator';
commentCreatorSpan.innerHTML = commentJSON.creator_name;
commentCreatorSpan.setAttribute('href', '/u/profile?' + commentJSON.creator_id);
commentDiv.appendChild(commentCreatorSpan);
commentContentSpan = document.createElement('span');
commentContentSpan.className = 'comment-content';
commentContentSpan.innerHTML = ' | ' + commentJSON.content;
commentDiv.appendChild(commentContentSpan);
return commentDiv;
}
function newCommentDOMObject(parent) {
const newCommentDiv = document.createElement('div');
newCommentDiv.className = 'comment input-group';
const newCommentContent = document.createElement('input');
newCommentContent.setAttribute('type', 'text');
newCommentContent.setAttribute('name', 'content');
newCommentContent.setAttribute('placeholder', 'New Comment');
newCommentContent.setAttribute('id', parent + '-comment-input');
newCommentContent.className = 'form-control';
newCommentDiv.appendChild(newCommentContent);
const newCommentParent = document.createElement('input');
newCommentParent.setAttribute('type', 'hidden');
newCommentParent.setAttribute('name', 'parent');
newCommentParent.setAttribute('value', parent);
newCommentDiv.appendChild(newCommentParent);
const newCommentButtonDiv = document.createElement('div');
newCommentButtonDiv.className = 'input-group-append';
newCommentDiv.appendChild(newCommentButtonDiv);
const newCommentSubmit = document.createElement('button');
newCommentSubmit.innerHTML = 'Submit';
newCommentSubmit.className = 'btn btn-outline-primary';
newCommentSubmit.setAttribute('story-id', parent);
newCommentSubmit.addEventListener('click', submitCommentHandler);
newCommentButtonDiv.appendChild(newCommentSubmit);
return newCommentDiv;
}
function submitCommentHandler() {
const commentInput = document.getElementById(this.getAttribute('story-id') + '-comment-input');
const data = {
content: commentInput.value,
parent: this.getAttribute('story-id')
};
post('/api/comment', data);
commentInput.value = '';
}
function newStoryDOMObject() {
const newStoryDiv = document.createElement('div');
newStoryDiv.className = 'input-group my-3';
const newStoryContent = document.createElement('input');
newStoryContent.setAttribute('type', 'text');
newStoryContent.setAttribute('placeholder', 'New Story');
newStoryContent.className = 'form-control';
newStoryContent.setAttribute('id', 'story-content-input')
newStoryDiv.appendChild(newStoryContent);
const newStoryButtonDiv = document.createElement('div');
newStoryButtonDiv.className = 'input-group-append';
newStoryDiv.appendChild(newStoryButtonDiv);
const newStorySubmit = document.createElement('button');
newStorySubmit.innerHTML = 'Submit';
newStorySubmit.className = 'btn btn-outline-primary';
newStorySubmit.addEventListener('click', submitStoryHandler);
newStoryButtonDiv.appendChild(newStorySubmit);
return newStoryDiv;
}
function submitStoryHandler() {
const newStoryInput = document.getElementById('story-content-input');
const data = {
content: newStoryInput.value,
};
post('/api/story', data);
newStoryInput.value = '';
}
function renderStories(user) {
if (user._id !== undefined)
document.getElementById('new-story').appendChild(newStoryDOMObject());
const storiesDiv = document.getElementById('stories');
get('/api/stories', {}, function(storiesArr) {
for (let i = 0; i < storiesArr.length; i++) {
const currentStory = storiesArr[i];
storiesDiv.prepend(storyDOMObject(currentStory, user));
get('/api/comment', { 'parent': currentStory._id }, function(commentsArr) {
for (let j = 0; j < commentsArr.length; j++) {
const currentComment = commentsArr[j];
const commentDiv = document.getElementById(currentComment.parent + '-comments');
commentDiv.appendChild(commentDOMObject(currentComment));
}
});
}
});
}