View difference between Paste ID: grNDcvmK and 6Litx1x5
SHOW: | | - or go back to the newest paste.
1
(() => {
2
3
    const BASE_URL = "https://judgetests.firebaseio.com/locations.json";
4
    let TODAY_URL = "https://judgetests.firebaseio.com/forecast/today/{code}.json";
5
6
    const elements = {
7
        locationInput: document.querySelector("#location"),
8
        button: document.querySelector("#submit"),
9
        notificationHeading: document.querySelector('h1.notification')
10
    };
11
12
    const errorHandler = (e) => {
13
        console.dir(e);
14
        elements.notificationHeading.textContent = e.message;
15
    };
16
17
    const jsonMiddleware = (r) => r.json();
18
19
    elements.button.addEventListener("click", getLocationValue);
20
21
    function getLocationValue() {
22
23
        const location = elements.locationInput.value;
24
25
        fetch(BASE_URL)
26
            .then(jsonMiddleware)
27
            .then((data) => {
28
                const { name, code } = data.find((o) => o.name === location);
29
30
                TODAY_URL = TODAY_URL.replace('{code}', code);
31
32
                fetch(TODAY_URL)
33
                    .then(jsonMiddleware)
34
                    .then((data) => {
35
                        console.log(data);
36
                    })
37
                    .catch(errorHandler)
38
            })
39
            .catch(errorHandler)
40
    }
41
})();