'use client'; import React, { useState, useEffect, useMemo } from 'react'; import { useAuth } from '@/contexts/AuthContext'; import { getLessons, type Lesson } from '@/api/schedule'; import { FeedbackModal } from '@/components/schedule/FeedbackModal'; import { DashboardLayout } from '@/components/dashboard/ui'; import { LoadingSpinner } from '@/components/common/LoadingSpinner'; function getSubjectName(lesson: Lesson): string { if (typeof lesson.subject === 'string') return lesson.subject; if (lesson.subject && typeof lesson.subject === 'object' && 'name' in lesson.subject) { return (lesson.subject as { name: string }).name; } return (lesson as { subject_name?: string }).subject_name || 'Занятие'; } function formatDate(s: string) { return new Date(s).toLocaleDateString('ru-RU', { day: 'numeric', month: 'long', year: 'numeric' }); } function formatTime(s: string) { return new Date(s).toLocaleTimeString('ru-RU', { hour: '2-digit', minute: '2-digit' }); } export default function FeedbackPage() { const { user, loading: authLoading } = useAuth(); const [lessons, setLessons] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [selectedLesson, setSelectedLesson] = useState(null); const [showModal, setShowModal] = useState(false); useEffect(() => { if (user?.role !== 'mentor') return; let cancelled = false; (async () => { try { setLoading(true); const res = await getLessons({ status: 'completed' }); const list = Array.isArray(res) ? res : res?.results || []; if (!cancelled) setLessons(list); } catch (e) { if (!cancelled) setError(e instanceof Error ? e.message : 'Ошибка загрузки'); } finally { if (!cancelled) setLoading(false); } })(); return () => { cancelled = true; }; }, [user?.role]); const studentLessons = useMemo( () => lessons.filter((l) => !(l as { group?: number }).group), [lessons] ); const getFeedbackStatus = (l: Lesson) => { const has = !!( l.mentor_grade || (l as { school_grade?: number }).school_grade || (l.mentor_notes && l.mentor_notes.trim().length > 0) ); return has ? 'done' : 'todo'; }; const todoLessons = studentLessons.filter((l) => getFeedbackStatus(l) === 'todo'); const doneLessons = studentLessons.filter((l) => getFeedbackStatus(l) === 'done'); const openFeedback = (lesson: Lesson) => { setSelectedLesson(lesson); setShowModal(true); }; const handleSuccess = async () => { setSelectedLesson(null); setShowModal(false); try { const res = await getLessons({ status: 'completed' }); const list = Array.isArray(res) ? res : res?.results || []; setLessons(list); } catch { // ignore } }; const loadLessons = async () => { try { const res = await getLessons({ status: 'completed' }); const list = Array.isArray(res) ? res : res?.results || []; setLessons(list); } catch { // ignore } }; if (authLoading) { return (
); } if (user?.role !== 'mentor') { return (
Страница доступна только менторам
); } const LessonCard = ({ lesson, onFill, }: { lesson: Lesson; onFill: () => void; }) => { const clientName = typeof lesson.client === 'object' && lesson.client?.user ? `${lesson.client.user.first_name} ${lesson.client.user.last_name}` : (lesson as { client_name?: string }).client_name || 'Студент'; return (
{getSubjectName(lesson)}

{lesson.title}

person {clientName}
calendar_today {formatDate(lesson.start_time)}
schedule {formatTime(lesson.start_time)} — {formatTime(lesson.end_time)}
{lesson.mentor_grade != null && (
Оценка: {lesson.mentor_grade}/5
)}
); }; return ( {error && (
{error}
)} {loading ? (
) : (

Ожидают {todoLessons.length > 0 ? `(${todoLessons.length})` : ''}

{todoLessons.map((l) => ( openFeedback(l)} /> ))} {todoLessons.length === 0 && (

Нет занятий

)}

Заполнено {doneLessons.length > 0 ? `(${doneLessons.length})` : ''}

{doneLessons.map((l) => ( openFeedback(l)} /> ))} {doneLessons.length === 0 && (

Нет занятий

)}
)} { setShowModal(false); setSelectedLesson(null); }} onSuccess={handleSuccess} />
); }