205 lines
6.1 KiB
TypeScript
205 lines
6.1 KiB
TypeScript
import { Link } from 'react-router-dom'
|
|
import { Icon } from '../components/Icon'
|
|
import type { IconName } from '../components/Icon'
|
|
import { Badge } from '../components/ui'
|
|
import type { CourseLevel, LessonType, PublicCourseCard } from '../api/types'
|
|
|
|
/**
|
|
* Drobne czesci wspolne dla stron Akademii.
|
|
*
|
|
* Kafelek kursu pokazuje sie na stronie glownej, na liscie kursow i w cenniku,
|
|
* a pasek postepu na pulpicie i w odtwarzaczu. Trzy kopie tego samego kafelka
|
|
* rozjechalyby sie przy pierwszej poprawce.
|
|
*/
|
|
|
|
export const LEVEL_LABEL: Record<CourseLevel, string> = {
|
|
BASIC: 'Podstawowy',
|
|
INTERMEDIATE: 'Średniozaawansowany',
|
|
ADVANCED: 'Zaawansowany',
|
|
}
|
|
|
|
export const LESSON_TYPE_LABEL: Record<LessonType, string> = {
|
|
VIDEO: 'Nagranie',
|
|
TEXT: 'Materiał do czytania',
|
|
FILE: 'Plik do pobrania',
|
|
WEBINAR: 'Webinar',
|
|
QUIZ: 'Sprawdzian',
|
|
}
|
|
|
|
export const LESSON_TYPE_ICON: Record<LessonType, IconName> = {
|
|
VIDEO: 'play',
|
|
TEXT: 'file-text',
|
|
FILE: 'download',
|
|
WEBINAR: 'presentation',
|
|
QUIZ: 'list-checks',
|
|
}
|
|
|
|
/**
|
|
* Cena brutto albo oznaczenie kursu darmowego.
|
|
*
|
|
* Brak ceny znaczy "darmowy", a nie "cena zero": to dwie rozne decyzje,
|
|
* rozstrzygniete po stronie backendu przez `Course.isFree()`.
|
|
*/
|
|
export function formatPrice(price: string | number | null, currency: string) {
|
|
if (price === null || price === undefined) {
|
|
return 'Bezpłatny'
|
|
}
|
|
const value = typeof price === 'string' ? Number(price) : price
|
|
if (!Number.isFinite(value)) {
|
|
return 'Bezpłatny'
|
|
}
|
|
return new Intl.NumberFormat('pl-PL', {
|
|
style: 'currency',
|
|
currency: currency || 'PLN',
|
|
minimumFractionDigits: 2,
|
|
}).format(value)
|
|
}
|
|
|
|
/** Czas trwania w formie, w ktorej mysli czlowiek: godziny i minuty. */
|
|
export function formatCourseDuration(minutes: number | null) {
|
|
if (!minutes || minutes <= 0) {
|
|
return null
|
|
}
|
|
const hours = Math.floor(minutes / 60)
|
|
const rest = minutes % 60
|
|
if (hours === 0) {
|
|
return `${rest} min`
|
|
}
|
|
return rest === 0 ? `${hours} godz.` : `${hours} godz. ${rest} min`
|
|
}
|
|
|
|
/** Adres okladki. Pusty identyfikator oznacza kafelek bez obrazka. */
|
|
export function mediaUrl(mediaId: number | null) {
|
|
return mediaId ? `/api/public/media/${mediaId}` : null
|
|
}
|
|
|
|
export function CourseCover({
|
|
mediaId,
|
|
title,
|
|
className = '',
|
|
}: {
|
|
mediaId: number | null
|
|
title: string
|
|
className?: string
|
|
}) {
|
|
const url = mediaUrl(mediaId)
|
|
if (!url) {
|
|
return (
|
|
<div
|
|
className={`flex items-center justify-center bg-gradient-hydro ${className}`}
|
|
aria-hidden="true"
|
|
>
|
|
<Icon name="graduation-cap" size={32} className="text-white/70" />
|
|
</div>
|
|
)
|
|
}
|
|
return (
|
|
<img
|
|
src={url}
|
|
alt={`Okładka kursu ${title}`}
|
|
loading="lazy"
|
|
className={`object-cover ${className}`}
|
|
/>
|
|
)
|
|
}
|
|
|
|
/** Kafelek kursu na liscie. */
|
|
export function CourseCard({ course }: { course: PublicCourseCard }) {
|
|
const duration = formatCourseDuration(course.durationMinutes)
|
|
|
|
return (
|
|
<Link
|
|
to={`/kurs/${course.slug}`}
|
|
className="group flex flex-col overflow-hidden rounded-lg border border-slate-100 bg-white shadow-sm transition-shadow duration-fast ease-standard hover:shadow-md focus-visible:outline-none focus-visible:ring-3 focus-visible:ring-blue-500/45"
|
|
>
|
|
<CourseCover mediaId={course.coverMediaId} title={course.title} className="h-40 w-full" />
|
|
|
|
<div className="flex flex-1 flex-col p-4">
|
|
<div className="flex flex-wrap items-center gap-1.5">
|
|
{course.category && <Badge tone="info">{course.category}</Badge>}
|
|
{course.free ? (
|
|
<Badge tone="ok">Bezpłatny</Badge>
|
|
) : (
|
|
<Badge tone="brand">{formatPrice(course.priceGross, course.currency)}</Badge>
|
|
)}
|
|
</div>
|
|
|
|
<h3 className="mt-2.5 text-base font-semibold leading-snug text-slate-900 group-hover:text-blue-600">
|
|
{course.title}
|
|
</h3>
|
|
{course.subtitle && (
|
|
<p className="mt-1 line-clamp-2 text-sm leading-relaxed text-slate-600">
|
|
{course.subtitle}
|
|
</p>
|
|
)}
|
|
|
|
<div className="mt-auto flex flex-wrap items-center gap-x-4 gap-y-1.5 pt-3 text-xs text-slate-500">
|
|
<span className="flex items-center gap-1.5">
|
|
<Icon name="book-open" size={14} />
|
|
<span className="hx-data">{course.lessonCount}</span>
|
|
{lessonWord(course.lessonCount)}
|
|
</span>
|
|
{duration && (
|
|
<span className="flex items-center gap-1.5">
|
|
<Icon name="clock" size={14} />
|
|
<span className="hx-data">{duration}</span>
|
|
</span>
|
|
)}
|
|
{course.level && <span>{LEVEL_LABEL[course.level]}</span>}
|
|
{course.certificateEnabled && (
|
|
<span className="flex items-center gap-1.5 text-blue-600">
|
|
<Icon name="award" size={14} />
|
|
Certyfikat
|
|
</span>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</Link>
|
|
)
|
|
}
|
|
|
|
/** Odmiana slowa "lekcja" przez liczbe: 1 lekcja, 2 lekcje, 5 lekcji. */
|
|
export function lessonWord(count: number) {
|
|
if (count === 1) return 'lekcja'
|
|
const rest10 = count % 10
|
|
const rest100 = count % 100
|
|
if (rest10 >= 2 && rest10 <= 4 && (rest100 < 12 || rest100 > 14)) return 'lekcje'
|
|
return 'lekcji'
|
|
}
|
|
|
|
/** Pasek postepu kursu. Liczba pochodzi z backendu, z lekcji wymaganych. */
|
|
export function ProgressBar({
|
|
percent,
|
|
label,
|
|
className = '',
|
|
}: {
|
|
percent: number
|
|
label?: string
|
|
className?: string
|
|
}) {
|
|
const safe = Math.max(0, Math.min(100, percent))
|
|
return (
|
|
<div className={className}>
|
|
{label && (
|
|
<div className="mb-1.5 flex items-center justify-between text-xs text-slate-600">
|
|
<span>{label}</span>
|
|
<span className="hx-data font-medium text-slate-900">{safe}%</span>
|
|
</div>
|
|
)}
|
|
<div
|
|
className="h-2 overflow-hidden rounded-full bg-slate-100"
|
|
role="progressbar"
|
|
aria-valuenow={safe}
|
|
aria-valuemin={0}
|
|
aria-valuemax={100}
|
|
aria-label={label || 'Postęp kursu'}
|
|
>
|
|
<div
|
|
className="h-full rounded-full bg-blue-500 transition-[width] duration-base ease-standard"
|
|
style={{ width: `${safe}%` }}
|
|
/>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|