-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (62 loc) · 2.93 KB
/
app.js
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
document.addEventListener("DOMContentLoaded", function () {
const apiKey = "9b47e2ec44dee886bb405198b7a4b6b3"; // OpenWeather API key
const form = document.querySelector("#weather-form");
const locationSelect = document.querySelector("#location");
const temperatureValue = document.querySelector("#temperature");
const weatherConditionValue = document.querySelector("#weather-condition");
const humidityValue = document.querySelector("#humidity");
const windSpeedValue = document.querySelector("#wind-speed");
const cityNameElement = document.querySelector("#city-name");
const shareButton = document.querySelector("#share-button");
// Function to update the sharing data based on the selected city
function updateSharingData(selectedLocation) {
const title = `Check out the weather in ${selectedLocation.toUpperCase()}!`;
const weatherText = `Current weather in ${selectedLocation.toUpperCase()}: [From ForeClime(https://foreclime.vercel.app/)]\n
- Weather Condition: ${weatherConditionValue.textContent.toUpperCase()}
- Temperature: ${temperatureValue.textContent}
- Humidity: ${humidityValue.textContent}
- Wind Speed: ${windSpeedValue.textContent}`;
return {
title,
text: weatherText,
};
}
// Function to handle sharing when the share button is clicked
async function shareWeatherText() {
try {
const selectedLocation = locationSelect.value;
const sharingData = updateSharingData(selectedLocation);
// Web Share API to provide sharing options for text
if (navigator.share) {
await navigator.share(sharingData);
} else {
// Fallback for browsers that don't support Web Share API
alert("Sharing not supported on this browser.");
}
} catch (error) {
console.error("Error sharing weather:", error);
}
}
// Code for fetching weather data from the API
form.addEventListener("submit", function (event) {
event.preventDefault(); // Prevent form submission
const selectedLocation = locationSelect.value;
const apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${selectedLocation}&appid=${apiKey}&units=metric`;
fetch(apiUrl)
.then((response) => response.json())
.then((data) => {
// Update weather information in the HTML
temperatureValue.textContent = `${data.main.temp}°C`;
weatherConditionValue.textContent = data.weather[0].description.toUpperCase();
humidityValue.textContent = `${data.main.humidity}%`;
windSpeedValue.textContent = `${data.wind.speed} km/h`;
// Updating the city name dynamically
cityNameElement.textContent = selectedLocation.toUpperCase();
// Attached the shareWeatherText function to the share button click event
shareButton.addEventListener("click", shareWeatherText);
})
.catch((error) => {
console.error("Error fetching weather data try 30 seconds later:", error);
});
});
});