Merge pull request #33 from rede5/codex/fix-typeerror-in-api-response-handling
Fix dashboard data guards and numeric parsing
This commit is contained in:
commit
b7ed676153
3 changed files with 30 additions and 7 deletions
|
|
@ -440,7 +440,11 @@ const Dashboard = () => {
|
|||
{pedido.numero}
|
||||
</p>
|
||||
<p className="text-xs text-gray-500">
|
||||
R$ {pedido.valor.toFixed(2)} - {pedido.comprador}
|
||||
R${" "}
|
||||
{Number.isFinite(pedido.valor)
|
||||
? pedido.valor.toFixed(2)
|
||||
: "0.00"}{" "}
|
||||
- {pedido.comprador}
|
||||
</p>
|
||||
</div>
|
||||
<span className="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-yellow-100 text-yellow-800 flex-shrink-0">
|
||||
|
|
|
|||
|
|
@ -78,10 +78,12 @@ export const useDashboardData = ({
|
|||
const activitiesData = await activitiesResponse.json();
|
||||
|
||||
if (activitiesData.success) {
|
||||
setAtividades(activitiesData.data || []);
|
||||
setAtividades(
|
||||
Array.isArray(activitiesData.data) ? activitiesData.data : []
|
||||
);
|
||||
console.log(
|
||||
`✅ [useDashboardData] Atividades obtidas:`,
|
||||
activitiesData.data?.length || 0
|
||||
Array.isArray(activitiesData.data) ? activitiesData.data.length : 0
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -98,10 +100,12 @@ export const useDashboardData = ({
|
|||
const pendingData = await pendingResponse.json();
|
||||
|
||||
if (pendingData.success) {
|
||||
setPedidosPendentes(pendingData.data || []);
|
||||
setPedidosPendentes(
|
||||
Array.isArray(pendingData.data) ? pendingData.data : []
|
||||
);
|
||||
console.log(
|
||||
`✅ [useDashboardData] Pedidos pendentes obtidos:`,
|
||||
pendingData.data?.length || 0
|
||||
Array.isArray(pendingData.data) ? pendingData.data.length : 0
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
|
|
|
|||
|
|
@ -38,6 +38,20 @@ interface Documento {
|
|||
[key: string]: any;
|
||||
}
|
||||
|
||||
const parseValor = (value: unknown, fallback = 0): number => {
|
||||
if (typeof value === 'number' && Number.isFinite(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (typeof value === 'string') {
|
||||
const normalized = value.replace(',', '.');
|
||||
const parsed = Number(normalized);
|
||||
return Number.isFinite(parsed) ? parsed : fallback;
|
||||
}
|
||||
|
||||
return fallback;
|
||||
};
|
||||
|
||||
class DashboardService {
|
||||
/**
|
||||
* Obtém estatísticas do dashboard baseadas no nível do usuário
|
||||
|
|
@ -265,11 +279,12 @@ class DashboardService {
|
|||
|
||||
// Converter pedidos em atividades
|
||||
pedidos.forEach((pedido: Documento) => {
|
||||
const valorTotal = parseValor(pedido['valor-total']);
|
||||
atividades.push({
|
||||
id: pedido.$id,
|
||||
tipo: 'pedido',
|
||||
titulo: `Novo pedido recebido`,
|
||||
descricao: `Pedido #${pedido.$id.substring(0, 8)} - R$ ${(pedido['valor-total'] || 0).toFixed(2)}`,
|
||||
descricao: `Pedido #${pedido.$id.substring(0, 8)} - R$ ${valorTotal.toFixed(2)}`,
|
||||
data: pedido.$createdAt,
|
||||
status: pedido.status === 'pendente' ? 'novo' : 'atualizado',
|
||||
});
|
||||
|
|
@ -330,7 +345,7 @@ class DashboardService {
|
|||
id: pedido.$id,
|
||||
numero: `#${pedido.$id.substring(0, 8)}`,
|
||||
status: pedido.status,
|
||||
valor: pedido['valor-total'] || 0,
|
||||
valor: parseValor(pedido['valor-total']),
|
||||
comprador: pedido.comprador || 'N/A',
|
||||
data: pedido.$createdAt,
|
||||
}));
|
||||
|
|
|
|||
Loading…
Reference in a new issue