Skip to content

Commit

Permalink
Fallback while weather api doesn't have key
Browse files Browse the repository at this point in the history
  • Loading branch information
travissouthard committed Nov 22, 2024
1 parent a7f0f65 commit 4bed100
Showing 1 changed file with 36 additions and 26 deletions.
62 changes: 36 additions & 26 deletions bike_camping_PHL/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,29 @@ const checkLowest = (temp) => temp < 40; //Below 40degF, most people need extra
const checkHighest = (temp) => temp > 80; //Above 80degF, most people need lighter gear
const checkWeatherId = (id) => id < "800"; //IDs with "800" or up are clear or cloudy, all under "800" have some adverse weather, check OpenWeather API documentation for details

$(() => { // On page load
$(() => {
// On page load
//Event handlers
const crossout = (event) => {
$(event.currentTarget).toggleClass("checked");
}
};
const renderCustomItems = () => {
let $customItem = $("<li>").text(customItems[customItems.length-1]).addClass("unchecked");
let $customItem = $("<li>")
.text(customItems[customItems.length - 1])
.addClass("unchecked");
$("#Custom").append($customItem);
$customItem.on("click", crossout);
}
};

//Event listeners
$("form").on("submit", (event) => { //Custom item submissions in form
$("form").on("submit", (event) => {
//Custom item submissions in form
event.preventDefault();
const $inputValue = $("#input").val();
customItems.push($inputValue);
renderCustomItems();
$(event.currentTarget).trigger('reset');
})
$(event.currentTarget).trigger("reset");
});

//Builds weather cards
const buildWeatherCards = (weatherData) => {
Expand All @@ -51,7 +55,10 @@ $(() => { // On page load
//Make elements with data
let $weatherCard = $("<div>").addClass("weather-card");
let $title = $("<h4>").text(dayList[i]);
let $icon = $("<img>").attr("src", `https://openweathermap.org/img/wn/${iconList[i]}@2x.png`);
let $icon = $("<img>").attr(
"src",
`https://openweathermap.org/img/wn/${iconList[i]}@2x.png`
);
let $high = $("<p>").text(`High: ${highs[i]}°(F)`);
let $low = $("<p>").text(`Low: ${lows[i]}°(F)`);
//Put elements in place
Expand All @@ -61,22 +68,22 @@ $(() => { // On page load
$weatherCard.append($low);
$(".container").append($weatherCard);
}
}
};



//Generates the checklist for the page
const generateChecklist = () => {
$(".checklist").empty(); //To make room for button presses
for (let category in checklist.base) {
//Adds list items from the checklist called
const appendList = (list) => {
for (let i = 0; i < list[category].length; i++) {
let $item = $("<li>").addClass(category).text(list[category][i]);
let $item = $("<li>")
.addClass(category)
.text(list[category][i]);
$item.on("click", crossout);
$category.append($item);
}
}
};
//Makes a ul to hold each category of the checklists
let $category = $("<ul>").attr("id", category);

Expand All @@ -86,41 +93,44 @@ $(() => { // On page load

//Conditionals to customize the list with the weather
//If the lowest low-temperature is below 40deg add the cold list
if (lows.some(checkLowest)) {
if (lows.length === 0 || lows.some(checkLowest)) {
appendList(checklist.cold);
}
//If the highest high-temperature is above 90deg add the hot list
if (highs.some(checkHighest)) {
//If the highest high-temperature is above 90deg add the hot list
if (lows.length === 0 || highs.some(checkHighest)) {
appendList(checklist.hot);
}
//If it is other than clear or cloudy, bring the rain list
if (highs.some(checkWeatherId)) {
if (lows.length === 0 || highs.some(checkWeatherId)) {
appendList(checklist.rain);
}
//Finishes with the base checklist
appendList(checklist.base);
$(".checklist").append($category);
}
}
};

//Fetches the weather from openweathermap.org
//This also calls buildWeatherCards and generateChecklist, without the weather, this does not really work. A breakproof way to still make a basic list should go in the error catch.
//This also calls buildWeatherCards and generateChecklist, without the weather, this does not really work. A breakproof way to still make a basic list should go in the error catch.
const getWeather = (lat, long) => {
const weatherApiKey = "&appid=d175d89ebe6588949bced83b103d7c13";
const weatherURL = `https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${long}&exclude=current,minutely,hourly&units=imperial`;
const weatherQuery = weatherURL + weatherApiKey;
$.ajax({
method: "GET",
url: weatherQuery,
datatype: "jsonp"
datatype: "jsonp",
}).done((weatherData) => {
buildWeatherCards(weatherData);
generateChecklist();
}), (error) => {
console.log(error);
}
}
}),
(error) => {
console.log(error);
};
};

//Calls each of these as the page loads in
getWeather(philaLat, philaLong);
});
// getWeather(philaLat, philaLong);

generateChecklist();
});

0 comments on commit 4bed100

Please sign in to comment.