-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbundle.js
229 lines (200 loc) · 8.59 KB
/
bundle.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
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
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
// ******************** Submitting a new form ********************
// const form = document.querySelector("#new-post-form");
// form.addEventListener("submit", submitPost);
function submitPost(e) {
e.preventDefault();
const currentDate = new Date();
const dateTimeStamp = `${currentDate.getDate()}/${
currentDate.getMonth() + 1 // because january starts at 0
}/${currentDate.getFullYear()}, ${currentDate.getHours()}:${currentDate.getMinutes()}`;
console.log(document.getElementById("gifPreview").getAttribute("src"));
const postData = {
subject: e.target.subject.value,
journalInput: e.target.journalInput.value,
gif: document.getElementById("gifPreview").getAttribute("src"),
date: dateTimeStamp,
};
const options = {
method: "POST",
body: JSON.stringify(postData),
headers: {
"Content-Type": "application/json",
},
};
fetch("https://bloguefp.herokuapp.com/", options) // also do we need to put this in a function to export?
.then((resp) => resp.json())
.then(appendPost)
.catch(console.warn);
}
// ******************** Append all new post to the page ********************
function appendPosts(posts) {
posts.forEach((post) => appendPost(post));
}
// ******************** Function to append together a single post from submitted data ********************
let parent = document.getElementById("postsContainer");
function appendPost(data) {
const postsDiv = document.createElement("div");
postsDiv.setAttribute("class", "newPostDiv");
// headers for the subject names
const header = document.createElement("h4");
header.setAttribute("id", "commentHeading");
header.textContent = `Post ${data.id}: ` + data.subject;
// paragraphs for the journal content
const contents = document.createElement("p");
contents.setAttribute("id", "commentContents");
contents.textContent = data.journalInput;
// paragraphs for the date
const date = document.createElement("p");
date.textContent = data.date;
// imgs for the gif
console.log(data.gif);
const newImg = document.createElement("img");
newImg.src = data.gif;
newImg.style.display = "block";
newImg.style.margin = "0 auto";
// div for emoji icons and assigning icons a class of emoji
const reactionDiv = document.createElement("div");
const loveIcon = `<i class="fas fa-heart fa-2x emoji"><small id="heartCounter${data.id}">${data.reactions.heart}</small></i>`;
const cryIcon = `<i class="fas fa-sad-tear fa-2x emoji"><small id="cryCounter${data.id}">${data.reactions.cry}</small></i>`;
const laughIcon = `<i class="fas fa-laugh-squint fa-2x emoji"><small id="laughCounter${data.id}">${data.reactions.laugh}</small></i>`;
reactionDiv.setAttribute("class", `${data.id}`);
reactionDiv.innerHTML = loveIcon + cryIcon + laughIcon;
// create form for comments
const commentDiv = document.createElement("div");
const formComment = document.createElement("form");
// create text input to type comment
const formCommentInput = document.createElement("input");
formCommentInput.setAttribute("type", "text");
formCommentInput.setAttribute("name", "comments");
formCommentInput.setAttribute("placeholder", "Add a comment");
formCommentInput.setAttribute("class", "formCommentInput");
// set id to the post to use later
formComment.setAttribute("id", data.id);
// create submit button
const formCommentSubmitButton = document.createElement("input");
formCommentSubmitButton.setAttribute("type", "submit");
formCommentSubmitButton.setAttribute("class", "formCommentSubmitButton");
// append form together
formComment.append(formCommentInput);
formComment.append(formCommentSubmitButton);
// add event listener to comment submit button
formComment.addEventListener("submit", submitComment); // function below
commentDiv.appendChild(formComment);
// appending each element to the new postsDiv, and then append this new div to existing postsContainer
postsDiv.appendChild(header);
postsDiv.appendChild(contents);
postsDiv.appendChild(newImg);
postsDiv.appendChild(date);
postsDiv.appendChild(reactionDiv);
postsDiv.appendChild(commentDiv);
parent.append(postsDiv);
const emojis = reactionDiv.getElementsByClassName("emoji");
for (let emoji of emojis) {
emoji.addEventListener("click", emojiReact);
}
}
// ******************** Function for users to submit comments to posts ********************
function submitComment(e) {
e.preventDefault();
const postId = parseInt(e.target.getAttribute("id"));
const commentData = {
comment: e.target.comments.value,
};
const options = {
method: "PATCH",
body: JSON.stringify(commentData),
headers: {
"Content-Type": "application/json",
},
};
fetch(`https://bloguefp.herokuapp.com/${postId}`, options)
.then((r) => r.json())
.catch(console.warn);
commentsFunction(commentData, e.target);
e.target.comments.value = "";
}
// ******************** Create function for new replies ********************
function commentsFunction(commentData, formComment) {
const newCommentContainer = document.createElement("div");
const newCommentMessage = document.createElement("p");
newCommentMessage.setAttribute("class", "newCommentMessage");
newCommentMessage.textContent = `${commentData.comment}`;
newCommentContainer.append(newCommentMessage);
formComment.append(newCommentContainer);
}
// ******************** Function to handle emoji ********************
function emojiReact(e) {
console.log(e);
let emoji = e.path[0].classList;
if (emoji[1] === "fa-heart") {
emoji = "heart";
} else if (emoji[1] === "fa-sad-tear") {
emoji = "cry";
} else {
emoji = "laugh";
}
const postId = e.path[1].className;
const options = {
method: "PATCH",
headers: {
"Content-Type": "application/json",
},
};
fetch(`https://bloguefp.herokuapp.com/${postId}/${emoji}`, options)
.then((resp) => resp.json())
.then((data) => emojiCounter(data, postId, emoji))
.catch(console.warn);
}
function emojiCounter(data, postId, emoji) {
document.getElementById(`${emoji}Counter${postId}`).textContent =
data.count;
}
// ******************** Add a GIF ********************
// const gifButton = document.getElementById("gif-button");
// gifButton.addEventListener("click", sendApiRequest);
function sendApiRequest(e) {
// e.preventDefault(); Button has no default behaviour
let apikey = "DV4iN2mItn9xsI2WSKzWWKpTaNpw9H9n";
let url = `https://api.giphy.com/v1/gifs/search?api_key=${apikey}&limit=10&q=`;
let str = document.getElementById("giphy").value.trim();
url = url.concat(str);
console.log(url);
fetch(url)
.then((r) => r.json())
.then((content) => {
let gifimg = document.getElementById("gifPreview");
gifimg.setAttribute(
"src",
content.data[Math.floor(content.data.length * Math.random())]
.images.downsized.url
); // choose a random gif out of the limit of 10
gifimg.classList.add("imgFormat");
//let gifContainer = document.getElementById("gifContainer");
//gifContainer.append(gifimg);
//gifContainer.insertAdjacentElement("afterbegin", gifimg); // gif image will show up as a preview in the make a post section
})
.catch((err) => {
console.log(err);
});
}
// ******************** Get all posts as soon as app is loaded ********************
function getAllPosts() {
fetch("https://bloguefp.herokuapp.com/")
.then((r) => r.json())
.then(appendPosts)
.catch(console.warn);
}
getAllPosts();
// ******************** Function exporting for testing ********************
module.exports = {
submitPost,
appendPost,
appendPosts,
sendApiRequest,
submitComment,
commentsFunction,
emojiReact,
getAllPosts,
};
},{}]},{},[1]);