2024-03-27 18:12:17 -05:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2023-12-23 20:33:25 -06:00
|
|
|
import { Carousel } from 'primereact/carousel';
|
2024-02-27 18:29:57 -06:00
|
|
|
import { parseEvent } from '@/utils/nostr';
|
2024-03-19 17:47:16 -05:00
|
|
|
import { useNostr } from '@/hooks/useNostr';
|
2024-03-26 18:14:32 -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-02-27 18:29:57 -06:00
|
|
|
|
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-04-22 15:46:52 -05:00
|
|
|
const [loading, setLoading] = useState(true);
|
2024-04-20 16:16:22 -05:00
|
|
|
const { fetchCourses } = useNostr();
|
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 () => {
|
2024-04-22 15:46:52 -05:00
|
|
|
setLoading(true);
|
2024-04-20 16:16:22 -05:00
|
|
|
try {
|
2024-04-22 15:46:52 -05:00
|
|
|
const fetchedCourses = await fetchCourses();
|
|
|
|
if (fetchedCourses && fetchedCourses.length > 0) {
|
|
|
|
const processed = fetchedCourses.map(course => parseEvent(course));
|
|
|
|
setProcessedCourses(processed);
|
|
|
|
} else {
|
|
|
|
console.log('No courses fetched or empty array returned');
|
|
|
|
}
|
|
|
|
setLoading(false);
|
2024-04-20 16:16:22 -05:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Error fetching courses:', error);
|
2024-04-22 15:46:52 -05:00
|
|
|
setLoading(false);
|
2024-04-20 16:16:22 -05:00
|
|
|
}
|
2024-04-22 15:46:52 -05:00
|
|
|
};
|
2024-04-20 16:16:22 -05:00
|
|
|
fetch();
|
2024-04-22 15:46:52 -05:00
|
|
|
}, [fetchCourses]);
|
2023-12-23 20:33:25 -06:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2024-04-22 15:46:52 -05:00
|
|
|
<h2 className="ml-[6%] mt-4">Courses</h2>
|
|
|
|
<Carousel value={loading ? [{}, {}, {}] : [...processedCourses, ...processedCourses]}
|
|
|
|
numVisible={2}
|
|
|
|
itemTemplate={loading ? TemplateSkeleton : CourseTemplate}
|
|
|
|
responsiveOptions={responsiveOptions} />
|
2023-12-23 20:33:25 -06:00
|
|
|
</>
|
2024-02-27 18:29:57 -06:00
|
|
|
);
|
2023-12-23 20:33:25 -06:00
|
|
|
}
|