Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (() => {
- const BASE_URL = "https://judgetests.firebaseio.com/locations.json";
- let TODAY_URL = "https://judgetests.firebaseio.com/forecast/today/{code}.json";
- const elements = {
- locationInput: document.querySelector("#location"),
- button: document.querySelector("#submit"),
- notificationHeading: document.querySelector('h1.notification')
- };
- const errorHandler = (e) => {
- console.dir(e);
- elements.notificationHeading.textContent = e.message;
- };
- const jsonMiddleware = (r) => r.json();
- elements.button.addEventListener("click", getLocationValue);
- function getLocationValue() {
- const location = elements.locationInput.value;
- fetch(BASE_URL)
- .then(jsonMiddleware)
- .then((data) => {
- const { name, code } = data.find((o) => o.name === location);
- TODAY_URL = TODAY_URL.replace('{code}', code);
- fetch(TODAY_URL)
- .then(jsonMiddleware)
- .then((data) => {
- console.log(data);
- })
- .catch(errorHandler)
- })
- .catch(errorHandler)
- }
- })();
Add Comment
Please, Sign In to add comment