Greg Claude Opus 4.7 (1M context) commited on
Commit
17b88c0
·
1 Parent(s): ec0af58

fix(dashboard-ui): surface errors, refetch on Limpiar, hide empty chart skeleton

Browse files
frontend/src/components/SexBySectionChart.tsx CHANGED
@@ -19,6 +19,7 @@ export default function SexBySectionChart() {
19
  const [selectedSectionIds, setSelectedSectionIds] = useState<number[]>([]);
20
  const [data, setData] = useState<SexBySection | null>(null);
21
  const [loading, setLoading] = useState(false);
 
22
 
23
  useEffect(() => {
24
  const loadSections = async () => {
@@ -39,6 +40,7 @@ export default function SexBySectionChart() {
39
 
40
  const fetchData = async () => {
41
  setLoading(true);
 
42
  try {
43
  const res = await getSexBySection({
44
  dateFrom,
@@ -46,6 +48,25 @@ export default function SexBySectionChart() {
46
  sectionIds: selectedSectionIds.length ? selectedSectionIds : undefined,
47
  });
48
  setData(res);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  } finally {
50
  setLoading(false);
51
  }
@@ -100,14 +121,24 @@ export default function SexBySectionChart() {
100
  {loading ? 'Cargando…' : 'Aplicar'}
101
  </button>
102
  <button
103
- onClick={() => { setDateFrom(today); setDateTo(today); setSelectedSectionIds([]); }}
104
  className="border px-3 py-1 rounded"
105
  >
106
  Limpiar
107
  </button>
108
  </div>
109
 
110
- {data && data.totals.total === 0 ? (
 
 
 
 
 
 
 
 
 
 
111
  <p className="text-gray-500 text-sm">No hay datos de asistencia en este rango.</p>
112
  ) : (
113
  <>
 
19
  const [selectedSectionIds, setSelectedSectionIds] = useState<number[]>([]);
20
  const [data, setData] = useState<SexBySection | null>(null);
21
  const [loading, setLoading] = useState(false);
22
+ const [error, setError] = useState<string | null>(null);
23
 
24
  useEffect(() => {
25
  const loadSections = async () => {
 
40
 
41
  const fetchData = async () => {
42
  setLoading(true);
43
+ setError(null);
44
  try {
45
  const res = await getSexBySection({
46
  dateFrom,
 
48
  sectionIds: selectedSectionIds.length ? selectedSectionIds : undefined,
49
  });
50
  setData(res);
51
+ } catch (e: any) {
52
+ setError(e?.response?.data?.detail || 'Error al cargar el gráfico');
53
+ } finally {
54
+ setLoading(false);
55
+ }
56
+ };
57
+
58
+ const handleClear = async () => {
59
+ setDateFrom(today);
60
+ setDateTo(today);
61
+ setSelectedSectionIds([]);
62
+ // Refetch with cleared params — don't rely on state being updated yet
63
+ setLoading(true);
64
+ setError(null);
65
+ try {
66
+ const res = await getSexBySection({ dateFrom: today, dateTo: today });
67
+ setData(res);
68
+ } catch (e: any) {
69
+ setError(e?.response?.data?.detail || 'Error al cargar el gráfico');
70
  } finally {
71
  setLoading(false);
72
  }
 
121
  {loading ? 'Cargando…' : 'Aplicar'}
122
  </button>
123
  <button
124
+ onClick={handleClear}
125
  className="border px-3 py-1 rounded"
126
  >
127
  Limpiar
128
  </button>
129
  </div>
130
 
131
+ {error && (
132
+ <p className="text-sm text-red-700 bg-red-50 border border-red-200 rounded px-3 py-2 mb-3">
133
+ {error}
134
+ </p>
135
+ )}
136
+
137
+ {!data && loading ? (
138
+ <p className="text-gray-500 text-sm">Cargando…</p>
139
+ ) : !data ? (
140
+ <p className="text-gray-500 text-sm">Sin datos para mostrar.</p>
141
+ ) : data.totals.total === 0 ? (
142
  <p className="text-gray-500 text-sm">No hay datos de asistencia en este rango.</p>
143
  ) : (
144
  <>