fix: useState hook error in catalog + add certificate download in learning page

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
root
2026-02-06 10:55:24 +00:00
parent 5ddb3db1ac
commit bed7e440c1
3 changed files with 118 additions and 3 deletions

View File

@ -34,6 +34,7 @@ export default function PublicCoursePage() {
const [loading, setLoading] = useState(true);
const [enrolling, setEnrolling] = useState(false);
const [enrolled, setEnrolled] = useState(false);
const [expandedChapters, setExpandedChapters] = useState<string[]>([]);
useEffect(() => {
if (!id) return;
@ -221,12 +222,19 @@ export default function PublicCoursePage() {
<h2 className="text-xl font-semibold mb-4">Содержание курса</h2>
<div className="space-y-2">
{course.chapters.map((chapter: any) => {
const [expanded, setExpanded] = useState(false);
const expanded = expandedChapters.includes(chapter.id);
const toggleExpanded = () => {
setExpandedChapters(prev =>
prev.includes(chapter.id)
? prev.filter(id => id !== chapter.id)
: [...prev, chapter.id]
);
};
return (
<div key={chapter.id} className="border rounded-lg overflow-hidden">
<button
className="flex items-center justify-between w-full p-4 hover:bg-muted/50 transition-colors"
onClick={() => setExpanded(!expanded)}
onClick={toggleExpanded}
>
<div className="flex items-center gap-3">
<BookOpen className="h-4 w-4 text-primary" />

View File

@ -2,7 +2,8 @@
import { useEffect, useState } from 'react';
import Link from 'next/link';
import { GraduationCap, BookOpen, Loader2, Trophy } from 'lucide-react';
import { GraduationCap, BookOpen, Loader2, Trophy, Download } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Card, CardContent } from '@/components/ui/card';
import { Progress } from '@/components/ui/progress';
import { api } from '@/lib/api';
@ -28,6 +29,13 @@ export default function LearningPage() {
const [enrollments, setEnrollments] = useState<EnrollmentData[]>([]);
const [loading, setLoading] = useState(true);
const handleDownloadCertificate = async (courseId: string) => {
try {
const { certificateUrl } = await api.getCertificate(courseId);
window.open(certificateUrl, '_blank');
} catch { /* silent */ }
};
useEffect(() => {
if (authLoading || !user) { setLoading(false); return; }
(async () => {
@ -97,6 +105,17 @@ export default function LearningPage() {
</div>
<Progress value={enrollment.progress} className="h-1.5" />
</div>
{enrollment.completedAt && (
<Button
size="sm"
variant="outline"
className="w-full mt-3"
onClick={(e) => { e.preventDefault(); handleDownloadCertificate(enrollment.course.id); }}
>
<Download className="mr-2 h-3.5 w-3.5" />
Скачать сертификат
</Button>
)}
</CardContent>
</Card>
</Link>