Przejdź do głównej treści

Kursy Walut

Morse

Aktualne kursy walut w tabeli pobierane z API nbp.pl z tabeli C. Przelicznik walut uwzględnia kursy kupna i sprzedaży.

Tabela kursów walut

Waluta Kod Kupno Sprzedaż

Przelicznik walut

Kod po stronie przeglądarki

<div id="app">
    <h2 class="my-3 text-center">Tabela kursów walut</h2>
    <table class="table">
        <thead>
            <tr>
                <th>Waluta</th>
                <th>Kod</th>
                <th>Kupno</th>
                <th>Sprzedaż</th>
            </tr>    
        </thead>
        <tbody data-bind="foreach: result">
            <tr>
                <td data-bind="text: currency"></td>
                <td data-bind="text: code"></td>
                <td data-bind="text: bid"></td>
                <td data-bind="text: ask"></td>
            </tr>
        </tbody>
    </table>
    <h2 class="my-3 text-center">Przelicznik walut</h2>
    <div class="row">
        <div class="col">
            <label class="form-label" for="inputValue">Wartość wejściowa</label>
            <input type="number" id="inputValue" class="form-control" data-bind="textInput: inputValue">
        </div>
        <div class="col">
            <label class="form-label" for="inputCurrency">Waluta wejściowa</label>
            <select id="inputCurrency" class="form-select" data-bind="foreach: options, value: selectedOptionFrom">
                <option data-bind="value: $data, text: $data"></option>
            </select> 
        </div>
        <div class="col">
            <label class="form-label" for="outputCurrency">Waluta wyjściowa</label>
            <select id="outputCurrency" class="form-select" data-bind="foreach: options, value: selectedOptionTo">
                <option data-bind="value: $data, text: $data"></option>
            </select> 
        </div>
        <div class="col">
            <label class="form-label" for="outputValue">Wartość wyjściowa</label>
            <input type="text" id="outputValue" class="form-control" data-bind="value: computedOutputValue" readonly>
        </div>
    </div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {    
const ViewModel = function()
{
    const self = this;
    self.result = ko.observable('');
    self.rates = ko.observableArray([]); 
    self.options = ko.observableArray([]);
    self.selectedOptionFrom = ko.observable('');
    self.selectedOptionTo = ko.observable('');
    self.inputValue = ko.observable('1.00');
    //methods
    self.search = function ()
    {
        self.result('');
        self.rates([]);
        self.options([]);
        axios.get('https://api.nbp.pl/api/exchangerates/tables/C?format=json').then(function (response) {            
            const obj = {
                currency: 'polski złoty',
                code: 'PLN',
                bid: 1.0000,
                ask: 1.0000
            };            
            let rates = response.data[0].rates;
            self.result(rates);
            rates.push(obj);
            self.rates(rates);

            for (let i = 0; i < rates.length; i++)
            {
                self.options.push(rates[i].code);
            }
            self.selectedOptionFrom('USD');
            self.selectedOptionTo('PLN');
        }).catch(function (error) {
            BootstrapToast.show({title: 'Błąd', message: 'Błąd pobierania danych', when: 'teraz', type: 'danger', icon: 'bi-x-circle-fill'});
        });
    };
    //computed
    self.computedOutputValue = ko.pureComputed(function() {
        const from = self.selectedOptionFrom();
        const to = self.selectedOptionTo();
        const input = self.inputValue();
                
        if (from == to)
        {
            return parseFloat(input).toFixed(2);  
        }

        let value = 0;
        for (let i = 0; i < self.rates().length; i++)
        {
            let obj = self.rates()[i];
            if (obj.code == from)
            {
                value = input * obj.bid;
                break;
            }
        }
        for (let i = 0; i < self.rates().length; i++)
        {
            let obj = self.rates()[i];
            if (obj.code == to)
            {
                value = value / obj.ask;
                break;
            }
        }
        
        return parseFloat(value).toFixed(2);  
    });
};
const vm = new ViewModel();
ko.applyBindings(vm, document.getElementById('app'));
vm.search();
});    
</script>

Kod po stronie serwera

Brak kodu serwera

Ta aplikacja działa wyłącznie w przeglądarce i nie korzysta z kodu po stronie serwera.

Licencja

## BSD-3-Clause License Agreement
BSD-3-Clause
Сopyright (c) 2026 Dariusz Rorat

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 listopada 2024 4

Kategorie

Technologie

Dziękujemy!
()

Informacja o cookies

Moja strona internetowa wykorzystuje wyłącznie niezbędne pliki cookies, które są wymagane do jej prawidłowego działania. Nie używam ciasteczek w celach marketingowych ani analitycznych. Korzystając z mojej strony, wyrażasz zgodę na stosowanie tych plików. Możesz dowiedzieć się więcej w mojej polityce prywatności.