analytica-frontend-lib
Version:
Repositório público dos componentes utilizados nas plataformas da Analytica Ensino
155 lines • 4.55 kB
JavaScript
// src/hooks/useRecommendedLessons.ts
import { useState, useCallback } from "react";
import { z } from "zod";
import dayjs from "dayjs";
var goalSubjectSchema = z.object({
id: z.string().uuid(),
name: z.string()
}).nullable();
var goalCreatorSchema = z.object({
id: z.string().uuid(),
name: z.string()
}).nullable();
var goalStatsSchema = z.object({
totalStudents: z.number(),
completedCount: z.number(),
completionPercentage: z.number()
});
var goalBreakdownSchema = z.object({
classId: z.string().uuid(),
className: z.string(),
schoolId: z.string(),
schoolName: z.string(),
studentCount: z.number(),
completedCount: z.number()
});
var goalDataSchema = z.object({
id: z.string().uuid(),
title: z.string(),
startDate: z.string().nullable(),
finalDate: z.string().nullable(),
createdAt: z.string(),
progress: z.number(),
totalLessons: z.number()
});
var goalHistoryItemSchema = z.object({
goal: goalDataSchema,
subject: goalSubjectSchema,
creator: goalCreatorSchema,
stats: goalStatsSchema,
breakdown: z.array(goalBreakdownSchema)
});
var goalsHistoryApiResponseSchema = z.object({
message: z.string(),
data: z.object({
goals: z.array(goalHistoryItemSchema),
total: z.number()
})
});
var determineGoalStatus = (finalDate, completionPercentage) => {
if (completionPercentage === 100) {
return "CONCLU\xCDDA" /* CONCLUIDA */;
}
if (finalDate) {
const now = dayjs();
const deadline = dayjs(finalDate);
if (deadline.isBefore(now)) {
return "VENCIDA" /* VENCIDA */;
}
}
return "ATIVA" /* ATIVA */;
};
var transformGoalToTableItem = (item) => {
const firstBreakdown = item.breakdown[0];
const schoolName = firstBreakdown?.schoolName || "-";
const className = firstBreakdown?.className || "-";
const classDisplay = item.breakdown.length > 1 ? `${item.breakdown.length} turmas` : className;
return {
id: item.goal.id,
startDate: item.goal.startDate ? dayjs(item.goal.startDate).format("DD/MM") : "-",
deadline: item.goal.finalDate ? dayjs(item.goal.finalDate).format("DD/MM") : "-",
title: item.goal.title,
school: schoolName,
year: "-",
// API doesn't provide year directly
subject: item.subject?.name || "-",
class: classDisplay,
status: determineGoalStatus(
item.goal.finalDate,
item.stats.completionPercentage
),
completionPercentage: item.stats.completionPercentage
};
};
var handleGoalFetchError = (error) => {
if (error instanceof z.ZodError) {
console.error("Erro ao validar dados de hist\xF3rico de aulas:", error);
return "Erro ao validar dados de hist\xF3rico de aulas";
}
console.error("Erro ao carregar hist\xF3rico de aulas:", error);
return "Erro ao carregar hist\xF3rico de aulas";
};
var createUseRecommendedLessonsHistory = (fetchGoalsHistory) => {
return () => {
const [state, setState] = useState({
goals: [],
loading: false,
error: null,
pagination: {
total: 0,
page: 1,
limit: 10,
totalPages: 0
}
});
const fetchGoals = useCallback(
async (filters) => {
setState((prev) => ({ ...prev, loading: true, error: null }));
try {
const responseData = await fetchGoalsHistory(filters);
const validatedData = goalsHistoryApiResponseSchema.parse(responseData);
const tableItems = validatedData.data.goals.map(
transformGoalToTableItem
);
const page = filters?.page || 1;
const limit = filters?.limit || 10;
const total = validatedData.data.total;
const totalPages = Math.ceil(total / limit);
setState({
goals: tableItems,
loading: false,
error: null,
pagination: {
total,
page,
limit,
totalPages
}
});
} catch (error) {
const errorMessage = handleGoalFetchError(error);
setState((prev) => ({
...prev,
loading: false,
error: errorMessage
}));
}
},
[fetchGoalsHistory]
);
return {
...state,
fetchGoals
};
};
};
var createRecommendedLessonsHistoryHook = createUseRecommendedLessonsHistory;
export {
createRecommendedLessonsHistoryHook,
createUseRecommendedLessonsHistory,
determineGoalStatus,
goalsHistoryApiResponseSchema,
handleGoalFetchError,
transformGoalToTableItem
};
//# sourceMappingURL=index.mjs.map