No Description

fetch_articles.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. function get_articles(single = false) {
  2. var articles;
  3. axios.get("http://136.145.231.32:8080/")
  4. .then(response => {
  5. articles = response.data;
  6. if (single) {
  7. let artName = articles[articles.length - 1];
  8. axios.get(`http://136.145.231.32:8080/${artName}`)
  9. .then(response => {
  10. let homeArticleParent = document.querySelector("#home-article")
  11. let articleButton = document.createElement("button");
  12. articleButton.textContent = response.data;
  13. articleButton.classList.add("m-5", "article-content");
  14. articleButton.setAttribute("data-toggle", "modal");
  15. articleButton.setAttribute("data-target", "#homeArticleModal");
  16. let modal = document.createElement("div");
  17. modal.classList.add("modal", "fade");
  18. modal.setAttribute("id", "homeArticleModal");
  19. modal.setAttribute("tabindex", "-1");
  20. modal.setAttribute("aria-labelledby", "homeArticleModalLabel");
  21. modal.setAttribute("aria-hidden", "true");
  22. modal.innerHTML = `
  23. <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable modal-lg">
  24. <div class="modal-content">
  25. <div class="modal-header">
  26. <h5 class="modal-title" id="homeArticleModal">${artName}</h5>
  27. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  28. <span aria-hidden="true">&times;</span>
  29. </button>
  30. </div>
  31. <div class="modal-body">
  32. ${response.data}
  33. </div>
  34. <div class="modal-footer">
  35. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  36. </div>
  37. </div>
  38. </div>`;
  39. document.body.appendChild(modal);
  40. homeArticleParent.appendChild(articleButton);
  41. })
  42. .catch(error => console.log(error));
  43. }
  44. else {
  45. for (let i = 0; i < articles.length; i++) {
  46. let element = articles[i];
  47. axios.get(`http://136.145.231.32:8080/${element}`)
  48. .then(response => {
  49. let articlesHeading = document.querySelector("#articles-heading").parentElement;
  50. let articleButton = document.createElement("button");
  51. articleButton.textContent = response.data;
  52. articleButton.classList.add("m-5", "article-content");
  53. articleButton.setAttribute("data-toggle", "modal");
  54. articleButton.setAttribute("data-target", `#articleModal${i}`);
  55. let modal = document.createElement("div");
  56. modal.classList.add("modal", "fade");
  57. modal.setAttribute("id", `articleModal${i}`);
  58. modal.setAttribute("tabindex", "-1");
  59. modal.setAttribute("aria-labelledby", `articleModalLabel${i}`);
  60. modal.setAttribute("aria-hidden", "true");
  61. modal.innerHTML = `
  62. <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable modal-lg">
  63. <div class="modal-content">
  64. <div class="modal-header">
  65. <h5 class="modal-title" id="articleModalLabel${i}">${element}</h5>
  66. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  67. <span aria-hidden="true">&times;</span>
  68. </button>
  69. </div>
  70. <div class="modal-body">
  71. ${response.data}
  72. </div>
  73. <div class="modal-footer">
  74. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  75. </div>
  76. </div>
  77. </div>`;
  78. document.body.appendChild(modal);
  79. articlesHeading.appendChild(articleButton);
  80. })
  81. .catch(error => console.error(error));
  82. }
  83. }
  84. })
  85. .catch(error => console.error(error));
  86. }