You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

user.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. document.addEventListener("DOMContentLoaded", function(){
  2. /*changin page*/
  3. var pageUp = document.getElementById("pageUp");
  4. var pageDown = document.getElementById("pageDown");
  5. var reviewButtons = document.getElementsByClassName('editButton');
  6. document.getElementById("changePwBtn").onclick = function(){
  7. window.location = "/new-password";
  8. }
  9. pageUp.addEventListener("click", () => {
  10. if(maxPages > currentPage+1) {
  11. currentPage++;
  12. loadPosts();
  13. }
  14. });
  15. pageDown.addEventListener("click", () => {
  16. if(currentPage > 0){
  17. currentPage--;
  18. loadPosts();
  19. }
  20. });
  21. async function deletePost(postid){
  22. var response = await fetch("/deletePost", {
  23. method: "POST",
  24. headers: {
  25. "Accept":"application/json",
  26. "Content-Type":"application/json"
  27. },
  28. body:JSON.stringify({postid:postid})
  29. })
  30. var data = await response.json();
  31. console.log(data);
  32. if(data.suc || !data.suc){
  33. loadPosts();
  34. }
  35. };
  36. document.addEventListener("click", function(e){
  37. if(e.target && e.target.classList.contains("remButton")){
  38. var postid = e.target.getAttribute("data-username");
  39. deletePost(postid);
  40. }
  41. })
  42. /*logout*/
  43. var lgoBtn = document.getElementById("lgoBtn");
  44. lgoBtn.addEventListener("click", () => {
  45. window.location = "/logout";
  46. });
  47. /*NewPost*/
  48. var addBtn = document.getElementById("addBtn");
  49. addBtn.addEventListener("click", () => {
  50. window.location = "/newPost";
  51. });
  52. /*Show Elements*/
  53. var maxPages;
  54. var currentPage = 0;
  55. var maxDisplayed = 8;
  56. async function loadPosts(){
  57. var response = await fetch("/getposts", {
  58. method: "GET",
  59. headers: {
  60. "Accept":"application/json",
  61. "Content-Type":"application/json"
  62. }
  63. })
  64. var data = await response.json();
  65. try{
  66. showPosts(data.users.slice(0 + (currentPage * maxDisplayed), maxDisplayed + (currentPage * maxDisplayed)));
  67. console.log(data.users.slice(0 + (currentPage * maxDisplayed), maxDisplayed + (currentPage * maxDisplayed)));
  68. maxPages = data.users.length / 8;
  69. console.log(maxPages, currentPage+1)
  70. reviewButtons = document.getElementsByClassName('editButton');
  71. console.log(reviewButtons.length);
  72. addButtons();
  73. }catch(error){
  74. document.getElementById("table").innerHTML = ""; //empty table
  75. }
  76. };
  77. loadPosts();
  78. function showPosts(pdata){
  79. document.getElementById("table").innerHTML = ""; //empty table
  80. var container = document.getElementById("table"); //insert in table
  81. pdata.forEach(element => {
  82. var row = document.createElement("div");
  83. var status;
  84. if(!element.showpost) {
  85. status = "hidden icon-eye-close"
  86. } else {
  87. status = "shown icon-eye-open"
  88. }
  89. row.classList.add("row");
  90. row.innerHTML = `
  91. <div class="col-left">
  92. <h2>${element.title}</h2>
  93. </div>
  94. <div class="col-right">
  95. <button class="editButton" data-username="${element.postid}">
  96. <i class="icon-file-text-alt , icon"></i>
  97. </button>
  98. <button class="${status} susButton">
  99. </button>
  100. <button class="remButton" data-username="${element.postid}">
  101. <i class="icon-trash , icon"></i>
  102. </button>
  103. </div>
  104. `
  105. container.appendChild(row)
  106. });
  107. }
  108. async function reviewPost(postid){
  109. var response = await fetch("/updatePostID", {
  110. method: "POST",
  111. headers: {
  112. "Accept":"application/json",
  113. "Content-Type":"application/json"
  114. },
  115. body:JSON.stringify({postid:postid})
  116. })
  117. var data = await response.json();
  118. if(data || !data)
  119. {
  120. window.location = "/post-review";
  121. }
  122. };
  123. function addButtons(){
  124. for(let i = 0; i < reviewButtons.length; i++){
  125. reviewButtons[i].addEventListener('click', function(){
  126. reviewPost(this.dataset.username)
  127. })
  128. }
  129. }
  130. });