index.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. fetch("/writing/comments/touchscreens")
  2. .then(response => response.json())
  3. .then((response)=>{
  4. if(typeof(response) === "string"){
  5. }else{
  6. let comments = document.getElementById("comments");
  7. let template = document.getElementById("comment").content.children[0];
  8. let dateOptions = {
  9. month: "long",
  10. day: "numeric",
  11. year: "numeric"
  12. };
  13. for(let i = 0; i < response.length; i++){
  14. let comment = template.cloneNode(true);
  15. comment.children[0].children[0].innerText = response[i].name;
  16. comment.children[0].children[1].innerText = new Date(response[i].date).toLocaleDateString("en-US", dateOptions);
  17. comment.children[1].innerText = response[i].content;
  18. comments.appendChild(comment);
  19. }
  20. }
  21. })
  22. .catch((err)=>{
  23. });
  24. createComment = ()=>{
  25. event.preventDefault();
  26. let data = {
  27. article: document.getElementById("articleInput").getAttribute("article"),
  28. name: document.getElementById("nameInput").value,
  29. content: document.getElementById("commentInput").value,
  30. date: new Date()
  31. };
  32. fetch("/writing/comments", {
  33. method: "post",
  34. headers: {
  35. "Content-Type": "application/json;charset=utf-8"
  36. },
  37. body: JSON.stringify(data)
  38. })
  39. .then(response => response.json())
  40. .then((response)=>{
  41. if(typeof(response) === "string"){
  42. }else{
  43. let comments = document.getElementById("comments");
  44. let comment = document.getElementById("comment").content.children[0].cloneNode(true);
  45. let dateOptions = {
  46. month: "long",
  47. day: "numeric",
  48. year: "long"
  49. };
  50. comment.children[0].children[0].innerText = response.name;
  51. comment.children[0].children[1].innerText = new Date(response.date).toLocaleDateString("en-US", dateOptions);
  52. comment.children[1].innerText = response.content;
  53. comments.insertBefore(comment, comments.children[1]);
  54. }
  55. })
  56. .catch((err)=>{
  57. });
  58. }
  59. document.getElementById("createComment").onsubmit = ()=>{createComment()};