index.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. <title>Document</title>
  8. <link
  9. href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
  10. rel="stylesheet"
  11. />
  12. </head>
  13. <body class="container bg-info">
  14. <h2>Чатик</h2>
  15. <form name="chatForm">
  16. <div class="form-group">
  17. <input class="form-control" placeholder="Nick:" name="nick" />
  18. </div>
  19. <div class="form-group">
  20. <input class="form-control" placeholder="Message:" name="message" />
  21. </div>
  22. <div class="panel-body">
  23. <button type="submit" class="btn btn-sm btn-primary">Отправить</button>
  24. </div>
  25. </form>
  26. <table class="table table-condensed table-striped table-bordered">
  27. <thead>
  28. <tr>
  29. <th>Date</th>
  30. <th>Nick</th>
  31. <th>Message</th>
  32. </tr>
  33. </thead>
  34. <tbody></tbody>
  35. </table>
  36. <script>
  37. async function GetUsers() {
  38. const response = await fetch("/chat", {
  39. method: "GET",
  40. headers: { Accept: "application/json" },
  41. });
  42. if (response.ok === true) {
  43. const messagesData = await response.json();
  44. let rows = document.querySelector("tbody");
  45. messagesData.forEach((message) => {
  46. rows.append(row(message));
  47. });
  48. }
  49. }
  50. async function CreateMessage(nickName, messageText) {
  51. const response = await fetch("/chat", {
  52. method: "POST",
  53. headers: {
  54. Accept: "application/json",
  55. "Content-Type": "application/json",
  56. },
  57. body: JSON.stringify({
  58. nick: nickName,
  59. message: messageText,
  60. }),
  61. });
  62. if (response.ok === true) {
  63. const message = await response.json();
  64. reset();
  65. document.querySelector("tbody").append(row(message));
  66. }
  67. }
  68. function reset() {
  69. const form = document.forms["chatForm"];
  70. form.reset();
  71. }
  72. function row(messageItem) {
  73. const tr = document.createElement("tr");
  74. tr.setAttribute("data-rowid", messageItem.id);
  75. const idTd = document.createElement("td");
  76. idTd.append(messageItem.id);
  77. tr.append(idTd);
  78. const nameTd = document.createElement("td");
  79. nameTd.append(messageItem.nick);
  80. tr.append(nameTd);
  81. const messTd = document.createElement("td");
  82. messTd.append(messageItem.message);
  83. tr.append(messTd);
  84. return tr;
  85. }
  86. document.forms["chatForm"].addEventListener("submit", (e) => {
  87. e.preventDefault();
  88. const form = document.forms["chatForm"];
  89. const nick = form.elements["nick"].value;
  90. const message = form.elements["message"].value;
  91. CreateMessage(nick, message);
  92. });
  93. GetUsers();
  94. </script>
  95. </body>
  96. </html>