script.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. const root = document.getElementById("root");
  2. const url = "https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=5";
  3. let data = [
  4. {
  5. "ccy": "USD",
  6. "base_ccy": "UAH",
  7. "buy": "26.00000",
  8. "sale": "26.20000"
  9. },
  10. {
  11. "ccy": "EUR",
  12. "base_ccy": "UAH",
  13. "buy": "29.90000",
  14. "sale": "30.50000"
  15. },
  16. {
  17. "ccy": "RUR",
  18. "base_ccy": "UAH",
  19. "buy": "0.35000",
  20. "sale": "0.38000"
  21. },
  22. {
  23. "ccy": "BTC",
  24. "base_ccy": "USD",
  25. "buy": "58252.9497",
  26. "sale": "64384.8391"
  27. }
  28. ]
  29. let currentCurrency = null;
  30. let bankOperation = "buy";
  31. let defaultValue = 100;
  32. getCurrency();
  33. function getCurrency() {
  34. fetch(url)
  35. .then((res) => {
  36. return res.json();
  37. },
  38. (err) => {
  39. currentCurrency = data[0];
  40. initApp(data, currentCurrency);
  41. console.log("some probloms");
  42. })
  43. .then((json) => {
  44. if (json) {
  45. data = json;
  46. currentCurrency = data[0];
  47. initApp(json, currentCurrency);
  48. }
  49. }
  50. )
  51. }
  52. function initApp(data, currentCurrency) {
  53. const wrapper = document.createElement("div");
  54. wrapper.classList.add("wrapper");
  55. root.appendChild(wrapper);
  56. const informBlockWrapper = document.createElement("div");
  57. informBlockWrapper.classList.add("informBlockWrapper");
  58. wrapper.appendChild(informBlockWrapper);
  59. const currencyBlockWrapper = document.createElement("div");
  60. currencyBlockWrapper.classList.add("currencyBlockWrapper");
  61. informBlockWrapper.appendChild(currencyBlockWrapper);
  62. for (let i = 0; i < 3; i++) {
  63. const currencyBlock = document.createElement("div");
  64. currencyBlock.classList.add("currencyBlock");
  65. currencyBlock.innerText = `${data[i].ccy}`;
  66. currencyBlockWrapper.appendChild(currencyBlock);
  67. currencyBlock.addEventListener("click", chooseCurrency);
  68. }
  69. const exchangeRatesWrapper = document.createElement("div");
  70. exchangeRatesWrapper.classList.add("currencyBlockWrapperValue");
  71. informBlockWrapper.appendChild(exchangeRatesWrapper);
  72. for (let i = 0; i < 3; i++) {
  73. const exchangeRates = document.createElement("div");
  74. exchangeRates.classList.add("currencyBlockValue");
  75. exchangeRatesWrapper.appendChild(exchangeRates);
  76. for (let j = 0; j < 1; j++) {
  77. const buySaleCell = document.createElement("div");
  78. buySaleCell.classList.add("currencyBlockItem");
  79. buySaleCell.innerText = `${Number(data[i].buy).toFixed(2)} / ${Number(data[i].sale).toFixed(2)}`;
  80. exchangeRates.appendChild(buySaleCell);
  81. }
  82. }
  83. const tableHeader = document.createElement("div");
  84. tableHeader.classList.add("tableHeader");
  85. wrapper.appendChild(tableHeader);
  86. const titleHeaderLeft = document.createElement("div");
  87. titleHeaderLeft.classList.add("blockTitle");
  88. titleHeaderLeft.innerText = "Отдаю";
  89. tableHeader.appendChild(titleHeaderLeft);
  90. const titleHeaderRight = document.createElement("div");
  91. titleHeaderRight.classList.add("titleHeaderRight", "blockTitle");
  92. titleHeaderRight.innerText = "Получаю";
  93. tableHeader.appendChild(titleHeaderRight);
  94. const converterWrapper = document.createElement("div");
  95. converterWrapper.classList.add("converterWrapper");
  96. wrapper.appendChild(converterWrapper);
  97. updateTable(currentCurrency)
  98. }
  99. function updateTable() {
  100. if (bankOperation === "buy") {
  101. leftBlock();
  102. centerBlock();
  103. rightBlock();
  104. } else {
  105. if (bankOperation === "sale") {
  106. rightBlock();
  107. centerBlock();
  108. leftBlock();
  109. }
  110. }
  111. }
  112. function leftBlock() {
  113. const converterWrapper = document.querySelector(".converterWrapper");
  114. const converterBlockLeft = document.createElement("div");
  115. converterBlockLeft.classList.add("converterBlock");
  116. converterWrapper.appendChild(converterBlockLeft);
  117. const converterBlokTitleSell = document.createElement("div");
  118. converterBlokTitleSell.classList.add("converterBlockTitle");
  119. converterBlokTitleSell.innerText = `${currentCurrency.ccy}`;
  120. converterBlockLeft.appendChild(converterBlokTitleSell);
  121. const converterCurrencySell = document.createElement("input");
  122. converterCurrencySell.classList.add("converterCurrency", "sell");
  123. converterCurrencySell.value = defaultValue;
  124. converterBlockLeft.appendChild(converterCurrencySell);
  125. converterCurrencySell.addEventListener("keyup", inputHandler);
  126. const converterBlockInfoSell = document.createElement("div");
  127. converterBlockInfoSell.classList.add("converterBlockInfo");
  128. converterBlockInfoSell.innerText = `1 ${currentCurrency.ccy} = ${Number(currentCurrency[bankOperation]).toFixed(2)} UAH`;
  129. converterBlockLeft.appendChild(converterBlockInfoSell);
  130. }
  131. function centerBlock() {
  132. const converterWrapper = document.querySelector(".converterWrapper");
  133. const converterBlockCenter = document.createElement("div");
  134. converterBlockCenter.classList.add("converterBlockCenter");
  135. converterWrapper.appendChild(converterBlockCenter);
  136. const icon = document.createElement("i");
  137. icon.classList.add("fas");
  138. icon.classList.add("fa-sync-alt");
  139. icon.addEventListener("click", swapCurrency);
  140. converterBlockCenter.appendChild(icon);
  141. }
  142. function rightBlock() {
  143. const converterWrapper = document.querySelector(".converterWrapper");
  144. const converterBlockRight = document.createElement("div");
  145. converterBlockRight.classList.add("converterBlock");
  146. converterWrapper.appendChild(converterBlockRight);
  147. const converterBlokTitleBuy = document.createElement("div");
  148. converterBlokTitleBuy.classList.add("converterBlockTitle");
  149. converterBlokTitleBuy.innerText = "UAH";
  150. converterBlockRight.appendChild(converterBlokTitleBuy);
  151. const converterCurrencyBuy = document.createElement("input");
  152. converterCurrencyBuy.classList.add("converterCurrency", "buy");
  153. converterCurrencyBuy.value = defaultValue * Number(currentCurrency[bankOperation]);
  154. converterCurrencyBuy.addEventListener("keyup", inputHandler);
  155. converterBlockRight.appendChild(converterCurrencyBuy);
  156. const converterBlockInfoBuy = document.createElement("div");
  157. converterBlockInfoBuy.classList.add("converterBlockInfo");
  158. converterBlockInfoBuy.innerText = `1 UAH = ${(1 / Number(currentCurrency[bankOperation])).toFixed(2)} ${currentCurrency.ccy}`;
  159. converterBlockRight.appendChild(converterBlockInfoBuy);
  160. }
  161. function chooseCurrency(event) {
  162. switch (event.target.innerText) {
  163. case "USD":
  164. currentCurrency = data[0];
  165. break
  166. case "EUR":
  167. currentCurrency = data[1];
  168. break;
  169. case "RUR":
  170. currentCurrency = data[2];
  171. break;
  172. }
  173. const converterWrapper = document.querySelector(".converterWrapper")
  174. converterWrapper.innerHTML = "";
  175. updateTable(currentCurrency);
  176. }
  177. function inputHandler(event) {
  178. let buyInput = document.querySelector(".buy");
  179. let sellInput = document.querySelector(".sell");
  180. let value = event.target.value;
  181. if (event.target.classList.contains("sell")) {
  182. if (isNaN(+event.target.value)) {
  183. buyInput.value = "Wrong value";
  184. } else {
  185. buyInput.value = (Number(value) * Number(currentCurrency[bankOperation])).toFixed(2);
  186. }
  187. }
  188. if (event.target.classList.contains("buy")) {
  189. if (isNaN(+event.target.value)) {
  190. sellInput.value = "Wrong value";
  191. } else {
  192. sellInput.value = (Number(value) / Number(currentCurrency[bankOperation])).toFixed(2);
  193. }
  194. }
  195. }
  196. function swapCurrency() {
  197. const converterWrapper = document.querySelector(".converterWrapper");
  198. converterWrapper.innerHTML = "";
  199. if (bankOperation === "buy") {
  200. bankOperation = "sale";
  201. } else {
  202. bankOperation = "buy";
  203. }
  204. updateTable();
  205. }