Esercizio 11 API

This commit is contained in:
2026-02-05 11:57:28 +01:00
parent f4d3e40059
commit 86611fd6e2
37 changed files with 5057 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>App Meteo - GET Request</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>🌤️ App Meteo</h1>
<p class="subtitle">Impara a fare GET request con Fetch API</p>
<div class="instructions">
<h3>📝 Obiettivo dell'Esercizio</h3>
<p>Dovrai scrivere il codice JavaScript per:</p>
<ol>
<li>Recuperare una lista di dati meteo dal server usando <code>fetch()</code></li>
<li>Visualizzare i dati ricevuti nella pagina</li>
<li>Gestire gli errori di rete</li>
</ol>
</div>
<div class="input-group">
<label for="cityInput">Cerca una città (opzionale):</label>
<input type="text" id="cityInput" placeholder="es: Roma">
</div>
<button onclick="fetchWeatherData()">Carica Dati Meteo</button>
<div class="loading" id="loading">
<div class="spinner"></div>
<p>Caricamento dati...</p>
</div>
<div class="result" id="result">
<div class="weather-info" id="weatherInfo"></div>
</div>
<a href="../index.html" class="back-button">← Torna al Hub</a>
</div>
<script src="script.js"></script>
</body>
</html>

View File

@@ -0,0 +1,65 @@
// TODO: Completa questa funzione
async function fetchWeatherData() {
const loading = document.getElementById('loading');
const result = document.getElementById('result');
const weatherInfo = document.getElementById('weatherInfo');
// 1. Mostra il loading spinner
loading.style.display = 'block';
result.classList.remove('show');
try {
// 2. Scrivi qui il fetch per ottenere i dati da:
// http://localhost:3000/api/weather
// 3. Converti la risposta in JSON
// 4. Mostra i dati nella pagina
// SUGGERIMENTO: Usa la struttura vista nel tutorial
// const response = await fetch('...');
// const data = await response.json();
console.log('Scrivi il codice qui!');
throw new Error('Codice non implementato');
} catch (error) {
console.error('Errore:', error);
result.classList.add('show');
weatherInfo.innerHTML = `
<div class="weather-detail error">
<strong>❌ Errore:</strong> ${error.message}
</div>
`;
} finally {
loading.style.display = 'none';
}
}
// Funzione helper per formattare la risposta
function displayWeatherData(data) {
const weatherInfo = document.getElementById('weatherInfo');
const result = document.getElementById('result');
if (Array.isArray(data)) {
// Se è un array
weatherInfo.innerHTML = data.map(item => `
<div class="weather-detail success">
<h3>${item.city || item.name || 'Città sconosciuta'}</h3>
<p>🌡️ Temperatura: ${item.temperature || item.temp || 'N/A'}°C</p>
<p>💨 Umidità: ${item.humidity || 'N/A'}%</p>
<p>☁️ Condizioni: ${item.condition || item.description || 'N/A'}</p>
</div>
`).join('');
} else {
// Se è un singolo oggetto
weatherInfo.innerHTML = `
<div class="weather-detail success">
<h3>${data.city || data.name || 'Città sconosciuta'}</h3>
<p>🌡️ Temperatura: ${data.temperature || data.temp || 'N/A'}°C</p>
<p>💨 Umidità: ${data.humidity || 'N/A'}%</p>
<p>☁️ Condizioni: ${data.condition || data.description || 'N/A'}</p>
</div>
`;
}
result.classList.add('show');
}

View File

@@ -0,0 +1,185 @@
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 40px 20px;
}
.container {
max-width: 600px;
margin: 0 auto;
background: white;
border-radius: 15px;
padding: 40px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
h1 {
color: #333;
text-align: center;
margin-bottom: 10px;
}
.subtitle {
text-align: center;
color: #666;
margin-bottom: 30px;
}
.instructions {
background: #e3f2fd;
border-left: 4px solid #2196F3;
padding: 15px;
margin-bottom: 30px;
border-radius: 4px;
}
.instructions h3 {
color: #1565c0;
margin-bottom: 10px;
}
.instructions ol {
margin-left: 20px;
color: #333;
}
.instructions li {
margin: 8px 0;
}
.input-group {
margin-bottom: 20px;
}
label {
display: block;
color: #333;
font-weight: 600;
margin-bottom: 8px;
}
input {
width: 100%;
padding: 10px;
border: 2px solid #ddd;
border-radius: 4px;
font-size: 1rem;
transition: border-color 0.3s;
}
input:focus {
outline: none;
border-color: #667eea;
}
button {
width: 100%;
padding: 12px;
background: #667eea;
color: white;
border: none;
border-radius: 4px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: all 0.3s;
}
button:hover {
background: #764ba2;
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3);
}
.loading {
display: none;
text-align: center;
color: #667eea;
margin: 20px 0;
}
.spinner {
border: 4px solid #f3f3f3;
border-top: 4px solid #667eea;
border-radius: 50%;
width: 40px;
height: 40px;
animation: spin 1s linear infinite;
margin: 0 auto 10px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.result {
margin-top: 30px;
padding: 20px;
background: #f5f5f5;
border-radius: 8px;
display: none;
}
.result.show {
display: block;
}
.weather-info {
text-align: center;
}
.weather-info h2 {
color: #333;
margin-bottom: 15px;
}
.weather-detail {
background: white;
padding: 10px;
margin: 10px 0;
border-radius: 4px;
border-left: 4px solid #667eea;
}
.error {
background: #ffebee;
border-left-color: #f44336;
color: #c62828;
}
.success {
background: #e8f5e9;
border-left-color: #4caf50;
color: #2e7d32;
}
.back-button {
display: inline-block;
margin-top: 30px;
padding: 10px 20px;
background: #999;
color: white;
text-decoration: none;
border-radius: 4px;
transition: all 0.3s;
}
.back-button:hover {
background: #777;
}
code {
background: #f5f5f5;
padding: 2px 6px;
border-radius: 4px;
font-family: monospace;
color: #d63384;
}