analytica-frontend-lib
Version:
Repositório público dos componentes utilizados nas plataformas da Analytica Ensino
115 lines (114 loc) • 3.64 kB
JavaScript
// src/hooks/useStudentsHighlight.ts
import { useState, useCallback } from "react";
import { z } from "zod";
var PERIOD_TABS = [
{ value: "7_DAYS", label: "7 dias" },
{ value: "1_MONTH", label: "1 m\xEAs" },
{ value: "3_MONTHS", label: "3 meses" },
{ value: "6_MONTHS", label: "6 meses" },
{ value: "1_YEAR", label: "1 ano" }
];
var trendDirectionSchema = z.enum(["up", "down", "stable"]).nullable();
var studentHighlightItemSchema = z.object({
id: z.string().uuid(),
name: z.string(),
correctAnswers: z.number().min(0),
incorrectAnswers: z.number().min(0),
totalQuestions: z.number().min(0),
trend: z.number().nullable(),
trendDirection: trendDirectionSchema
});
var studentsHighlightApiResponseSchema = z.object({
message: z.string(),
data: z.object({
topStudents: z.array(studentHighlightItemSchema),
bottomStudents: z.array(studentHighlightItemSchema)
})
});
var calculatePerformancePercentage = (correctAnswers, totalQuestions) => {
if (totalQuestions === 0) {
return 0;
}
return Math.round(correctAnswers / totalQuestions * 100);
};
var transformStudentHighlightItem = (item, position) => ({
id: item.id,
position,
name: item.name,
percentage: calculatePerformancePercentage(
item.correctAnswers,
item.totalQuestions
),
correctAnswers: item.correctAnswers,
incorrectAnswers: item.incorrectAnswers,
totalQuestions: item.totalQuestions,
trend: item.trend,
trendDirection: item.trendDirection
});
var handleStudentsHighlightFetchError = (error) => {
if (error instanceof z.ZodError) {
console.error("Erro ao validar dados de destaque de estudantes:", error);
return "Erro ao validar dados de destaque de estudantes";
}
console.error("Erro ao carregar destaque de estudantes:", error);
return "Erro ao carregar destaque de estudantes";
};
var initialState = {
topStudents: [],
bottomStudents: [],
loading: false,
error: null
};
var createUseStudentsHighlight = (fetchStudentsHighlightApi) => {
return () => {
const [state, setState] = useState(initialState);
const fetchStudentsHighlight = useCallback(
async (filters) => {
setState((prev) => ({ ...prev, loading: true, error: null }));
try {
const responseData = await fetchStudentsHighlightApi(filters);
const validatedData = studentsHighlightApiResponseSchema.parse(responseData);
const topStudents = validatedData.data.topStudents.map(
(student, index) => transformStudentHighlightItem(student, index + 1)
);
const bottomStudents = validatedData.data.bottomStudents.map(
(student, index) => transformStudentHighlightItem(student, index + 1)
);
setState({
topStudents,
bottomStudents,
loading: false,
error: null
});
} catch (error) {
const errorMessage = handleStudentsHighlightFetchError(error);
setState((prev) => ({
...prev,
loading: false,
error: errorMessage
}));
}
},
[fetchStudentsHighlightApi]
);
const reset = useCallback(() => {
setState(initialState);
}, []);
return {
...state,
fetchStudentsHighlight,
reset
};
};
};
var createStudentsHighlightHook = createUseStudentsHighlight;
export {
PERIOD_TABS,
studentsHighlightApiResponseSchema,
calculatePerformancePercentage,
transformStudentHighlightItem,
handleStudentsHighlightFetchError,
createUseStudentsHighlight,
createStudentsHighlightHook
};
//# sourceMappingURL=chunk-NPDKLMTS.mjs.map