UNPKG

analytica-frontend-lib

Version:

Repositório público dos componentes utilizados nas plataformas da Analytica Ensino

195 lines (194 loc) 7.07 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/hooks/useRecommendedLessons.ts var useRecommendedLessons_exports = {}; __export(useRecommendedLessons_exports, { createRecommendedLessonsHistoryHook: () => createRecommendedLessonsHistoryHook, createUseRecommendedLessonsHistory: () => createUseRecommendedLessonsHistory, determineGoalStatus: () => determineGoalStatus, goalsHistoryApiResponseSchema: () => goalsHistoryApiResponseSchema, handleGoalFetchError: () => handleGoalFetchError, transformGoalToTableItem: () => transformGoalToTableItem }); module.exports = __toCommonJS(useRecommendedLessons_exports); var import_react = require("react"); var import_zod = require("zod"); var import_dayjs = __toESM(require("dayjs")); var goalSubjectSchema = import_zod.z.object({ id: import_zod.z.string().uuid(), name: import_zod.z.string() }).nullable(); var goalCreatorSchema = import_zod.z.object({ id: import_zod.z.string().uuid(), name: import_zod.z.string() }).nullable(); var goalStatsSchema = import_zod.z.object({ totalStudents: import_zod.z.number(), completedCount: import_zod.z.number(), completionPercentage: import_zod.z.number() }); var goalBreakdownSchema = import_zod.z.object({ classId: import_zod.z.string().uuid(), className: import_zod.z.string(), schoolId: import_zod.z.string(), schoolName: import_zod.z.string(), studentCount: import_zod.z.number(), completedCount: import_zod.z.number() }); var goalDataSchema = import_zod.z.object({ id: import_zod.z.string().uuid(), title: import_zod.z.string(), startDate: import_zod.z.string().nullable(), finalDate: import_zod.z.string().nullable(), createdAt: import_zod.z.string(), progress: import_zod.z.number(), totalLessons: import_zod.z.number() }); var goalHistoryItemSchema = import_zod.z.object({ goal: goalDataSchema, subject: goalSubjectSchema, creator: goalCreatorSchema, stats: goalStatsSchema, breakdown: import_zod.z.array(goalBreakdownSchema) }); var goalsHistoryApiResponseSchema = import_zod.z.object({ message: import_zod.z.string(), data: import_zod.z.object({ goals: import_zod.z.array(goalHistoryItemSchema), total: import_zod.z.number() }) }); var determineGoalStatus = (finalDate, completionPercentage) => { if (completionPercentage === 100) { return "CONCLU\xCDDA" /* CONCLUIDA */; } if (finalDate) { const now = (0, import_dayjs.default)(); const deadline = (0, import_dayjs.default)(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 ? (0, import_dayjs.default)(item.goal.startDate).format("DD/MM") : "-", deadline: item.goal.finalDate ? (0, import_dayjs.default)(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 import_zod.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] = (0, import_react.useState)({ goals: [], loading: false, error: null, pagination: { total: 0, page: 1, limit: 10, totalPages: 0 } }); const fetchGoals = (0, import_react.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; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createRecommendedLessonsHistoryHook, createUseRecommendedLessonsHistory, determineGoalStatus, goalsHistoryApiResponseSchema, handleGoalFetchError, transformGoalToTableItem }); //# sourceMappingURL=index.js.map