This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.htm
131 lines (128 loc) · 4.13 KB
/
index.htm
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
<!doctype html>
<html lang="en">
<head>
<title>Content Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="This is a content example.">
<script async>
const API = 'http://localhost:8080/';
document.addEventListener('DOMContentLoaded', function () {
fetch(API)
.then((response) => response.json())
.then((data) => writeMessagesToPage(data))
.catch(function(err) {document.getElementById("postbox").innerText = "failed to connect to api..."});
});
function PushMessageToRemote() {
const req = new XMLHttpRequest();
req.open("POST", API);
let messageJSON = JSON.stringify({"author": document.getElementById("author").value, "message": document.getElementById("message").value})
req.setRequestHeader('Content-type', 'application/json');
req.send(messageJSON);
req.addEventListener('load', function() {
if (req.status === 201) {
document.getElementById('sendmsg').style.borderColor = "green";
writeMessagesToPage([{timestamp: (new Date).toJSON(), ...JSON.parse(messageJSON)}]);
} else {
document.getElementById('sendmsg').style.borderColor = "red";
// the server returned any value other than 201
}
setTimeout(function (){
document.getElementById('sendmsg').style.borderColor = "black";
}, 5000);
});
req.addEventListener('error', function() {
// failed to connect to server outright
document.getElementById('sendmsg').style.borderColor = "red";
setTimeout(function (){
document.getElementById('sendmsg').style.borderColor = "black";
}, 5000);
})
}
function writeMessagesToPage(arr) {
let postbox = document.getElementById("postbox");
arr.forEach(function(message) {
let post = document.createElement('div');
let postAuthor = document.createElement('span');
let postTimestamp = document.createElement('span');
let postMessage = document.createElement('span');
let postMsgAuthor = document.createElement('div');
post.className = "post";
postAuthor.className = "author";
postTimestamp.className = "time";
postMessage.className = "message";
postMsgAuthor.className = "message-wrapper";
postAuthor.innerText = message["author"];
postTimestamp.innerText = timeToUserReadable(message["timestamp"]);
postMessage.innerText = message["message"];
post.appendChild(postAuthor);
post.appendChild(postTimestamp);
post.appendChild(document.createElement('hr'));
postMsgAuthor.appendChild(postMessage);
post.appendChild(postMsgAuthor);
postbox.appendChild(post);
})
}
function timeToUserReadable(timestring = "") {
let dateObj = new Date(timestring);
let outstring = `${dateObj.getHours().toString().padStart(2, '0')}:${dateObj.getMinutes().toString().padStart(2, '0')}, ${dateObj.getDate().toString().padStart(2, '0')}/${(dateObj.getMonth() + 1).toString().padStart(2, '0')}/${dateObj.getFullYear()}`
return outstring;
}
</script>
<style>
.send-message-form > div {
margin: auto;
width: 50%;
border: 3px solid black;
padding: 10px;
display:block;
padding-bottom: 0;
}
#sendmsg > button {
float: right;
}
.post {
background-color: lightgray;
padding: 1em;
margin: 3em 1em;
}
.post > .time {
float: right;
}
.send-message-form {
margin: 3em;
}
.post > .message-wrapper {
padding: 1em 0;
}
#sendmsg > .msg-entry-wrapper {
padding-top: 1em;
}
#sendmsg > .msg-entry-wrapper > textarea {
width: 100%;
height: 20vh;
resize: vertical;
}
</style>
</head>
<body>
<h1>Content Example</h1>
<div id="container">
<div id="postbox">
</div>
<hr>
<form action="http://localhost:8080/" method="post" class="send-message-form">
<div id="sendmsg">
<label for="name">Name: </label><input type="text" name="author" id="author" required>
<button onclick="PushMessageToRemote()" type="button">Send</button>
<br>
<div class="msg-entry-wrapper">
<label for="message">Write your message here:</label>
<textarea id="message" name="message"></textarea>
</div>
<br>
</div>
</form>
</div>
</body>
</html>