-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathchat.js
More file actions
182 lines (158 loc) · 5.93 KB
/
chat.js
File metadata and controls
182 lines (158 loc) · 5.93 KB
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
const chatFeedDiv = document.getElementById("chat-feed");
const fullMsgInChat = document.querySelector(".div-for-each-msg");
const msgInChat = document.querySelector(".msg-text-in-chat-view");
const usernameInChat = document.querySelector(".username-in-chat-view");
const refreshBtn = document.getElementById("refresh-btn");
const sendBtn = document.getElementById("send-msg-btn");
const pollBtn = document.getElementById("poll-btn");
const addMsgUsernameInput = document.getElementById("add-msg-username");
const addMsgTextInput = document.getElementById("add-msg-text");
const confirmToUser = document.getElementById("confirm-to-user");
// const url = "http://localhost:3000";
const url = "https://katchatapp.hosting.codeyourfuture.io";
// generatequote and show to user
const seeAllMessages = async () => {
// const selectedQuote = pickFromArray(quotes);
const response = await fetch(url);
const allMessages = await response.json();
//this fixes the messages not showing on live sitwe
state.messages = allMessages;
// quote.innerHTML = selectedQuote.quote;
// author.innerHTML = selectedQuote.author;
// change greetingi am keeping everythign as is and jusy want to
// document.getElementById("greeting").innerHTML = "Your quote is:";
// document.getElementById("greeting").style = "size: 1rem";
chatFeedDiv.innerHTML = "";
allMessages.forEach((msg) => {
chatFeedDiv.innerHTML += `
<div class="div-for-each-msg">
<p class="msg-in-chat-view">${msg.msgText}</p>
<p class="username-in-chat-view">${msg.username}</p>
</div>`;
});
};
//show to user on click of new quote
refreshBtn.addEventListener("click", seeAllMessages);
const sendMsg = async () => {
//removing whitespace or special characters
const addMsgText = addMsgTextInput.value
.trim()
.replace(/[^a-zA-Z0-9,.;:?! ]/g, "");
const addMsgUsername = addMsgUsernameInput.value
.trim()
.replace(/[^a-zA-Z0-9,.;:?! ]/g, "")
.toUpperCase();
//check if empty or too long
if (!addMsgText || !addMsgUsername) {
confirmToUser.innerHTML = "Please add message text and your username.";
return;
}
if (addMsgText.length > 400 || addMsgUsername.length > 40) {
confirmToUser.innerHTML =
"Message must be up to 400 chars and username must be less than 40 chars.";
return;
}
// so macthes backend (typeof body != "object" || !("quote" in body) || !("author" in body))
const addingMsg = {
msgText: addMsgText,
username: addMsgUsername,
};
const responseFromAdd = await fetch(url, {
method: "POST",
headers: {
//so backedn can parde body as json
"Content-Type": "application/json",
},
//turn obj into str typeof body != "object"
body: JSON.stringify(addingMsg),
});
// quotes.push(newQuoteAuthor, newQuoteText); - rhtis alsready in the backed so no need
// tell user
if (responseFromAdd.ok === true) {
confirmToUser.innerHTML = "Your message has been sent.";
//clear input
addMsgTextInput.value = "";
addMsgUsernameInput.value = "";
} else {
//take error message frrom response grab from heree
// console.error(`Failed to extract quote and author from post body: ${bodyString}`);
// res.status(400).send("Expected body to be a JSON object containing keys quote and author.");
// return;
const errorToShow = await responseFromAdd.text();
confirmToUser.innerHTML = `${errorToShow} Please try again.`;
}
};
sendBtn.addEventListener("click", sendMsg);
// auto refresh
// setInterval(seeAllMessages, 2000);
//polling coursework
// const keepFetchingMessages = async () => {
// const lastMessageTime = state.messages.length > 0 ? state.messages[state.messages.length - 1].timestamp : null;
// const queryString = lastMessageTime ? `?since=${lastMessageTime}` : "";
// const url = `${server}/messages${queryString}`;
// const rawResponse = await fetch(url);
// const response = await rawResponse.json();
// state.messages.push(...response);
// render();
// setTimeout(keepFetchingMessages, 100);
// }
let messages = [];
const state = { messages: [] };
//render defining from coursework example build from
const render = () => {
chatFeedDiv.innerHTML = "";
state.messages.forEach((msg) => {
chatFeedDiv.innerHTML += `
<div class="div-for-each-msg">
<p class="msg-in-chat-view">${msg.msgText}</p>
<p class="username-in-chat-view">${msg.username}</p>
</div>`;
});
};
//polling coursework
const keepFetchingMessages = async () => {
const lastMessageTime =
state.messages.length > 0
? state.messages[state.messages.length - 1].timestamp
: null;
const queryString = lastMessageTime ? `?since=${lastMessageTime}` : "";
const urlQueryMod = `${url}/messages${queryString}`;
const rawResponse = await fetch(urlQueryMod);
const response = await rawResponse.json();
state.messages.push(...response);
render();
setTimeout(keepFetchingMessages, 100);
};
pollBtn.addEventListener("click", keepFetchingMessages);
//test long poll
const longPollBtn = document.getElementById("long-poll-btn");
const testLongPoll = async () => {
const lastMessageTime =
state.messages.length > 0
? state.messages[state.messages.length - 1].timestamp
: null;
const queryString = lastMessageTime ? `?since=${lastMessageTime}` : "";
const urlQueryMod = `${url}/long-poll${queryString}`;
const rawResponse = await fetch(urlQueryMod);
const response = await rawResponse.json();
state.messages.push(...response);
render();
testLongPoll();
};
longPollBtn.addEventListener("click", testLongPoll);
//addiitonal privacy feature hide messages
const hideMessages = document.getElementById("hide-btn");
hideMessages.addEventListener("click", () => {
if (chatFeedDiv.style.display === "none") {
chatFeedDiv.style.display = "block";
hideMessages.textContent = "Hide chat";
} else {
chatFeedDiv.style.display = "none";
hideMessages.textContent = "Show chat";
}
});
// seeAllMessages();
// but with long poll
seeAllMessages().then(() => {
testLongPoll();
});