This commit is contained in:
Ulises Gascón
2024-02-07 12:29:54 +01:00
committed by Ulises Gascon
parent 91ba1f5054
commit 9dd233c371
143 changed files with 68070 additions and 1 deletions
+66
View File
@@ -0,0 +1,66 @@
// == Selectors ==
const whispers = document.getElementById('whispers')
const whisperCreateButton = document.getElementById('whisper-create')
// == Event Listeners ==
whispers.addEventListener('click', event => {
if(event.target.tagName === 'BUTTON') {
const button = event.target
const article = event.target.closest('article')
const action = button.dataset.action
const id = article.dataset.id
const message = article.querySelector('p').innerText
if(action === 'edit') requestUserEdit(id, message)
if(action === 'delete') requestUserDelete(id)
}
})
whisperCreateButton.addEventListener('click', event => {
const message = prompt("What's your whisper?")
if(message) {
createWhisper(message)
.then(refreshAllUI)
}
})
// === Functions ==
// -- API --
const fetchAllWhispers = () => fetch('http://localhost:3000/api/v1/whisper').then((response) => response.json())
const deleteWhisper = (id) => fetch(`http://localhost:3000/api/v1/whisper/${id}`, { method: 'DELETE' })
const updateWhisper = (id, message) => fetch(`http://localhost:3000/api/v1/whisper/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message }) })
const createWhisper = (message) => fetch('http://localhost:3000/api/v1/whisper', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message }) })
// -- UI --
const refreshWhispers = data => whispers.innerHTML = data
.reverse()
.map(whisper => {
return `
<article data-id="${whisper.id}">
<div class="actions">
<button data-action="edit">✏️</button>
<button data-action="delete">❌</button>
</div>
<p>${whisper.message}</p>
</article>`
}).join('')
const refreshAllUI = () => fetchAllWhispers().then(refreshWhispers)
const requestUserEdit = (id, message) => {
const newMessage = prompt("Edit the Whisper", message);
if(newMessage && newMessage !== message) {
updateWhisper(id, newMessage)
.then(refreshAllUI)
}
console.log("Request User Edit", id, message)
}
const requestUserDelete = (id) => {
const confirmation = confirm("Are you sure you want to delete this whisper?");
if(confirmation) {
deleteWhisper(id)
.then(refreshAllUI)
}
}
// == Initialization ==
refreshAllUI()
+25
View File
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Whispering | Home</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<nav class="menu">
<ul>
<li><a href="/about">About</a></li>
<li><a href="#">Whispering</a></li>
</ul>
</nav>
<main>
<button type="button" class="block" id="whisper-create">Spread a whisper 🤭</button>
<div id="whispers"></div>
</main>
<script src="app.js"></script>
</body>
</html>
Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

+110
View File
@@ -0,0 +1,110 @@
html,
body {
font-family: sans-serif;
margin: 0;
padding: 0;
background-color: #fff;
}
.menu ul {
list-style: none;
margin: 0;
padding: 0;
width: 100%;
height: 50px;
background-color: #343e8b;
}
.menu li {
float: right;
margin-right: 4%;
font-weight: bold;
line-height: 50px;
}
.menu li:last-child {
float: left;
margin-left: 4%;
}
.menu a {
color: white;
text-decoration: none;
}
main {
max-width: 600px;
margin: 0 auto;
padding: 20px;
text-align: center;
display: flex;
flex-direction: column;
align-items: center;
}
img {
max-width: 100%;
height: auto;
}
figcaption {
font-size: 0.8em;
}
h1 {
font-size: 2em;
line-height: 1.5em;
}
h2 {
font-size: 1.5em;
line-height: 1.5em;
}
p {
font-size: 1.2em;
line-height: 1.5em;
}
#whispers {
border-top: 1px solid #ccc;
margin-top: 20px;
}
article {
background: #f8f6f6;
border: 2px solid #343e8b;
margin: 1.5em 10px;
padding: 0 10px;
border-radius: 5px;
display: flex;
flex-direction: column;
}
article p {
display: inline;
}
article .actions {
margin-top: 10px;
display: flex;
justify-content: flex-end;
}
article .actions > button {
cursor: pointer;
margin: 3px;
border: none;
background-color: #f8f6f6;
}
.block {
display: block;
width: 60%;
border: none;
background-color: #343e8b;
padding: 0.5em 10px;
font-size: 16px;
cursor: pointer;
text-align: center;
color: white;
border-radius: 5px;
}