Skip to content

Commit

Permalink
changed desktop file name
Browse files Browse the repository at this point in the history
  • Loading branch information
Reptartheman committed Dec 28, 2024
1 parent b729d4d commit ebdff85
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 95 deletions.
Binary file modified .DS_Store
Binary file not shown.
142 changes: 47 additions & 95 deletions Assets/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,105 +8,57 @@ let cityList = document.querySelector(".cityList");
let searchedCities = [];


const getCurrentTemperature = () => {
const searchInput = cityInput.value.trim();
const currentTemp = `https://api.openweathermap.org/data/2.5/weather?q=${searchInput}&appid=${apiKey}&units=imperial`
fetch(currentTemp).then(res => res.json())
.then(data => {
console.log(data);
return data.main;
})
.then(data => `${displayDataResult("temperature", Math.floor(data.temp))}`)
.catch(err => err);
const fetchWeatherData = async (searchInput, apiKey) => {
const baseUrl = `https://api.openweathermap.org/data/2.5/weather?q=${searchInput}&appid=${apiKey}&units=imperial`;

try {
const response = await fetch(baseUrl);
const data = await response.json();
return {
temperature: Math.floor(data.main.temp),
cityName: data.name,
weather: data.weather[0],
icon: `https://openweathermap.org/img/wn/${data.weather[0].icon}.png`
};
} catch (err) {
console.error('Error fetch err');
return null;
}
};

const getCityName = () => {
const searchInput = cityInput.value.trim();
const currentCity = `https://api.openweathermap.org/data/2.5/weather?q=${searchInput}&appid=${apiKey}&units=imperial`
fetch(currentCity).then(res => res.json())
.then(data => data.name)
.then(name => `${displayDataResult("location", name)}`)
.catch(err => err);
}
const displayWeatherInfo = (weatherData) => {
const displayMap = {
temperature: (temp) => `${temp}℉`,
location: (name) => `Current conditions for ${name}`,
weatherDescription: (desc) => desc,
weatherIcon: (icon) => {
const weatherIcon = document.getElementById("weatherIcon");
weatherIcon.setAttribute("src", icon);
return weatherIcon;
}
};

Object.entries(weatherData).forEach(([key, value]) => {
const element = document.getElementById(key);
if (element) {
element.innerHTML = displayMap[key](value);
}
});
};

const getWeatherDescription = () => {
const searchInput = cityInput.value.trim();
const currentWeather = `https://api.openweathermap.org/data/2.5/weather?q=${searchInput}&appid=${apiKey}&units=imperial`
fetch(currentWeather).then(res => res.json())
.then(data => data.weather)
.then(weather => `${displayDataResult("weatherDescription", weather[0].description)}`)
.catch(err => err);
}

const getWeatherIcon = () => {
searchButton.addEventListener('click', async (e) => {
e.preventDefault();
const searchInput = cityInput.value.trim();
const currentIcon = `https://api.openweathermap.org/data/2.5/weather?q=${searchInput}&appid=${apiKey}&units=imperial`
fetch(currentIcon).then(res => res.json())
.then(data => data.weather)
.then(weather => {
const weatherIcon = document.getElementById("weatherIcon")
const iconUrl = `https://openweathermap.org/img/wn/${weather[0].icon}.png`;
weatherIcon.setAttribute("src", iconUrl)
return `${displayDataResult("weatherIcon", weatherIcon)}`
})
.catch(err => err);
}


const displayDataResult = (elementId, info) => {
const displayElement = document.getElementById(elementId);
if (elementId === "temperature") {
displayElement.innerHTML = `${info}℉`;
} else if (elementId === "date") {
displayElement.innerHTML = `Today is: ${info}`;
} else if (elementId === "location") {
displayElement.innerHTML = `Current conditions for ${info} `;
} else if (elementId === "weatherDescription") {
displayElement.innerHTML = `${info}`;
} else if (elementId === "weatherIcon") {
displayElement.innerHTML = `${info}`;
}
return displayElement;
}


searchButton.addEventListener('click', (e) => {
e.preventDefault()
getCurrentTemperature();
getCityName();
getWeatherDescription();
getWeatherIcon();
})
displayDataResult("date", `${todaysDate}`);

/* function weatherSearch(event) {
event.preventDefault();
weatherDisplay.innerHTML="";
const forecast = `https://api.openweathermap.org/data/2.5/forecast?q=${searchInput}&appid=${apiKey}&units=imperial`
fetch(forecast).then(function (response){
if(response.ok){
response.json().then(function (data) {
fiveDay(data)
});
}
const weatherData = await fetchWeatherData(searchInput, apiKey);

if (weatherData) {
displayWeatherInfo({
temperature: weatherData.temperature,
location: weatherData.cityName,
weatherDescription: weatherData.weather.description,
weatherIcon: weatherData.icon
});
searchedCities.push(searchInput)
searchBarText.value = "";
storeCities();
renderPast();
} else {
window.alert("Please enter a city name");
return;
}
}; */



}
});

0 comments on commit ebdff85

Please sign in to comment.