index.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <link href="style.css" rel="stylesheet" />
  8. <title>js/01</title>
  9. </head>
  10. <body>
  11. <div class="header"></div>
  12. <div class="container">
  13. <div class="alerts"></div>
  14. <div class="container-form"></div>
  15. </div>
  16. <script>
  17. let alerts = [];
  18. let exchangeRates = {};
  19. const _alert = (el) => `
  20. <div class="alert ${el.type}">${el.text}</div>
  21. `;
  22. const currency = (name, selected) => {
  23. return selected
  24. ? `<option value="${name}" selected>${name}</option>`
  25. : `<option value="${name}">${name}</option>`;
  26. };
  27. function updateCurrencyBlock() {
  28. let currencyBlock = document.querySelector("#currency");
  29. currencyBlock.innerHTML = "";
  30. Object.keys(exchangeRates)
  31. .filter((key) => key !== "USD")
  32. .forEach((el) => {
  33. let isSelected = el === "UAH";
  34. currencyBlock.insertAdjacentHTML("beforeend", currency(el, isSelected));
  35. });
  36. }
  37. const exchangeForm = `
  38. <form action="" class="form" >
  39. <div class="form-body">
  40. <label for="">Currency:</label>
  41. <select type="number" name="currency" class="form-input" id = "currency" onchange = "calculate()">
  42. </select>
  43. <label for="#amount">Amount</label>
  44. <input type="number" name="amount" id="amount" class="form-input" id="" value="1" oninput = "calculate()"/>
  45. </div>
  46. <div class="form-footer">
  47. <label for="USD">USD</label>
  48. <input type="text" name="USD" placeholder="0" id="USD" class="form-input" oninput = "reverseCalculate()"/>
  49. </div>
  50. </form>
  51. `;
  52. const authForm = `
  53. <form action="" onsubmit="auth(event)" class="form">
  54. <div class="form-body">
  55. <input type="text" placeholder="Login" name="login" class="form-input" />
  56. <input type="password" placeholder = "Password" name="password" class="form-input" />
  57. <button type="submit" class="btn btn-login">Login</button>
  58. </div>
  59. </form>
  60. `;
  61. const btnLogout = `
  62. <button class="btn btn-logout" onclick="logout()">Logout</button>
  63. `;
  64. const credentials = { login: "admin", password: "qwerty" };
  65. document.querySelector(".container-form").innerHTML = "loading...";
  66. fetch("https://open.er-api.com/v6/latest/USD", {
  67. method: "GET",
  68. })
  69. .then((r) => r.json())
  70. .then(({ rates }) => {
  71. exchangeRates = rates;
  72. })
  73. .then(() => {
  74. checkIsAuth();
  75. });
  76. function checkIsAuth() {
  77. const isAuth = JSON.parse(localStorage.getItem("isAuth"));
  78. if (isAuth === true) {
  79. document.querySelector("div.header").innerHTML = btnLogout;
  80. document.querySelector("div.container-form").innerHTML = exchangeForm;
  81. updateCurrencyBlock();
  82. removeAlert("", "error");
  83. calculate();
  84. } else {
  85. document.querySelector("div.container-form").innerHTML = authForm;
  86. document.querySelector("div.header").innerHTML = "";
  87. }
  88. }
  89. function updateAlerts() {
  90. let alerts_block = document.querySelector("div.alerts");
  91. alerts_block.innerHTML = "";
  92. alerts.forEach((el) => {
  93. alerts_block.insertAdjacentHTML("beforeend", _alert(el));
  94. });
  95. }
  96. function auth(e) {
  97. e.preventDefault();
  98. let login = e.target.elements.login.value;
  99. let password = e.target.elements.password.value;
  100. login ? removeAlert("Login is required", "error") : addAlert("Login is required", "error");
  101. password ? removeAlert("Password is required", "error") : removeAlert("Password is required", "error");
  102. if (credentials.login !== login || credentials.password !== password) {
  103. localStorage.setItem("isAuth", false);
  104. addAlert("The username or password is incorrect", "error");
  105. return;
  106. }
  107. localStorage.setItem("isAuth", true);
  108. checkIsAuth();
  109. addAlert("Successfull auth", (type = "success"), 5000);
  110. return;
  111. }
  112. function addAlert(text, type, timeout) {
  113. !alerts.some((el) => el.text === text && el.type === type)
  114. ? alerts.push({ text: text, type: type })
  115. : null;
  116. updateAlerts();
  117. if (timeout) {
  118. setTimeout(() => removeAlert(text, type), timeout);
  119. }
  120. }
  121. function removeAlert(text = "", type = "") {
  122. if (text === "" && type === "") {
  123. alerts = [];
  124. }
  125. if (text === "" && type !== "") {
  126. alerts = alerts.filter((el) => el.type !== type);
  127. }
  128. if (text !== "" && type === "") {
  129. alerts = alerts.filter((el) => el.text !== text);
  130. }
  131. if (text !== "" && type !== "") {
  132. alerts = alerts.filter((el) => el.text !== text || el.type !== type);
  133. }
  134. updateAlerts();
  135. }
  136. function logout() {
  137. localStorage.setItem("isAuth", false);
  138. isAuth = false;
  139. removeAlert();
  140. checkIsAuth();
  141. }
  142. function reverseCalculate(e) {
  143. let currency = document.querySelector('select[name="currency"]').selectedOptions[0].value;
  144. let amount = +document.querySelector('input[name="USD"]')?.value ?? 0;
  145. if (amount <= 0) {
  146. addAlert("Amount field value should be greater than 0!", "error");
  147. return;
  148. }
  149. removeAlert("Amount field value should be greater than 0!", "error");
  150. document.querySelector('input[name="amount"]').value = (amount * +exchangeRates[currency]).toFixed(3);
  151. }
  152. function calculate(e) {
  153. let currency = document.querySelector('select[name="currency"]').selectedOptions[0].value;
  154. let amount = +document.querySelector('input[name="amount"]')?.value ?? 0;
  155. if (amount <= 0) {
  156. addAlert("Amount field value should be greater than 0!", "error");
  157. return;
  158. }
  159. removeAlert("Amount field value should be greater than 0!", "error");
  160. document.querySelector('input[name="USD"]').value = (amount / +exchangeRates[currency]).toFixed(3);
  161. }
  162. </script>
  163. </body>
  164. </html>