2024-10-16 20:14:40 -05:00
|
|
|
import React, { useState, useEffect, use } from 'react';
|
2023-12-23 20:33:25 -06:00
|
|
|
import { Carousel } from 'primereact/carousel';
|
2024-07-22 17:16:36 -05:00
|
|
|
import { parseCourseEvent } from '@/utils/nostr';
|
2024-09-15 13:27:37 -05:00
|
|
|
import { CourseTemplate } from '@/components/content/carousels/templates/CourseTemplate';
|
2024-04-22 15:46:52 -05:00
|
|
|
import TemplateSkeleton from '@/components/content/carousels/skeletons/TemplateSkeleton';
|
2024-08-26 17:33:26 -05:00
|
|
|
import { useCourses } from '@/hooks/nostr/useCourses';
|
2024-09-16 16:10:28 -05:00
|
|
|
import useWindowWidth from '@/hooks/useWindowWidth';
|
|
|
|
import { Divider } from 'primereact/divider';
|
2024-03-17 17:32:23 -05:00
|
|
|
const responsiveOptions = [
|
|
|
|
{
|
2024-03-19 12:32:05 -05:00
|
|
|
breakpoint: '3000px',
|
2024-03-17 17:32:23 -05:00
|
|
|
numVisible: 3,
|
|
|
|
numScroll: 1
|
|
|
|
},
|
|
|
|
{
|
2024-03-19 12:32:05 -05:00
|
|
|
breakpoint: '1462px',
|
2024-03-17 17:32:23 -05:00
|
|
|
numVisible: 2,
|
|
|
|
numScroll: 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
breakpoint: '575px',
|
|
|
|
numVisible: 1,
|
|
|
|
numScroll: 1
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2024-02-27 18:29:57 -06:00
|
|
|
export default function CoursesCarousel() {
|
|
|
|
const [processedCourses, setProcessedCourses] = useState([]);
|
2024-08-26 17:33:26 -05:00
|
|
|
const { courses, coursesLoading, coursesError } = useCourses()
|
2024-09-16 16:10:28 -05:00
|
|
|
const windowWidth = useWindowWidth();
|
|
|
|
const isMobileView = windowWidth <= 450;
|
2024-02-27 18:29:57 -06:00
|
|
|
|
2024-03-19 17:47:16 -05:00
|
|
|
useEffect(() => {
|
2024-04-20 16:16:22 -05:00
|
|
|
const fetch = async () => {
|
|
|
|
try {
|
2024-08-04 18:00:59 -05:00
|
|
|
if (courses && courses.length > 0) {
|
2024-08-03 13:42:46 -05:00
|
|
|
const processedCourses = courses.map(course => parseCourseEvent(course));
|
2024-10-16 20:14:40 -05:00
|
|
|
|
|
|
|
// Sort courses by created_at in descending order (most recent first)
|
|
|
|
const sortedCourses = processedCourses.sort((a, b) => b.created_at - a.created_at);
|
|
|
|
|
|
|
|
setProcessedCourses(sortedCourses);
|
2024-04-22 15:46:52 -05:00
|
|
|
} else {
|
|
|
|
console.log('No courses fetched or empty array returned');
|
|
|
|
}
|
2024-04-20 16:16:22 -05:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Error fetching courses:', error);
|
|
|
|
}
|
2024-04-23 21:30:54 -05:00
|
|
|
};
|
2024-04-20 16:16:22 -05:00
|
|
|
fetch();
|
2024-08-04 18:00:59 -05:00
|
|
|
}, [courses]);
|
2024-08-03 13:42:46 -05:00
|
|
|
|
|
|
|
if (coursesError) {
|
|
|
|
return <div>Error: {coursesError.message}</div>
|
|
|
|
}
|
2023-12-23 20:33:25 -06:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2024-09-16 16:10:28 -05:00
|
|
|
<h3 className={`ml-[6%] mt-4 max-mob:text-3xl max-mob:ml-10`}>Courses</h3>
|
|
|
|
<Divider className={`${isMobileView ? '' : 'hidden'}`} />
|
2024-04-23 21:30:54 -05:00
|
|
|
<div className={"min-h-[384px]"}>
|
|
|
|
<Carousel
|
2024-08-05 15:25:04 -05:00
|
|
|
value={coursesLoading || !processedCourses.length ? [{}, {}, {}] : [...processedCourses]}
|
2024-04-23 21:30:54 -05:00
|
|
|
numVisible={2}
|
2024-09-16 16:10:28 -05:00
|
|
|
pt={{
|
|
|
|
previousButton: {
|
|
|
|
className: isMobileView ? 'm-0' : ''
|
|
|
|
},
|
|
|
|
nextButton: {
|
|
|
|
className: isMobileView ? 'm-0' : ''
|
|
|
|
}
|
|
|
|
}}
|
2024-08-01 17:10:43 -05:00
|
|
|
itemTemplate={(item) =>
|
2024-08-05 15:25:04 -05:00
|
|
|
!processedCourses.length ?
|
|
|
|
<TemplateSkeleton key={Math.random()} /> :
|
2024-10-17 09:23:46 -05:00
|
|
|
<CourseTemplate key={item.id} course={item} showMetaTags={false} />
|
2024-08-01 17:10:43 -05:00
|
|
|
}
|
2024-04-23 21:30:54 -05:00
|
|
|
responsiveOptions={responsiveOptions} />
|
|
|
|
</div>
|
2023-12-23 20:33:25 -06:00
|
|
|
</>
|
2024-02-27 18:29:57 -06:00
|
|
|
);
|
2023-12-23 20:33:25 -06:00
|
|
|
}
|