-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
134 lines (120 loc) · 4.81 KB
/
index.html
File metadata and controls
134 lines (120 loc) · 4.81 KB
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
134
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>API Data Display</title>
<style>
.data-table {
width: 100%;
border-collapse: collapse;
}
.data-table th, .data-table td {
border: 1px solid #ddd;
padding: 8px;
}
.data-table th {
background-color: #f2f2f2;
text-align: left;
cursor: pointer;
}
.pagination {
margin: 10px 0;
}
.pagination button {
margin: 0 5px;
padding: 5px 10px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Latest Accounts</h1>
<table id="accounts" class="data-table">
<!-- Account data will be displayed here -->
</table>
<div class="pagination" id="accounts-pagination"></div>
<h1>Latest Blocks</h1>
<table id="blocks" class="data-table">
<!-- Block data will be displayed here -->
</table>
<div class="pagination" id="blocks-pagination"></div>
<h1>Latest Transactions</h1>
<table id="transactions" class="data-table">
<!-- Transaction data will be displayed here -->
</table>
<div class="pagination" id="transactions-pagination"></div>
<script>
const state = {
accounts: { page: 1, limit: 10, sort: 'timestamp', order: 'DESC' },
blocks: { page: 1, limit: 10, sort: 'timestamp', order: 'DESC' },
transactions: { page: 1, limit: 10, sort: 'block_number', order: 'DESC' }
};
async function fetchData(endpoint, elementId, paginationId, type) {
const { page, limit, sort, order } = state[type];
const url = `${endpoint}?page=${page}&limit=${limit}&sort=${sort}&order=${order}`;
try {
const response = await fetch(url);
const data = await response.json();
const container = document.getElementById(elementId);
const paginationContainer = document.getElementById(paginationId);
container.innerHTML = '';
// Create table header
if (data.length > 0) {
const tableHeader = document.createElement('tr');
Object.keys(data[0]).forEach(key => {
const th = document.createElement('th');
th.textContent = key;
th.onclick = () => {
state[type].sort = key;
state[type].order = state[type].order === 'ASC' ? 'DESC' : 'ASC';
fetchData(endpoint, elementId, paginationId, type);
};
tableHeader.appendChild(th);
});
container.appendChild(tableHeader);
}
// Populate table rows
data.forEach(item => {
const tableRow = document.createElement('tr');
Object.keys(item).forEach(key => {
const td = document.createElement('td');
if (key === 'tx_hash') {
const a = document.createElement('a');
a.href = `transaction.html?tx_hash=${item[key]}`;
a.textContent = item[key];
td.appendChild(a);
} else if (key === 'address' || key === 'from_address' || key === 'to_address') {
const a = document.createElement('a');
a.href = `account.html?address=${item[key]}`;
a.textContent = item[key];
td.appendChild(a);
} else {
td.textContent = item[key];
}
tableRow.appendChild(td);
});
container.appendChild(tableRow);
});
// Update pagination
paginationContainer.innerHTML = `
<button onclick="changePage('${type}', ${page - 1})" ${page === 1 ? 'disabled' : ''}>Previous</button>
<span>Page ${page}</span>
<button onclick="changePage('${type}', ${page + 1})">Next</button>
`;
} catch (error) {
console.error('Error fetching data:', error);
}
}
function changePage(type, newPage) {
if (newPage < 1) return;
state[type].page = newPage;
fetchData(`http://localhost:3300/${type}`, `${type}`, `${type}-pagination`, type);
}
// Initial fetch for accounts, blocks, and transactions
fetchData('http://localhost:3300/accounts', 'accounts', 'accounts-pagination', 'accounts');
fetchData('http://localhost:3300/blocks', 'blocks', 'blocks-pagination', 'blocks');
fetchData('http://localhost:3300/transactions', 'transactions', 'transactions-pagination', 'transactions');
</script>
</body>
</html>