Added logbook stuff.

This commit is contained in:
Andrew Lalis 2023-07-22 18:03:45 -04:00
parent 4daf7ccc41
commit de03f82dea
4 changed files with 185 additions and 0 deletions

View File

@ -23,6 +23,7 @@
<a href="projects.html">Projects</a> <a href="projects.html">Projects</a>
<a href="training.html">Training</a> <a href="training.html">Training</a>
<a href="contact.html">Contact</a> <a href="contact.html">Contact</a>
<a href="logbook.html">Logbook</a>
</div> </div>
<div> <div>
<a href="https://github.com/andrewlalis">GitHub</a> <a href="https://github.com/andrewlalis">GitHub</a>

87
logbook.html Normal file
View File

@ -0,0 +1,87 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Andrew's Logbook</title>
<meta charset="utf-8">
<meta name="description" content="The logbook for andrewlalis.com">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles/font.css" type="text/css">
<script src="scripts/themes.min.js"></script>
<noscript><style>.jsonly{display: none !important;}</style></noscript>
<link rel="stylesheet" href="styles/style.css" type="text/css">
<link rel="stylesheet" href="styles/logbook.css" type="text/css">
</head>
<body>
<header class="page-header">
<h1>Andrew's Logbook</h1>
<nav>
<div>
<a href="index.html">Home</a>
<a href="articles.html">Articles</a>
<a href="projects.html">Projects</a>
<a href="training.html">Training</a>
<a href="contact.html">Contact</a>
<a class="page-header-selected" href="logbook.html">Logbook</a>
</div>
<div>
<a href="https://github.com/andrewlalis">GitHub</a>
<a href="https://www.linkedin.com/in/andrew-lalis/">LinkedIn</a>
<a href="https://www.youtube.com/channel/UC9X4mx6-ObPUB6-ud2IGAFQ">YouTube</a>
</div>
</nav>
<button id="themeToggleButton" class="jsonly">Change Color Theme</button>
<hr>
</header>
<main>
<article>
<h2>Logbook</h2>
<p>
If you'd like, you can leave a message in my logbook so that I and others might see it.
</p>
<p style="font-size: smaller;">
<em>Note that I do check all logs and some basic information about who sent them (IP address, browser, etc). Inappropriate comments will be removed and their authors banned.</em>
</p>
<form id="logbook-form">
<div>
<label>
Name
<input
id="logbook-name-input"
type="text"
name="name"
minlength="2"
maxlength="32"
pattern="[a-zA-Z0-9-_ ]+"
required
/>
</label>
</div>
<div>
<label>
Message
<textarea
id="logbook-message-input"
name="message"
style="width: 100%; min-height: 10ch;"
minlength="2"
maxlength="255"
required
></textarea>
</label>
</div>
<button type="submit">Submit</button>
</form>
<p id="logbook-form-message-box" style="display: none;"></p>
</article>
<div id="messages-container"></div>
</main>
<script src="scripts/logbook.js"></script>
</body>
</html>

84
scripts/logbook.js Normal file
View File

@ -0,0 +1,84 @@
const LOGBOOK_URL = "http://localhost:8080";
const messagesContainer = document.getElementById("messages-container");
const form = document.getElementById("logbook-form");
const formMessageBox = document.getElementById("logbook-form-message-box");
form.onsubmit = async (e) => {
e.preventDefault();
const data = {
name: document.getElementById("logbook-name-input").value,
message: document.getElementById("logbook-message-input").value
};
formMessageBox.style.display = "block";
formMessageBox.innerText = "Sending log entry...";
try {
const response = await fetch(LOGBOOK_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
},
body: JSON.stringify(data)
});
if (response.ok) {
formMessageBox.innerText = "Log entry added!";
form.style.display = "none"; // Hide the form.
renderMessages(await fetchMessages());
} else {
formMessageBox.innerText = "Log entry rejected: " + response.status;
}
} catch (error) {
formMessageBox.innerText = "An error occurred: " + error;
}
}
// On startup, load the latest messages and show them.
const messagesPromise = fetchMessages();
messagesPromise.then(messages => renderMessages(messages));
async function fetchMessages() {
const response = await fetch(LOGBOOK_URL);
if (response.ok) {
return await response.json();
} else {
console.error("Couldn't get log messages.", response.status);
return [];
}
}
function renderMessages(messages) {
messagesContainer.innerHTML = "";
messages.forEach(message => {
const messageElement = document.createElement("div");
messageElement.className = "logbook-message";
const timestampElement = document.createElement("time");
const date = Date(message.createdAt);
timestampElement.dateTime = message.createdAt;
timestampElement.innerText = date.toLocaleString();
const authorElement = document.createElement("strong");
authorElement.innerText = message.name;
const saidElement = document.createElement("span");
saidElement.innerText = " said: " + message.message;
const messageBodyElement = document.createElement("p");
messageBodyElement.appendChild(authorElement);
messageBodyElement.appendChild(saidElement);
messageElement.appendChild(timestampElement);
messageElement.appendChild(messageBodyElement);
messagesContainer.appendChild(messageElement);
});
}
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}

13
styles/logbook.css Normal file
View File

@ -0,0 +1,13 @@
#messages-container {
margin-top: 1em;
}
.logbook-message {
background-color: var(--background-color-2);
margin: 0.5em 0;
padding: 0.5em;
}
.logbook-message time {
display: block;
}