Align admin, community, search, library, learning and course management with the mockup. Add saved courses, download history, public community feed, instructor directory, backend search, course views, admin content tools and community moderation. Harden API and Nginx for production.
61 lines
4.4 KiB
TypeScript
61 lines
4.4 KiB
TypeScript
import React, { useEffect, useState } from 'react';
|
|
import { Link } from 'react-router-dom';
|
|
import { CircleNotchIcon } from '@phosphor-icons/react';
|
|
import { Course } from '../types';
|
|
import { getMyLearningCourses } from '../services/db';
|
|
import { CourseCard, Cover, Thumb } from '../components/catalog/Artwork';
|
|
import { PlayMockIcon } from '../components/ui/MockupIcons';
|
|
import { useSavedCourses } from '../hooks/useSavedCourses';
|
|
import { useCatalog } from '../context/CatalogContext';
|
|
import { useIsMobile } from '../hooks/useMediaQuery';
|
|
import { paths } from '../lib/routes';
|
|
|
|
export const MyLearning: React.FC<{ onPlay: (course: Course) => void }> = () => {
|
|
const [courses, setCourses] = useState<Course[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [tab, setTab] = useState<'andamento' | 'concluidos' | 'salvos'>('andamento');
|
|
const saved = useSavedCourses();
|
|
const isMobile = useIsMobile();
|
|
const { courses: catalogCourses } = useCatalog();
|
|
useEffect(() => { getMyLearningCourses().then(setCourses).catch(() => setCourses([])).finally(() => setLoading(false)); }, []);
|
|
const going = courses.filter((course) => (course.progress || 0) < 100);
|
|
const done = courses.filter((course) => (course.progress || 0) >= 100);
|
|
const savedCourses = catalogCourses.filter((course) => saved.isSaved(course.id));
|
|
const first = going[0];
|
|
const completedCount = first ? Math.round(((first.progress || 0) / 100) * first.lessons.length) : 0;
|
|
const nextLesson = first?.lessons[Math.min(completedCount, Math.max(first.lessons.length - 1, 0))];
|
|
|
|
if (loading) return <main className="wrap page enter"><div className="loading"><CircleNotchIcon className="ph spin" /></div></main>;
|
|
|
|
if (isMobile) {
|
|
const chosen = tab === 'andamento' ? going : tab === 'concluidos' ? done : savedCourses;
|
|
return <main className="m-page m-gut enter"><header className="m-large"><h1>Minha aprendizagem</h1></header>
|
|
<div className="m-seg" role="tablist">{[['andamento', 'Em andamento'], ['concluidos', 'Concluídos'], ['salvos', 'Salvos']].map(([key, label]) => <button type="button" role="tab" key={key} aria-selected={tab === key} onClick={() => setTab(key as typeof tab)}>{label}</button>)}</div>
|
|
{chosen.length ? <section className="m-group"><div className="m-list">{chosen.map((course) => {
|
|
const progress = course.progress || 0;
|
|
const lessonIndex = Math.min(Math.round((progress / 100) * course.lessons.length), Math.max(course.lessons.length - 1, 0));
|
|
const linkedLesson = course.lessons[lessonIndex];
|
|
const to = tab === 'andamento' && linkedLesson ? paths.lesson(course.id, linkedLesson.id) : paths.course(course.id);
|
|
const sub = tab === 'andamento' ? `Aula ${lessonIndex + 1} de ${course.lessons.length}` : tab === 'concluidos' ? 'Concluído' : `${course.lessons.length} aulas`;
|
|
return <Link className="m-row" key={course.id} to={to}><Thumb course={course} className="m-th" /><span className="m-rt"><b>{course.title}</b><small>{sub}</small></span>{tab === 'andamento' && <span className="m-mini"><span className="m-bar"><i style={{ width: `${progress}%` }} /></span></span>}</Link>;
|
|
})}</div></section> : <p className="m-empty">Nada por aqui ainda.</p>}
|
|
</main>;
|
|
}
|
|
|
|
return <main className="wrap page enter">
|
|
<div className="ptitle"><div><h1>Minha aprendizagem</h1></div></div>
|
|
{first && <Link className="box contbox" to={paths.continueCourse(first.id)}>
|
|
<Cover course={first} variant="mini" />
|
|
<div>
|
|
<h2>{nextLesson?.title || first.title}</h2>
|
|
<p className="num" style={{ fontSize: 14, color: 'var(--fg-2)', marginTop: 4 }}>Continuar de onde parou · {first.title}, aula {Math.min(completedCount + 1, first.lessons.length)} de {first.lessons.length}</p>
|
|
<div className="prog"><i style={{ width: `${Math.round(first.progress || 0)}%` }} /></div>
|
|
</div>
|
|
<span className="btn"><PlayMockIcon className="ph" /> Continuar</span>
|
|
</Link>}
|
|
<section className="shelf"><header><h2>Em andamento</h2></header><div className="grid">{going.map((course) => <CourseCard key={course.id} course={course} />)}</div></section>
|
|
<section className="shelf"><header><h2>Concluídos</h2></header><div className="grid">{done.map((course) => <CourseCard key={course.id} course={course} />)}</div></section>
|
|
<section className="shelf"><header><h2>Salvos</h2></header><div className="grid">{savedCourses.map((course) => <CourseCard key={course.id} course={course} />)}</div></section>
|
|
</main>;
|
|
};
|