forked from PicPay/picpay-desafio-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
207 lines (177 loc) · 7.18 KB
/
Copy pathindex.html
File metadata and controls
207 lines (177 loc) · 7.18 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Extrato de Transferências</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
}
.container {
width: 80%;
max-width: 600px;
margin: 20px 0;
}
#extrato {
background: #ffffff;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
border-radius: 8px;
padding: 20px;
min-height: 30vh;
display: flex;
flex-direction: column;
align-items: center;
}
#extrato ul {
list-style-type: none;
padding: 0;
margin: 0;
width: 100%;
}
#extrato li {
background: #f9f9f9;
margin: 8px 0;
padding: 6px 10px;
border-radius: 4px;
border: 1px solid #ddd;
}
#extrato li * {
padding: 4px 0px;
margin: 0;
}
#extrato li .tipo {
color: #333;
font-weight: bold;
padding: 0.25rem 0.5rem;
border-radius: 6px;
width: min-content;
}
#extrato li.payer .tipo {
background-color: #dd7171;
}
#extrato li.payee .tipo {
background-color: #71dd71;
}
#extrato h2 {
margin: 0 0 20px 0;
font-size: 1.5em;
color: #333;
}
#paginacao {
display: flex;
justify-content: center;
gap: 2rem;
width: 50%;
}
#paginacao button {
padding: 10px 20px;
border: none;
border-radius: 4px;
background: #007bff;
color: #fff;
font-size: 1em;
cursor: pointer;
transition: background 0.3s ease;
}
#paginacao button:disabled {
background: #cccccc;
cursor: not-allowed;
}
#paginacao button:hover:not(:disabled) {
background: #0056b3;
}
</style>
</head>
<body>
<div class="container" id="extrato">
<ul>
<li>Loading...</li>
</ul>
</div>
<div class="container" id="paginacao">
<button type="button" id="first" onclick="navigate('first')">First</button>
<button type="button" id="prev" onclick="navigate('prev')" disabled>Prev</button>
<button type="button" id="current" disabled>Current</button>
<button type="button" id="next" onclick="navigate('next')" disabled>Next</button>
<button type="button" id="last" onclick="navigate('last')">Last</button>
</div>
<script>
const token = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZF91c2VyIjoxLCJuYW1lIjoiSm_Do28gUGVkcm8iLCJjcGZfY25waiI6IjQ4NzExMzk5ODA1IiwicGVyc29uX3R5cGUiOiJGIiwiZW1haWwiOiJqb2FvcGVkcm9AYXNjb250c2lzdGVtYXMuY29tLmJyIiwiaWF0IjoxNzE3NjE1MTY3fQ.kDRB_tqTUC2scn5mG1F4yp4EjUzfjEM4Jd0vqqy5Cp0';
let page = 1;
const limit = 4;
let links = {};
let totalPages = 0;
let id_user = 1;
async function fetchData(url) {
document.querySelector('#extrato ul').innerHTML = '<li>Loading...</li>';
try {
const response = await fetch(url, {
headers: {
'Authorization': token
}
});
if (!response.ok) throw new Error('Network response was not ok');
const linksHeader = response.headers.get('X-Pagination');
totalPages = Number(response.headers.get('X-Pagination-Pages'));
links = JSON.parse(linksHeader);
const res = await response.json();
if (res.data && res.data.length > 0) {
document.querySelector('#extrato ul').innerHTML = '';
res.data.forEach(d => {
document.querySelector('#extrato ul').innerHTML += itemList(d);
});
} else {
document.querySelector('#extrato ul').innerHTML = '<li>No data available</li>';
}
updatePagination();
} catch (error) {
console.warn(error);
document.querySelector('#extrato ul').innerHTML = '<li>Error loading data</li>';
}
}
function navigate(direction) {
const link = links.find(link => link.rel === direction);
if (link) {
page = new URL(link.href).searchParams.get('page');
fetchData(link.href);
}
}
function updatePagination() {
document.getElementById('first').disabled = !links.find(link => link.rel === 'first') || page == 1;
document.getElementById('prev').disabled = !links.find(link => link.rel === 'prev') || page == 1;
document.getElementById('next').disabled = !links.find(link => link.rel === 'next') || page == totalPages;
document.getElementById('last').disabled = !links.find(link => link.rel === 'last') || page == totalPages;
document.getElementById('prev').style.display = Number(page) - 1 >= 1 ? 'block' : 'none';
document.getElementById('next').style.display = Number(page) + 1 <= totalPages ? 'block' : 'none';
document.getElementById('first').style.display = document.getElementById('prev').style.display;
document.getElementById('last').style.display = document.getElementById('next').style.display;
document.getElementById('first').innerHTML = 'First';
document.getElementById('prev').innerHTML = Number(page) - 1 >= 1 ? Number(page) - 1 : 1;
document.getElementById('current').innerHTML = Number(page);
document.getElementById('next').innerHTML = Number(page) + 1 ? Number(page) + 1 : totalPages;
document.getElementById('last').innerHTML = 'Last';
}
function itemList(transfer) {
console.log(transfer)
console.log(transfer.payer.id_user)
console.log(id_user)
return `
<li class="${transfer.payer.id_payer == id_user ? 'payer' : 'payee'}">
<legend class="tipo">${transfer.payer.id_payer == id_user ? 'Saída' : 'Entrada'}</legend>
<p><strong>Transferência ${transfer.payer.id_payer == id_user ? 'efetuada' : 'recebida'}</strong></p>
<p>Valor: R\$${transfer.amount.toLocaleString('pt-br', { minimumFractionDigits: 2 }).replace('.', ',')}</p>
<p>${transfer.payer.id_payer == id_user ? 'Para' : 'De'}: ${transfer.payer.id_payer == id_user ? transfer.payer.name : transfer.payee.name}</p>
</li>
`;
}
fetchData(`http://localhost:3000/transfers/user/${id_user}?limit=${limit}&page=${page}`);
</script>
</body>
</html>