rinomina esercizi js

This commit is contained in:
2026-02-12 18:36:35 +01:00
parent f0b6b85b36
commit 60878cf770
150 changed files with 16 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Esercizio 4 - Ricerca Post</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<a href="../index.html" style="position: absolute; top: 20px; left: 20px; text-decoration: none; color: #555; font-weight: bold;">← Dashboard</a>
<div class="app-container">
<h1>🔍 Ricerca Post</h1>
<p class="subtitle">Fetch multipli con filtri incrociati</p>
<!-- SEZIONE CONFIGURAZIONE -->
<div class="config-box">
<label for="keyword">Ricerca per parola chiave:</label>
<div class="input-group">
<input type="text" id="keyword" placeholder="Es: JavaScript, CSS, React...">
<button id="btnSearch">Cerca</button>
</div>
</div>
<!-- LOADING -->
<div id="loading" class="loading nascosto">
⏳ Caricamento...
</div>
<!-- RISULTATI -->
<div id="results" class="results-container"></div>
</div>
<script src="script.js"></script>
</body>
</html>

View File

@@ -0,0 +1,83 @@
// ⚠️ COMPILARE E CONTROLLARE PRIMA DI INIZIARE ⚠️
const BASE_URL = 'http://localhost:5000/api';
const keyword = document.querySelector('#keyword');
const loading = document.querySelector('#loading');
const results = document.querySelector('#results');
const btnSearch = document.querySelector('#btnSearch');
/**
* FUNZIONE: Crea la card HTML per un singolo post con autore a fianco
*
* Funzione già fatta - non modificare
*/
function creaElementoPost(post, autore) {
results.innerHTML += `
<div class="post-row">
<div class="row-author">
<img src="${autore.avatar}" alt="Avatar" class="row-avatar">
<div class="row-author-info">
<strong>${autore.nome} ${autore.cognome}</strong>
<small>${autore.email}</small>
</div>
</div>
<div class="row-post">
<div class="post-title">${post.titolo}</div>
<div class="post-preview">${post.contenuto}</div>
<div class="post-meta">
<span>📅 ${post.data}</span>
<span>❤️ ${post.likes} likes</span>
</div>
</div>
</div>
`;
}
/**
* FUNZIONE: Gestione errori
*
* Mostra un messaggio di errore e logga in console
* Funzione già fatta - non modificare
*/
function handleError(message) {
results.innerHTML = `
<div class="error">
<strong>❌ ${message}</strong>
</div>
`;
console.error('Errore:', message);
}
/**
* ESERCIZIO 5: Ricerca Post con Autori
*
* Devi completare questa funzione:
* 1. Leggi la parola chiave dall'input
* 2. Svuota il contenuto dei risultati (results.innerHTML = '')
* 3. Valida che non sia vuota (usa trim)
* 4. Mostra lo spinner di caricamento
* 5. Apri un blocco try/catch
* 6. Fai una GET a /posts
* 7. Filtra i post dove titolo O contenuto contiene la keyword
* (ricorda minuscole per la ricerca)
* 8. Se nessun post trovato, usa handleError() e return
* 9. Per ogni post trovato:
* - Prendi l'id autore e fai una GET a /users?id=[id]
* - Crea un nuovo elemento della lista dei risultati con creaElementoPost(post, autore)
* 10. Gestisci gli errori con handleError()
* 11. Nascondi lo spinner di caricamento
*/
async function fetchPostsByKeyword() {
//TODO
}
// COLLEGA IL BOTTONE
btnSearch.addEventListener('click', fetchPostsByKeyword);
// PERMETTI ENTER
keyword.addEventListener('keypress', (e) => {
if (e.key === 'Enter') fetchPostsByKeyword();
});

View File

@@ -0,0 +1,268 @@
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
display: flex;
justify-content: center;
align-items: flex-start;
min-height: 100vh;
margin: 0;
padding: 20px;
}
.app-container {
background: white;
width: 100%;
max-width: 700px;
padding: 30px;
border-radius: 15px;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
margin-top: 20px;
}
h1 {
color: #333;
text-align: center;
margin: 0 0 10px 0;
font-size: 1.8rem;
}
.subtitle {
color: #666;
text-align: center;
margin: 0 0 30px 0;
font-size: 1rem;
}
.config-box {
background: #f9f9f9;
padding: 20px;
border-radius: 10px;
margin-bottom: 25px;
}
.config-box label {
display: block;
color: #333;
font-weight: 600;
margin-bottom: 12px;
}
.input-group {
display: flex;
gap: 10px;
}
.input-group input,
.input-group button {
padding: 12px 15px;
border-radius: 6px;
border: 1px solid #ddd;
font-size: 1rem;
}
.input-group input {
flex: 1;
border-color: #ddd;
}
.input-group input:focus {
outline: none;
border-color: #007bff;
box-shadow: 0 0 5px rgba(0, 123, 255, 0.2);
}
.input-group button {
background: #007bff;
color: white;
border: none;
font-weight: bold;
cursor: pointer;
padding: 12px 25px;
}
.input-group button:hover {
background: #0056b3;
}
.loading {
text-align: center;
color: #666;
font-size: 1rem;
padding: 30px;
animation: pulse 1s infinite;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.6; }
}
.results-container {
display: flex;
flex-direction: column;
gap: 15px;
}
.post-row {
background: white;
border-radius: 10px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
display: flex;
animation: slideIn 0.3s ease-out;
min-height: 100px;
border: 1px solid #f0f0f0;
}
.row-author {
background: #f5f5f5;
color: #333;
padding: 15px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 130px;
flex-shrink: 0;
gap: 8px;
border-right: 1px solid #e0e0e0;
}
.row-avatar {
width: 48px;
height: 48px;
border-radius: 50%;
object-fit: cover;
border: 2px solid #ddd;
}
.row-author-info {
text-align: center;
width: 100%;
}
.row-author-info strong {
display: block;
font-size: 0.85rem;
margin-bottom: 3px;
word-break: break-word;
color: #333;
font-weight: 600;
}
.row-author-info small {
font-size: 0.7rem;
color: #999;
word-break: break-word;
}
.row-post {
flex: 1;
padding: 15px;
display: flex;
flex-direction: column;
justify-content: center;
}
.author-section {
background: #f9f9f9;
border-radius: 10px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
animation: slideIn 0.3s ease-out;
}
@keyframes slideIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.author-header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px;
display: flex;
align-items: center;
gap: 15px;
}
.author-avatar {
width: 60px;
height: 60px;
border-radius: 50%;
object-fit: cover;
border: 3px solid white;
}
.author-info h3 {
margin: 0 0 5px 0;
font-size: 1.2rem;
}
.author-info p {
margin: 0;
font-size: 0.9rem;
opacity: 0.9;
}
.posts-list {
padding: 15px;
}
.post-item {
background: white;
padding: 12px;
margin-bottom: 10px;
border-radius: 6px;
border-left: 3px solid #667eea;
}
.post-item:last-child {
margin-bottom: 0;
}
.post-title {
font-weight: 600;
color: #333;
margin: 0 0 5px 0;
}
.post-preview {
color: #666;
font-size: 0.9rem;
margin: 0 0 8px 0;
line-height: 1.4;
}
.post-meta {
display: flex;
gap: 15px;
font-size: 0.8rem;
color: #999;
}
.empty {
text-align: center;
color: #999;
padding: 40px 20px;
font-size: 1.1rem;
}
.error {
background: #fee;
color: #c00;
padding: 15px;
border-radius: 6px;
border-left: 4px solid #c00;
font-weight: 500;
}
.nascosto {
display: none;
}