index.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 exchangeRate = {};
  19. const _alert = (el) => `
  20. <div class="alert ${el.type}">${el.text}</div>
  21. `;
  22. const exchangeForm = `
  23. <form action="" class="form" onsubmit="calculate(event)">
  24. <div class="form-body">
  25. <label for="">Currency:</label>
  26. <select type="number" name="currency" class="form-input">
  27. <option value="RUB">RUB</option>
  28. <option value="UAH">UAH</option>
  29. <option value="EUR">EUR</option>
  30. </select>
  31. <label for="#amount">Amount</label>
  32. <input type="number" name="Amount" id="amount" class="form-input" id="" value="0" />
  33. <button type="submit" class="form-btn btn">Calculate</button>
  34. </div>
  35. <div class="form-footer">
  36. <label for="USD">USD</label>
  37. <input type="text" name="USD" placeholder="0" id="USD" class="form-input" readonly />
  38. </div>
  39. </form>
  40. `;
  41. const authForm = `
  42. <form action="" onsubmit="auth(event)" class="form">
  43. <div class="form-body">
  44. <input type="text" placeholder="Login" name="login" class="form-input" />
  45. <input type="password" placeholder = "Password" name="password" class="form-input" />
  46. <button type="submit" class="btn btn-login">Login</button>
  47. </div>
  48. </form>
  49. `;
  50. const btnLogout = `
  51. <button class="btn btn-logout" onclick="logout()">Logout</button>
  52. `;
  53. const credentials = { login: "admin", password: "qwerty" };
  54. document.querySelector(".container-form").innerHTML = "loading...";
  55. fetch("https://open.er-api.com/v6/latest/USD", {
  56. method: "GET",
  57. })
  58. .then((r) => r.json())
  59. .then(({ rates }) => {
  60. exchangeRates = rates;
  61. })
  62. .then(() => {
  63. checkIsAuth();
  64. });
  65. function checkIsAuth() {
  66. const isAuth = JSON.parse(localStorage.getItem("isAuth"));
  67. if (isAuth === true) {
  68. document.querySelector("div.header").innerHTML = btnLogout;
  69. document.querySelector("div.container-form").innerHTML = exchangeForm;
  70. removeAlert("", "error");
  71. } else {
  72. document.querySelector("div.container-form").innerHTML = authForm;
  73. document.querySelector("div.header").innerHTML = "";
  74. }
  75. }
  76. function updateAlerts() {
  77. let alerts_block = document.querySelector("div.alerts");
  78. alerts_block.innerHTML = "";
  79. alerts.forEach((el) => {
  80. alerts_block.insertAdjacentHTML("beforeend", _alert(el));
  81. });
  82. }
  83. function auth(e) {
  84. e.preventDefault();
  85. let login = e.target.elements.login.value;
  86. let password = e.target.elements.password.value;
  87. login ? removeAlert("Login is required", "error") : addAlert("Login is required", "error");
  88. password ? removeAlert("Password is required", "error") : removeAlert("Password is required", "error");
  89. if (credentials.login !== login || credentials.password !== password) {
  90. localStorage.setItem("isAuth", false);
  91. addAlert("The username or password is incorrect", "error");
  92. return;
  93. }
  94. localStorage.setItem("isAuth", true);
  95. checkIsAuth();
  96. addAlert("Successfull auth", (type = "success"), 5000);
  97. return;
  98. }
  99. function addAlert(text, type, timeout) {
  100. !alerts.some((el) => el.text === text && el.type === type)
  101. ? alerts.push({ text: text, type: type })
  102. : null;
  103. updateAlerts();
  104. if (timeout) {
  105. setTimeout(() => removeAlert(text, type), timeout);
  106. }
  107. }
  108. function removeAlert(text = "", type = "") {
  109. if (text === "" && type === "") {
  110. alerts = [];
  111. }
  112. if (text === "" && type !== "") {
  113. alerts = alerts.filter((el) => el.type !== type);
  114. }
  115. if (text !== "" && type === "") {
  116. alerts = alerts.filter((el) => el.text !== text);
  117. }
  118. if (text !== "" && type !== "") {
  119. alerts = alerts.filter((el) => el.text !== text || el.type !== type);
  120. }
  121. updateAlerts();
  122. }
  123. function logout() {
  124. localStorage.setItem("isAuth", false);
  125. isAuth = false;
  126. removeAlert();
  127. checkIsAuth();
  128. }
  129. function calculate(e) {
  130. e?.preventDefault();
  131. let currency = e.target.elements.currency.selectedOptions[0].value;
  132. let amount = +e.target.elements.amount?.value ?? 0;
  133. if (amount < 0) {
  134. addAlert("Amount field value should be greater than 0!", "error");
  135. return;
  136. }
  137. removeAlert("Amount field value should be greater than 0!", "error");
  138. document.querySelector('input[name="USD"]').value = (amount / +exchangeRates[currency]).toFixed(3);
  139. }
  140. </script>
  141. </body>
  142. </html>