-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdminUIScript.js
133 lines (114 loc) · 4.91 KB
/
AdminUIScript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { paginateMembersTable } from './PaginationScript.js'
import { editRow, deleteRow } from './DeleteAndEditScript.js'
let memberDetailsList = null;
let filteredMembersData = null;
const getMembersData = async function () {
const membersResponseObject = await fetch("https://geektrust.s3-ap-southeast-1.amazonaws.com/adminui-problem/members.json");
console.log(membersResponseObject)
memberDetailsList = await membersResponseObject.json();
filteredMembersData = memberDetailsList.map(member => member);
console.log(memberDetailsList)
}
export const createInitialActionsCell = (memberId) => {
let actionsCellElement = document.createElement('td');
actionsCellElement.setAttribute('id', `actionscell${memberId}`)
let editIconElement = document.createElement("i");
editIconElement.classList.add('fas', 'fa-user-edit');
editIconElement.setAttribute('id', `editbutton${memberId}`)
editIconElement.addEventListener('click', function (event) { editRow(event, memberId) })
actionsCellElement.appendChild(editIconElement);
let deleteIconElement = document.createElement("i");
deleteIconElement.classList.add('fas', 'fa-trash-alt', 'delete-icon');
deleteIconElement.setAttribute('id', `deletebutton${memberId}`)
deleteIconElement.addEventListener('click', function (event) { deleteRow(event, memberId) })
actionsCellElement.appendChild(deleteIconElement);
return actionsCellElement
}
const changeCheckedRowBg=(event)=>{
let memberId=event.target.id.split('checkbox')[1]
if (event.target.checked==true){
document.getElementById(`member${memberId}`).style.backgroundColor='#e8e8e8';
}
else{
document.getElementById(`member${memberId}`).style.backgroundColor='white';
}
}
const createMembersTableRow = (member) => {
let memberDetailsRow = document.createElement('tr');
let checkBoxCellElement = document.createElement('td');
let checkBoxElement = document.createElement('input');
checkBoxElement.setAttribute('id', `checkbox${member['id']}`)
checkBoxElement.setAttribute('name', `memberCheckBox`)
checkBoxElement.addEventListener('change',changeCheckedRowBg);
checkBoxElement.type = 'checkbox'
checkBoxCellElement.appendChild(checkBoxElement);
memberDetailsRow.appendChild(checkBoxCellElement);
for (let detail in member) {
if (detail !== 'id') {
let detailElement = document.createElement('td');
detailElement.textContent = member[detail];
detailElement.setAttribute('id', `${detail}${member['id']}`)
memberDetailsRow.appendChild(detailElement);
}
else {
memberDetailsRow.setAttribute('id', `member${member[detail]}`)
}
}
let actionsCellElement = createInitialActionsCell(member['id'])
memberDetailsRow.appendChild(actionsCellElement);
return memberDetailsRow;
}
export const fillMembersTable = (membersData) => {
const membersTableBodyElement = document.getElementById('membersTableBody');
membersTableBodyElement.textContent = '';
membersData.map(member => {
let memberDetailsRow = createMembersTableRow(member)
membersTableBodyElement.appendChild(memberDetailsRow)
})
}
const filterMembersTable = (event) => {
let searchText = event.target.value;
console.log(searchText)
filteredMembersData = memberDetailsList.filter(eachMember => {
for (let detail in eachMember) {
if (detail !== 'id') {
if (eachMember[detail].toLowerCase().includes(searchText.toLowerCase())) {
return eachMember;
}
}
}
})
console.log(filteredMembersData);
paginateMembersTable(filteredMembersData);
}
export const filterMembersArrayDataAndRerender = (memberIds) => {
filteredMembersData = filteredMembersData.filter(member => {
if (!memberIds.includes(parseInt(member['id'] ))) {
return member;
}
})
memberDetailsList = memberDetailsList.filter(member => {
if (!memberIds.includes(parseInt(member['id'] ))) {
return member;
}
})
paginateMembersTable(filteredMembersData);
console.log(filteredMembersData);
}
export const updateDetailsInDetailsArray = (memberId, newName, newEmail, newRole) => {
for (let member of memberDetailsList) {
if (member.id == memberId) {
member.name = newName;
member.email = newEmail;
member.role = newRole;
}
break;
}
}
const functionsHandler = async () => {
await getMembersData();
let membersSearchElement = document.getElementById('membersSearch');
membersSearchElement.addEventListener('input', function (event) { filterMembersTable(event) })
paginateMembersTable(memberDetailsList);
}
functionsHandler()