index.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. const fun = () => {
  2. btn.onclick = () => {
  3. const img = document.createElement("img");
  4. const urlImage = image.files[0].name;
  5. img.id = "image-file";
  6. img.src = `/image/${urlImage}`;
  7. fetch("/image", {
  8. headers: {
  9. Accept: "application/json",
  10. "Content-type": "application/json",
  11. },
  12. method: "POST",
  13. body: JSON.stringify({
  14. image: urlImage,
  15. }),
  16. });
  17. fetch("message", {
  18. headers: {
  19. Accept: "application/json",
  20. "Content-type": "application/json",
  21. },
  22. method: "POST",
  23. body: JSON.stringify({
  24. nick: nick.value,
  25. message: message.value,
  26. image: urlImage,
  27. }),
  28. })
  29. .then((a) => a.json())
  30. .then((res) => {
  31. const chatMessage = document.querySelector("#chatMessage");
  32. const persone = document.createElement("div");
  33. const spanNick = document.createElement("span");
  34. const spanMessage = document.createElement("span");
  35. persone.id = "persone";
  36. spanNick.id = "nick-message";
  37. spanMessage.id = "nick-message";
  38. spanNick.innerText = res.nick;
  39. spanMessage.innerText = res.message;
  40. persone.appendChild(img);
  41. persone.appendChild(spanNick);
  42. persone.appendChild(spanMessage);
  43. chatMessage.appendChild(persone);
  44. spanMessage.onclick = () => {
  45. const footer = document.querySelector("footer");
  46. footer.style.display = "block";
  47. editMessage.onclick = () => {
  48. spanMessage.style.background = "blue";
  49. nick.value = spanNick.innerText;
  50. message.value = spanMessage.innerText;
  51. footer.style.display = "none";
  52. btn.onclick = () => {
  53. fetch(`/message/${res._id}`, {
  54. headers: {
  55. Accept: "application/json",
  56. "Content-type": "application/json",
  57. },
  58. method: "PUT",
  59. body: JSON.stringify({
  60. nick: nick.value,
  61. message: nick.value,
  62. }),
  63. })
  64. .then((v) => v.json())
  65. .then(() => {
  66. spanNick.innerText = nick.value;
  67. spanMessage.innerText = nick.value;
  68. spanMessage.style.background = "none";
  69. fun();
  70. });
  71. };
  72. };
  73. deleteMessage.onclick = () => {
  74. fetch(`/image/${res.image}`, {
  75. headers: {
  76. Accept: "application/json",
  77. "Content-type": "application/json",
  78. },
  79. method: "DELETE",
  80. });
  81. fetch(`/message/${res._id}`, {
  82. headers: {
  83. Accept: "application/json",
  84. "Content-type": "application/json",
  85. },
  86. method: "DELETE",
  87. })
  88. .then((d) => d.json())
  89. .then(() => {
  90. spanNick.innerText = null;
  91. spanMessage.innerText = null;
  92. spanMessage.style.background = "none";
  93. persone.style.background = "none";
  94. footer.style.display = "none";
  95. chatMessage.removeChild(persone);
  96. });
  97. };
  98. };
  99. });
  100. };
  101. };
  102. fun();
  103. function getMsg() {
  104. fetch("/message")
  105. .then((res) => res.json())
  106. .then((data) => {
  107. for (const key of data) {
  108. const oldMessage = document.querySelector("#oldMessage");
  109. const persone = document.createElement("div");
  110. const spanNick = document.createElement("span");
  111. const spanMessage = document.createElement("span");
  112. const img = document.createElement("img");
  113. const urlImage = key.image;
  114. img.id = "image-file";
  115. img.src = `/image/${urlImage}`;
  116. persone.id = "persone";
  117. spanNick.id = "nick-message";
  118. spanMessage.id = "nick-message";
  119. spanNick.innerText = key.nick;
  120. spanMessage.innerText = key.message;
  121. persone.appendChild(img);
  122. persone.appendChild(spanNick);
  123. persone.appendChild(spanMessage);
  124. oldMessage.appendChild(persone);
  125. spanMessage.onclick = () => {
  126. const footer = document.querySelector("footer");
  127. footer.style.display = "block";
  128. editMessage.onclick = () => {
  129. spanMessage.style.background = "blue";
  130. key.nick = spanNick.innerText;
  131. key.message = spanMessage.innerText;
  132. footer.style.display = "none";
  133. btn.onclick = () => {
  134. fetch(`/message/${key._id}`, {
  135. headers: {
  136. Accept: "application/json",
  137. "Content-type": "application/json",
  138. },
  139. method: "PUT",
  140. body: JSON.stringify({
  141. nick: nick.value,
  142. message: nick.value,
  143. }),
  144. })
  145. .then((v) => v.json())
  146. .then(() => {
  147. spanNick.innerText = nick.value;
  148. spanMessage.innerText = nick.value;
  149. spanMessage.style.background = "none";
  150. fun();
  151. });
  152. };
  153. };
  154. deleteMessage.onclick = () => {
  155. fetch(`/message/${key._id}`, {
  156. headers: {
  157. Accept: "application/json",
  158. "Content-type": "application/json",
  159. },
  160. method: "DELETE",
  161. })
  162. .then((d) => d.json())
  163. .then(() => {
  164. spanNick.innerText = null;
  165. spanMessage.innerText = null;
  166. spanMessage.style.background = "none";
  167. persone.style.background = "none";
  168. footer.style.display = "none";
  169. oldMessage.removeChild(persone);
  170. });
  171. };
  172. };
  173. }
  174. });
  175. }
  176. getMsg();
  177. // image.onchange = async () => {
  178. // url.innerHTML = url.href =
  179. // "/" +
  180. // (await (
  181. // await fetch("/image", {
  182. // method: "POST",
  183. // body: image.files[0],
  184. // })
  185. // ).text());
  186. // };
  187. image.onchange = () => {
  188. url.innerHTML = url.href = `/image/${image.files[0].name}`;
  189. };