Skip to content

Commit 5720f08

Browse files
refactor(reports): extrai lógica de API para ReportsService
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 35d49de commit 5720f08

2 files changed

Lines changed: 29 additions & 23 deletions

File tree

src/frontend/src/pages/Reports.jsx

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { useState } from "react";
22
import { getUsuarioLogado } from "../services/authService";
3-
4-
const API_URL = "http://localhost:5169/api/report";
3+
import { getRelatorio, exportarPdf } from "../services/ReportsService";
54

65
export default function Reports() {
76
const cobrador = getUsuarioLogado()?.nome ?? "";
@@ -32,15 +31,7 @@ export default function Reports() {
3231
setLoading(true);
3332
setErro("");
3433

35-
const response = await fetch(
36-
`${API_URL}?dataInicio=${dataInicio}&dataFim=${dataFim}&cobrador=${encodeURIComponent(cobrador)}`
37-
);
38-
39-
if (!response.ok) {
40-
throw new Error("Erro ao buscar relatório.");
41-
}
42-
43-
const data = await response.json();
34+
const data = await getRelatorio(dataInicio, dataFim, cobrador);
4435
setRelatorio(data);
4536
} catch (error) {
4637
console.error(error);
@@ -57,18 +48,7 @@ export default function Reports() {
5748
}
5849

5950
try {
60-
const response = await fetch(`${API_URL}/export-pdf`, {
61-
method: "POST",
62-
headers: {
63-
"Content-Type": "application/json",
64-
},
65-
body: JSON.stringify({
66-
dataInicio,
67-
dataFim,
68-
cobrador,
69-
}),
70-
});
71-
51+
const response = await exportarPdf(dataInicio, dataFim, cobrador);
7252
const blob = await response.blob();
7353
const url = window.URL.createObjectURL(blob);
7454

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { getToken } from "./authService";
2+
3+
const BASE_URL = "http://localhost:5169/api/report";
4+
5+
function headers() {
6+
return {
7+
"Content-Type": "application/json",
8+
Authorization: `Bearer ${getToken()}`,
9+
};
10+
}
11+
12+
async function req(url, options = {}) {
13+
const res = await fetch(url, { ...options, headers: headers() });
14+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
15+
return res.json();
16+
}
17+
18+
export const getRelatorio = (dataInicio, dataFim, cobrador) =>
19+
req(`${BASE_URL}?dataInicio=${dataInicio}&dataFim=${dataFim}&cobrador=${encodeURIComponent(cobrador)}`);
20+
21+
export const exportarPdf = (dataInicio, dataFim, cobrador) =>
22+
fetch(`${BASE_URL}/export-pdf`, {
23+
method: "POST",
24+
headers: headers(),
25+
body: JSON.stringify({ dataInicio, dataFim, cobrador }),
26+
});

0 commit comments

Comments
 (0)