Конвертер валют
Имеется скрипт который автоматически переводит разные валют
<div class="container">
<h2>Конвертер валют</h2>
<p>Последнее обновление: <b class="date">26.03.2020 18:35:57</b></p>
<div class="group">
<input type="number" class="first-input">
<select class="first-select"></select>
<input type="number" class="second-input">
<select class="second-select"></select>
</div>
Пример тут
Мой НБРБ не умеет выгружать в JSON. Все валюты он отображает в https://www.nbrb.by/api/exrates/rates?periodicity=0 Как модернизировать текущий скрипт под нацбанк РБ
UPD. Переделал значения, но не работает
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js"></script>
<script>
const container = document.querySelector('.container .group');
const currencyDate = document.querySelector('.container .date');
const firstInput = container.querySelector('.first-input');
const secondInput = container.querySelector('.second-input');
const firstSelect = container.querySelector('.first-select');
const secondSelect = container.querySelector('.second-select');
firstInput.addEventListener('input', () => firstHandle());
secondInput.addEventListener('input', () => secondHandle());
firstSelect.addEventListener('change', () => firstHandle());
secondSelect.addEventListener('change', () => secondHandle());
const firstHandle = () => {
const value = firstInput.value;
const currency = firstSelect.options[firstSelect.selectedIndex].dataset.currency;
secondInput.value = value * currency;
}
const secondHandle = () => {
const value = secondInput.value;
const currency = secondSelect.options[secondSelect.selectedIndex].dataset.currency;
firstInput.value = value * currency;
}
const handleInput = () => {
const first = firstSelect.options[firstSelect.selectedIndex].dataset.currency;
const second = secondSelect.options[secondSelect.selectedIndex].dataset.currency;
firstInput.value = 1;
firstHandle();
};
const handleRequest = ({ data }) => {
const date = new Date(data.PreviousDate);
const optionsDateFormat = {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: 'numeric',
minute: 'numeric',
second: 'numeric'
};
currencyDate.innerHTML = date.toLocaleDateString('ru-RU', optionsDateFormat);
Object.values(data.Valute).forEach(valute => {
const option = document.createElement('option');
const option2 = document.createElement('option');
option.innerHTML = valute.Name;
option.dataset.currency = Cur_OfficialRate;
if (Cur_OfficialRate === 'EUR')
option.selected = true;
option2.innerHTML = valute.Name;
option2.dataset.currency = Cur_OfficialRate;
if (Cur_OfficialRate === 'BYN')
option2.selected = true;
firstSelect.append(option);
secondSelect.append(option2);
});
handleInput();
}
const options = {
headers: { 'Content-Type': 'application/json' }
}
axios.get('https://www.nbrb.by/api/exrates/rates?periodicity=0', options)
.then(handleRequest).catch();
</script>