feat: add reviews, notifications, notes and account hardening
This commit is contained in:
@@ -2,7 +2,7 @@ import React, { useEffect, useRef, useState } from 'react';
|
||||
import { Link, Navigate, useNavigate, useParams } from 'react-router-dom';
|
||||
import { CaretRightIcon, CheckIcon, CircleNotchIcon, PlayIcon } from '@phosphor-icons/react';
|
||||
import { Comment, Course, Lesson } from '../types';
|
||||
import { getComments, incrementViews, saveComment, saveLessonProgress } from '../services/db';
|
||||
import { getComments, getLessonNote, incrementViews, saveComment, saveLessonNote, saveLessonProgress } from '../services/db';
|
||||
import { useCourse, useLessonProgress } from '../hooks/useCourse';
|
||||
import { useAuth } from '../context/AuthContext';
|
||||
import { useToast } from '../context/ToastContext';
|
||||
@@ -26,9 +26,34 @@ export const useLessonScreen = (course: Course | undefined, lesson: Lesson | und
|
||||
const [comments, setComments] = useState<Comment[]>([]);
|
||||
const [draft, setDraft] = useState('');
|
||||
const [posting, setPosting] = useState(false);
|
||||
const [note, setNote] = useState('');
|
||||
const [noteSaving, setNoteSaving] = useState(false);
|
||||
const advanceTimer = useRef<number>();
|
||||
const trackedView = useRef<string>('');
|
||||
|
||||
useEffect(() => {
|
||||
if (!user || !lesson) { setNote(''); return; }
|
||||
let cancelled = false;
|
||||
getLessonNote(lesson.id)
|
||||
.then((data) => { if (!cancelled) setNote(data.body); })
|
||||
.catch(() => { if (!cancelled) setNote(''); });
|
||||
return () => { cancelled = true; };
|
||||
}, [lesson?.id, user?.id]);
|
||||
|
||||
const saveNote = async () => {
|
||||
if (!lesson) return;
|
||||
setNoteSaving(true);
|
||||
try {
|
||||
const saved = await saveLessonNote(lesson.id, note);
|
||||
setNote(saved.body);
|
||||
showToast('Anotação salva.', 'success');
|
||||
} catch {
|
||||
showToast('Não foi possível salvar a anotação.', 'error');
|
||||
} finally {
|
||||
setNoteSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!user || !course || !lesson) return;
|
||||
const key = `${course.id}:${lesson.id}`;
|
||||
@@ -86,7 +111,7 @@ export const useLessonScreen = (course: Course | undefined, lesson: Lesson | und
|
||||
|
||||
const materials = lesson?.attachments || [];
|
||||
const courseLevel = course?.attachments || [];
|
||||
return { progress, comments, draft, setDraft, posting, postComment, onTime, onEnded, index, nextLesson, materials, courseLevel };
|
||||
return { progress, comments, draft, setDraft, posting, postComment, onTime, onEnded, index, nextLesson, materials, courseLevel, note, setNote, noteSaving, saveNote };
|
||||
};
|
||||
|
||||
export const CommentItem: React.FC<{ comment: Comment; lessonNumber?: number }> = ({ comment, lessonNumber }) => (
|
||||
@@ -134,6 +159,7 @@ export const LessonPage: React.FC = () => {
|
||||
{tab === 'materiais' && <section className="m-group"><div className="m-list">{[...screen.materials, ...screen.courseLevel].map((attachment) => <div className="m-row" key={attachment.id}><span className="m-ic">{attachmentIcon(attachment.type)}</span><span className="m-rt"><b>{attachment.name}</b><small>{attachment.size}</small></span><MaterialButton attachment={attachment} action={action} label={false} className="m-icb" /></div>)}</div>{screen.materials.length + screen.courseLevel.length === 0 && <p className="m-empty">Esta aula não tem materiais.</p>}</section>}
|
||||
{tab === 'discussao' && <><form className="m-compose" onSubmit={screen.postComment}><textarea rows={1} value={screen.draft} onChange={(event) => screen.setDraft(event.target.value)} placeholder="Escreva uma pergunta" aria-label="Escrever comentário" /><button className="btn m-sm" type="submit" disabled={!screen.draft.trim() || screen.posting}>Enviar</button></form><div className="m-thread">{comments.map((comment) => <CommentItem key={comment.id} comment={comment} />)}{!comments.length && <p className="m-empty">Nenhuma pergunta ainda.</p>}</div></>}
|
||||
</div>
|
||||
<section className="m-group"><h2>Anotações</h2><div className="m-list"><label className="m-field col"><span>Suas anotações desta aula</span><textarea rows={4} value={screen.note} onChange={(event) => screen.setNote(event.target.value)} placeholder="Escreva aqui…" /></label></div><button className="link" type="button" style={{ marginTop: 10 }} onClick={() => void screen.saveNote()} disabled={screen.noteSaving}>{screen.noteSaving ? 'Salvando…' : 'Salvar anotação'}</button></section>
|
||||
</div>
|
||||
<div className="m-cta"><button type="button" className="btn sec m-half" onClick={() => progress.toggleCompleted(lesson.id)} aria-pressed={done}><CheckIcon className="ph" />{done ? 'Vista' : 'Marcar vista'}</button>{nextLesson ? <Link className="btn m-half" to={paths.lesson(course.id, nextLesson.id)}>Próxima aula</Link> : <Link className="btn m-half" to={paths.course(course.id)}>Concluir</Link>}</div>
|
||||
</main>;
|
||||
@@ -208,6 +234,7 @@ export const LessonPage: React.FC = () => {
|
||||
<p className="small num" style={{ fontSize: 13 }}>{progress.doneCount} de {course.lessons.length} aulas concluídas</p>
|
||||
</Link>
|
||||
{lesson.description && <div className="box"><h3>Sobre esta aula</h3><p className="small" style={{ whiteSpace: 'pre-line' }}>{lesson.description}</p></div>}
|
||||
<div className="box"><h3>Anotações</h3><div className="field"><textarea rows={4} value={screen.note} onChange={(event) => screen.setNote(event.target.value)} placeholder="Suas anotações desta aula…" aria-label="Anotações da aula" /></div><button className="btn sec" type="button" onClick={() => void screen.saveNote()} disabled={screen.noteSaving}>{screen.noteSaving ? 'Salvando…' : 'Salvar anotação'}</button></div>
|
||||
</aside>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
Reference in New Issue
Block a user