86 lines
9.2 KiB
TypeScript
86 lines
9.2 KiB
TypeScript
import { useEffect, useState } from "react"
|
|
import { Link, useParams, useSearchParams } from "react-router-dom"
|
|
import { ArrowLeft, ArrowRight, BookOpen, CheckCircle2, LockKeyhole, Search } from "lucide-react"
|
|
import { BrandLogo } from "../../components/brand/BrandLogo"
|
|
import { catalogThumbnail, getPublicCourse, getPublicCourses, type PublicCatalogPage as Catalog, type PublicCourseOutline } from "../../api/publicCatalog.api"
|
|
|
|
const learnerApp = import.meta.env.VITE_LEARNER_APP_URL as string | undefined
|
|
|
|
export function PublicCatalogPage() {
|
|
const { courseId } = useParams<{ courseId: string }>()
|
|
const [params, setParams] = useSearchParams()
|
|
const search = params.get("q") ?? ""
|
|
const offset = Math.max(0, Number(params.get("offset")) || 0)
|
|
const [draftSearch, setDraftSearch] = useState(search)
|
|
const [catalog, setCatalog] = useState<Catalog | null>(null)
|
|
const [course, setCourse] = useState<PublicCourseOutline | null>(null)
|
|
const [loading, setLoading] = useState(true)
|
|
const [error, setError] = useState("")
|
|
const [retry, setRetry] = useState(0)
|
|
|
|
useEffect(() => {
|
|
const controller = new AbortController()
|
|
async function load() {
|
|
setLoading(true); setError(""); setCourse(null)
|
|
try {
|
|
if (courseId) {
|
|
const id = Number(courseId)
|
|
if (!Number.isSafeInteger(id) || id < 1) throw new Error("This course is not available.")
|
|
const outline = await getPublicCourse(id, controller.signal)
|
|
if (!controller.signal.aborted) setCourse(outline)
|
|
} else {
|
|
const page = await getPublicCourses(search, offset, controller.signal)
|
|
if (!controller.signal.aborted) setCatalog(page)
|
|
}
|
|
} catch (problem) {
|
|
if (!controller.signal.aborted) setError(problem instanceof Error ? problem.message : "Unable to load courses.")
|
|
} finally { if (!controller.signal.aborted) setLoading(false) }
|
|
}
|
|
void load()
|
|
return () => controller.abort()
|
|
}, [courseId, search, offset, retry])
|
|
|
|
return <div className="min-h-screen bg-[#f7f6f2] text-[#163d35]">
|
|
<header className="border-b border-[#163d35]/10 bg-white"><div className="mx-auto flex max-w-6xl items-center justify-between gap-4 px-5 py-5">
|
|
<Link to="/catalog" aria-label="Rezina course catalog"><BrandLogo className="h-10" /></Link>
|
|
<nav className="flex items-center gap-5 text-sm"><Link to="/catalog" className="font-semibold">Explore courses</Link><Link to="/login" className="rounded-full border border-[#163d35]/20 px-4 py-2">Staff sign in</Link></nav>
|
|
</div></header>
|
|
<main className="mx-auto max-w-6xl px-5 py-10 sm:py-16">
|
|
{courseId ? <Link to={`/catalog${params.size ? `?${params}` : ""}`} className="mb-8 inline-flex items-center gap-2 text-sm"><ArrowLeft size={16} />All courses</Link> : <section className="mb-10 max-w-3xl">
|
|
<p className="mb-4 text-xs font-semibold uppercase tracking-[.24em] text-[#987343]">Your next chapter starts here</p>
|
|
<h1 className="text-4xl font-semibold leading-tight tracking-tight sm:text-6xl">Build skills.<br />Open new possibilities.</h1>
|
|
<p className="mt-5 max-w-xl text-lg leading-relaxed text-[#163d35]/70">Explore our courses, discover what you will learn, and find the right place to begin. Browse every published course without an account.</p>
|
|
<form onSubmit={event => { event.preventDefault(); setParams(draftSearch.trim() ? { q: draftSearch.trim() } : {}) }} className="mt-7 flex max-w-xl gap-2 rounded-xl border border-[#163d35]/15 bg-white p-2">
|
|
<label className="flex flex-1 items-center gap-3 px-2"><Search size={20} /><input aria-label="Search courses" className="min-w-0 flex-1 bg-transparent py-2 outline-none" placeholder="What would you like to learn?" maxLength={200} value={draftSearch} onChange={event => setDraftSearch(event.target.value)} /></label>
|
|
<button className="rounded-lg bg-[#163d35] px-5 text-sm font-semibold text-white">Search</button>
|
|
</form>
|
|
</section>}
|
|
{loading ? <p role="status" className="py-16 text-center">Loading courses...</p> : error ? <div role="alert" className="rounded-2xl border bg-white p-8"><p>{error}</p><button className="mt-4 underline" onClick={() => setRetry(value => value + 1)}>Try again</button></div> : course ? <>
|
|
<section className="grid gap-8 rounded-3xl bg-[#163d35] p-7 text-white sm:p-10 md:grid-cols-[1fr_280px]">
|
|
<div><p className="mb-3 text-sm text-[#e4c28f]">{course.access_tier === "PREMIUM" ? "Premium course" : "Free course"}</p><h1 className="text-3xl font-semibold sm:text-5xl">{course.name}</h1><p className="mt-5 leading-relaxed text-white/75">{course.description}</p>
|
|
<p className="mt-5 text-sm text-white/70">{course.chapter_count} chapters · {course.lesson_count} lessons · {course.quiz_count} quizzes</p>
|
|
{learnerApp ? <a href={learnerApp} className="mt-7 inline-flex rounded-full bg-[#e4c28f] px-6 py-3 font-semibold text-[#163d35]">Sign in to learn<ArrowRight className="ml-2" size={18} /></a> : <p className="mt-7 flex items-center gap-2 text-sm"><LockKeyhole size={17} />Sign in through your learning app to watch lessons and take quizzes.</p>}
|
|
</div><div className="flex items-center justify-center overflow-hidden rounded-xl bg-white/5">{course.thumbnail ? <img className="h-full max-h-64 w-full object-cover" src={catalogThumbnail(course.thumbnail)} alt="" /> : <BookOpen size={80} className="text-[#e4c28f]" />}</div>
|
|
</section>
|
|
<section className="mt-12 max-w-4xl"><h2 className="mb-6 text-2xl font-semibold">What you will learn</h2><div className="space-y-4">
|
|
{course.chapters.map((chapter, index) => <details key={chapter.id} className="rounded-2xl border border-[#163d35]/10 bg-white p-5" open={index === 0}>
|
|
<summary className="cursor-pointer text-lg font-semibold">{index + 1}. {chapter.name}<span className="ml-3 text-sm font-normal text-[#163d35]/60">{chapter.lessons.length} lessons</span></summary>
|
|
<p className="mt-3 text-sm text-[#163d35]/70">{chapter.description}</p>
|
|
<ul className="mt-4 divide-y divide-[#163d35]/10">{chapter.lessons.map(lesson => <li key={lesson.id} className="flex gap-3 py-4"><BookOpen size={18} className="mt-1 shrink-0" /><div><h3 className="font-medium">{lesson.title}</h3><p className="mt-1 text-sm leading-relaxed text-[#163d35]/65">{lesson.description}</p><span className="mt-2 inline-block text-xs text-[#163d35]/60">{lesson.access_tier === "PREMIUM" ? "Premium lesson" : "Free lesson"}</span></div></li>)}
|
|
{chapter.quizzes.map(quiz => <li key={`quiz-${quiz.id}`} className="flex gap-3 py-4"><CheckCircle2 size={18} className="mt-1 shrink-0" /><div><h3 className="font-medium">{quiz.title}</h3><p className="mt-1 text-sm text-[#163d35]/65">Chapter quiz · {quiz.question_count} questions</p></div></li>)}</ul>
|
|
</details>)}
|
|
{!course.chapters.length && <p>The course outline will be available soon.</p>}
|
|
</div></section>
|
|
</> : <section aria-label="Course highlights">
|
|
<div className="mb-6 flex items-center justify-between"><h2 className="text-2xl font-semibold">{search ? `Results for “${search}”` : "Explore our courses"}</h2><span className="text-sm text-[#163d35]/65">{catalog?.total_count ?? 0} courses</span></div>
|
|
<div className="grid gap-6 sm:grid-cols-2 lg:grid-cols-3">{catalog?.courses.map(item => <Link key={item.id} to={`/catalog/courses/${item.id}${params.size ? `?${params}` : ""}`} className="group overflow-hidden rounded-2xl border border-[#163d35]/10 bg-white transition hover:-translate-y-1 hover:shadow-lg">
|
|
<div className="flex h-44 items-center justify-center overflow-hidden bg-[#163d35]">{item.thumbnail ? <img src={catalogThumbnail(item.thumbnail)} alt="" className="h-full w-full object-cover" loading="lazy" /> : <BookOpen size={55} className="text-[#e4c28f]" />}</div>
|
|
<div className="p-6"><span className="text-xs font-semibold uppercase tracking-wide text-[#987343]">{item.access_tier === "PREMIUM" ? "Premium" : "Free"}</span><h3 className="mt-2 text-xl font-semibold">{item.name}</h3><p className="mt-3 line-clamp-3 text-sm leading-relaxed text-[#163d35]/65">{item.description}</p><p className="mt-5 text-xs text-[#163d35]/65">{item.chapter_count} chapters · {item.lesson_count} lessons · {item.quiz_count} quizzes</p><span className="mt-5 inline-flex items-center gap-2 text-sm font-semibold">View course<ArrowRight size={16} /></span></div>
|
|
</Link>)}</div>
|
|
{!catalog?.courses.length && <p className="rounded-2xl border border-dashed p-12 text-center">{search ? "No courses match your search. Try another topic." : "New courses are on their way. Check back soon."}</p>}
|
|
{!!catalog?.total_count && <nav aria-label="Catalog pages" className="mt-8 flex justify-center gap-5"><button className="rounded-full border px-5 py-2 disabled:opacity-40" disabled={offset === 0} onClick={() => setParams({ q: search, offset: String(Math.max(0, offset - 12)) })}>Previous</button><button className="rounded-full border px-5 py-2 disabled:opacity-40" disabled={offset + 12 >= catalog.total_count} onClick={() => setParams({ q: search, offset: String(offset + 12) })}>Next</button></nav>}
|
|
</section>}
|
|
</main><footer className="mx-auto max-w-6xl px-5 pb-10 text-sm text-[#163d35]/60">Rezina · Learn at your own pace.</footer>
|
|
</div>
|
|
}
|