script

 avatar
unknown
typescript
2 years ago
2.7 kB
5
Indexable
interface Station {
    stacja: string;
    temperatura?: string;
    cisnienie?: string;
    wilgotnosc_wzgledna?: string;
    stan_wody?: string;
    temperatura_wody?: string;
    zjawisko_lodowe?: string;
    typ: "weather" | "hydro";
    class?: string;
}

let stacjeIMGW: Station[] = [];

function szukaj(inp: string) {
    let nazwa = inp.toLowerCase();

    //tabela po wyfiltrowaniu
    let noweDane = stacjeIMGW.filter(e => e.stacja.toLowerCase().includes(nazwa));

    let stacja = document.querySelector('.stations')!;
    stacja.innerHTML = "";
    noweDane.forEach(st => {
        let linia1 = '';
        let linia2 = '';
        let linia3 = '';
        if (st.typ == 'weather') {
            linia1 = `Temperatura: ${st.temperatura}<sup>°C</sup>`;
            linia2 = `Ciśnienie: ${st.cisnienie ? (st.cisnienie + "hPa") : 'brak'}`;
            linia3 = `Wilgotność: ${st.wilgotnosc_wzgledna}%`;
        }
        else if (st.typ == 'hydro') {
            linia1 = `Stan wody: ${st.stan_wody}cm`;
            linia2 = `Temperatura: ${st.temperatura_wody ? (st.temperatura_wody + "<sup>°C</sup>") : 'brak'}`;
            linia3 = `Oblodzenie ${st.zjawisko_lodowe}mm`;
        }
        stacja.innerHTML +=
            `<div class="box__stations_${st.typ} ${st.class ?? ''}">
            <h2>${st.stacja}</h2>
            <div>${linia1}</div>
            <div>${linia2}</div>
            <div>${linia3}</div>
        </div>`;
    });
}

fetch("https://danepubliczne.imgw.pl/api/data/synop/")
    .then(e => e.json())
    .then((e: Station[]) => {
        let min = 100;
        let max = -100;
        let mini: Station | undefined;
        let maxi: Station | undefined;
        e.forEach(dane => {
            dane.class = "";
            dane.typ = 'weather';
            if (parseFloat(dane.temperatura ?? '') < min) {
                min = parseFloat(dane.temperatura ?? '');
                mini = dane;
            }
            if (parseFloat(dane.temperatura ?? '') > max) {
                max = parseFloat(dane.temperatura ?? '');
                maxi = dane;
            }
            stacjeIMGW.push(dane);
        });

        if (mini) {
            mini.class = "low";
        }
        if (maxi) {
            maxi.class = "hi";
        }

        szukaj("");
    });

fetch("https://danepubliczne.imgw.pl/api/data/hydro/")
    .then(e => {
        return e.json();
    })
    .then((listaStacji: Station[]) => {
        listaStacji.forEach(stacja => {
            stacja.typ = "hydro";
            stacjeIMGW.push(stacja);
        });
    });
Editor is loading...