forked from XceeDesigns/Website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact-dashboard.js
96 lines (83 loc) · 4.23 KB
/
contact-dashboard.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
const fetchData = async () => {
const input = document.getElementById('fetchInput').value;
const fetchBtn = document.getElementById('fetchBtn');
fetchBtn.disabled = true;
const response = await fetch(`https://xceedesigns-backend.vercel.app/contact/fetch`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ adminAuth: input })
});
if(response.status === 401) {
const modalTitle = document.querySelector('.modal-title');
modalTitle.textContent = 'XceeDesigns LLC.';
modalTitle.style.fontWeight = 'bold';
const modalBody = document.querySelector('.modal-body');
modalBody.textContent = 'Authentication failed, please try again.';
$('#myModal').modal('show');
fetchBtn.disabled = false;
return;
}
const data = await response.json();
fetchBtn.disabled = false;
const table = document.createElement('table');
table.classList.add('table');
// Remove previous table if exists
const previousTable = document.querySelector('table');
if (previousTable) {
previousTable.remove();
}
const tableHead = document.createElement('thead');
const tableHeadRow = document.createElement('tr');
const nameHeader = document.createElement('th');
nameHeader.textContent = 'Name';
tableHeadRow.appendChild(nameHeader);
const emailHeader = document.createElement('th');
emailHeader.textContent = 'Email';
tableHeadRow.appendChild(emailHeader);
const messageHeader = document.createElement('th');
messageHeader.textContent = 'Message';
tableHeadRow.appendChild(messageHeader);
const deleteHeader = document.createElement('th');
deleteHeader.textContent = 'Delete';
tableHeadRow.appendChild(deleteHeader);
tableHead.appendChild(tableHeadRow);
table.appendChild(tableHead);
const tableBody = document.createElement('tbody');
data.forEach(entry => {
const tableRow = document.createElement('tr');
const nameCell = document.createElement('td');
nameCell.textContent = entry.name;
tableRow.appendChild(nameCell);
const emailCell = document.createElement('td');
emailCell.textContent = entry.email;
tableRow.appendChild(emailCell);
const messageCell = document.createElement('td');
messageCell.textContent = entry.message;
messageCell.style.maxWidth = '300px';
messageCell.style.maxHeight = '100px'; // Set a maximum height for the message cell
messageCell.style.wordWrap = 'break-word'; // Wrap long words
messageCell.style.whiteSpace = 'normal';
tableRow.appendChild(messageCell);
const deleteCell = document.createElement('td');
const deleteIcon = document.createElement('div');
deleteIcon.classList.add('delete-icon');
deleteIcon.textContent = '🗑️';
deleteIcon.style.cursor = 'pointer';
deleteIcon.addEventListener('click', async () => {
const res = await fetch(`https://xceedesigns-backend.vercel.app/contact/delete/${entry._id}`, {
method: 'DELETE'
});
tableRow.remove();
});
deleteCell.appendChild(deleteIcon);
tableRow.appendChild(deleteCell);
tableBody.appendChild(tableRow);
});
table.appendChild(tableBody);
const tableContainer = document.createElement('div');
tableContainer.classList.add('table-responsive');
tableContainer.appendChild(table);
document.getElementById('dashboard').appendChild(tableContainer);
};