source: src/main/resources/static/js/users-view.js@ 43c9090

Last change on this file since 43c9090 was 43c9090, checked in by macagaso <gasoskamarija@…>, 5 weeks ago

Updated version

  • Property mode set to 100644
File size: 3.5 KB
Line 
1function updateSearchInputVisibility(data) {
2 const searchInputContainer = document.getElementById("search-input-container");
3 if (data !== 'defaultValue') {
4 searchInputContainer.style.display = 'block';
5 } else {
6 searchInputContainer.style.display = 'none';
7 }
8}
9
10function setInitialSelectValue(selectedElement){
11 selectedElement.selectedIndex=0;
12}
13function calculateAge(dateBirth){
14 const [year, month, day] = dateBirth.split('-').map(Number);
15 const birthDate = new Date(year, month - 1, day);
16 const currentDate = new Date();
17 let age = currentDate.getFullYear() - birthDate.getFullYear();
18 const currentMonth = currentDate.getMonth();
19 const currentDay = currentDate.getDate();
20
21 if (currentMonth < (birthDate.getMonth()) || (currentMonth === birthDate.getMonth() && currentDay < birthDate.getDate())) {
22 age--;
23 }
24
25 return age;
26}
27
28function renderTable(filteredUsers) {
29
30 const tbody = document.querySelector("#users-table tbody");
31 tbody.innerHTML = ""; // Clear existing rows
32 if(filteredUsers.length===0){
33 return;
34 }
35
36 filteredUsers.forEach(user => {
37 const row = document.createElement("tr");
38
39 for (const key of ["username", "name", "surname", "dateBirth", "phone"]) {
40 const cell = document.createElement("td");
41 if(key==='dateBirth'){
42 let ageTemp=calculateAge(user[key]);
43 cell.textContent=ageTemp.toString();
44 }
45 else{
46 cell.textContent = user[key];
47 }
48 row.appendChild(cell);
49 }
50
51 const buttonCell = document.createElement("td");
52 const button = document.createElement("button");
53 button.textContent = "Преглед на корисник";
54 button.addEventListener('click',()=>{
55 let temp=user.username;
56 const params = new URLSearchParams({ param1: 'ADMIN', param2: temp }).toString();
57 window.location.href = `editUser.html?${params}`;
58 })
59 buttonCell.appendChild(button);
60 row.appendChild(buttonCell);
61 tbody.appendChild(row);
62 });
63}
64
65
66async function filterUsers(byParam,selectedValue) {
67 let url=`api/users/getUsersByParameter?parameter=${byParam}&filter=${selectedValue}`;
68 try{
69 const response=await fetch(url);
70 if(!response.ok){
71 console.log('Network response was not ok');
72 }
73 else{
74 const users=await response.json();
75 renderTable(users);
76 }
77 }
78 catch(error){
79 console.log("Error fetching users");
80 }
81}
82
83let userStatusElement=document.getElementById("users-status");
84let userParameterElement=document.getElementById("users-parameters");
85
86userStatusElement.addEventListener("change", (event)=>{
87 setInitialSelectValue(userParameterElement);
88 document.getElementById("search-input-container").style.display = 'none';
89 const selectedValue = event.currentTarget.value;
90 if(selectedValue!=='defaultValue')
91 filterUsers("status",selectedValue).then(r => console.log(r));
92});
93
94userParameterElement.addEventListener("change", (event)=>{
95 setInitialSelectValue(userStatusElement);
96 const dataCheck = event.currentTarget.value;
97 updateSearchInputVisibility(dataCheck);
98
99 document.getElementById("search-button").addEventListener('click',function (){
100 let filterTemp=document.getElementById("search-input");
101 filterUsers(dataCheck,filterTemp.value).then(r => console.log(r));
102 filterTemp.value='';
103 })
104});
105
106
Note: See TracBrowser for help on using the repository browser.