feat: add reviews, notifications, notes and account hardening
This commit is contained in:
137
components/course/CourseReviews.tsx
Normal file
137
components/course/CourseReviews.tsx
Normal file
@@ -0,0 +1,137 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { StarIcon } from '@phosphor-icons/react';
|
||||
import { useAuth } from '../../context/AuthContext';
|
||||
import { useToast } from '../../context/ToastContext';
|
||||
import { CourseReviewSummary, MyCourseReview, deleteMyCourseReview, getCourseReviews, getMyCourseReview, saveCourseReview } from '../../services/db';
|
||||
import { ConfirmDialog } from '../ui/ConfirmDialog';
|
||||
import { shortDate } from '../../lib/format';
|
||||
import { paths } from '../../lib/routes';
|
||||
|
||||
const Stars: React.FC<{ value: number; label?: string }> = ({ value, label }) => (
|
||||
<span className="review-stars" role="img" aria-label={label || `${value} de 5 estrelas`}>
|
||||
{[1, 2, 3, 4, 5].map((star) => <StarIcon key={star} weight={star <= Math.round(value) ? 'fill' : 'regular'} />)}
|
||||
</span>
|
||||
);
|
||||
|
||||
export const CourseReviews: React.FC<{ courseId: string }> = ({ courseId }) => {
|
||||
const { user } = useAuth();
|
||||
const { showToast } = useToast();
|
||||
const [summary, setSummary] = useState<CourseReviewSummary | null>(null);
|
||||
const [mine, setMine] = useState<MyCourseReview | null>(null);
|
||||
const [rating, setRating] = useState(0);
|
||||
const [comment, setComment] = useState('');
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [deleteOpen, setDeleteOpen] = useState(false);
|
||||
|
||||
const load = async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const [publicReviews, myReview] = await Promise.all([
|
||||
getCourseReviews(courseId),
|
||||
user ? getMyCourseReview(courseId).catch(() => null) : Promise.resolve(null),
|
||||
]);
|
||||
setSummary(publicReviews);
|
||||
setMine(myReview);
|
||||
setRating(myReview?.rating ?? 0);
|
||||
setComment(myReview?.comment ?? '');
|
||||
} catch {
|
||||
setSummary(null);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
void load();
|
||||
}, [courseId, user?.id]);
|
||||
|
||||
const submit = async (event: React.FormEvent) => {
|
||||
event.preventDefault();
|
||||
if (!rating) {
|
||||
showToast('Escolha uma nota de 1 a 5 estrelas.', 'error');
|
||||
return;
|
||||
}
|
||||
setSaving(true);
|
||||
try {
|
||||
const result = await saveCourseReview(courseId, rating, comment.trim());
|
||||
setMine(result.review);
|
||||
setSummary(result.summary);
|
||||
setRating(result.review.rating);
|
||||
setComment(result.review.comment);
|
||||
showToast('Avaliação salva.', 'success');
|
||||
} catch {
|
||||
showToast('Não foi possível salvar sua avaliação.', 'error');
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const remove = async () => {
|
||||
setDeleteOpen(false);
|
||||
try {
|
||||
await deleteMyCourseReview(courseId);
|
||||
setMine(null);
|
||||
setRating(0);
|
||||
setComment('');
|
||||
setSummary(await getCourseReviews(courseId));
|
||||
showToast('Avaliação removida.', 'success');
|
||||
} catch {
|
||||
showToast('Não foi possível remover sua avaliação.', 'error');
|
||||
}
|
||||
};
|
||||
|
||||
const average = summary?.average ?? 0;
|
||||
const count = summary?.count ?? 0;
|
||||
|
||||
return <section className="reviews">
|
||||
<header className="reviews-header">
|
||||
<h2>Avaliações</h2>
|
||||
{count > 0 && <span className="count num">{count} {count === 1 ? 'avaliação' : 'avaliações'}</span>}
|
||||
</header>
|
||||
|
||||
{loading ? <p className="muted">Carregando avaliações…</p> : <>
|
||||
<div className="reviews-layout">
|
||||
<div className="box review-summary">
|
||||
<b className="review-average num">{count ? average.toFixed(1) : '—'}</b>
|
||||
<Stars value={average} label={count ? `Média ${average.toFixed(1)} de 5` : 'Sem avaliações'} />
|
||||
<small>{count ? `${count} ${count === 1 ? 'pessoa avaliou' : 'pessoas avaliaram'}` : 'Seja a primeira pessoa a avaliar'}</small>
|
||||
</div>
|
||||
|
||||
{user ? <form className="box review-form" onSubmit={submit}>
|
||||
<div>
|
||||
<span className="review-form-label">{mine ? 'Sua avaliação' : 'Avalie este curso'}</span>
|
||||
<div className="review-rating" role="group" aria-label="Escolha sua nota">
|
||||
{[1, 2, 3, 4, 5].map((star) => <button key={star} type="button" aria-label={`${star} ${star === 1 ? 'estrela' : 'estrelas'}`} aria-pressed={rating === star} onClick={() => setRating(star)}><StarIcon weight={star <= rating ? 'fill' : 'regular'} /></button>)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="field">
|
||||
<label htmlFor={`review-comment-${courseId}`}>Comentário <span className="faint">(opcional)</span></label>
|
||||
<textarea id={`review-comment-${courseId}`} value={comment} onChange={(event) => setComment(event.target.value)} maxLength={2000} placeholder="Conte o que você achou do curso…" />
|
||||
</div>
|
||||
<div className="review-actions">
|
||||
<button className="btn" type="submit" disabled={saving}>{saving ? 'Salvando…' : mine ? 'Atualizar avaliação' : 'Publicar avaliação'}</button>
|
||||
{mine && <button className="link bad-text" type="button" onClick={() => setDeleteOpen(true)}>Remover minha avaliação</button>}
|
||||
</div>
|
||||
</form> : <div className="box review-signin">
|
||||
<b>Gostou do curso?</b>
|
||||
<p className="small">Entre na sua conta para avaliar e ajudar outras pessoas.</p>
|
||||
<Link className="btn sec" to={paths.signIn}>Entrar para avaliar</Link>
|
||||
</div>}
|
||||
</div>
|
||||
|
||||
{summary?.reviews.length ? <div className="review-list">
|
||||
{summary.reviews.map((review) => <article className="review-item" key={`${review.userName}-${review.updatedAt}`}>
|
||||
<header>
|
||||
<div><b>{review.userName}</b><Stars value={review.rating} /></div>
|
||||
<small>{shortDate(review.updatedAt)}</small>
|
||||
</header>
|
||||
{review.comment && <p>{review.comment}</p>}
|
||||
</article>)}
|
||||
</div> : <p className="empty">Nenhuma avaliação publicada ainda.</p>}
|
||||
</>}
|
||||
|
||||
<ConfirmDialog open={deleteOpen} title="Remover avaliação" message="Sua avaliação será removida permanentemente." confirmLabel="Remover avaliação" onCancel={() => setDeleteOpen(false)} onConfirm={() => void remove()} />
|
||||
</section>;
|
||||
};
|
||||
Reference in New Issue
Block a user