index.html 5.7 KB

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