plebdevs/src/components/content/carousels/CoursesCarousel.js

61 lines
1.9 KiB
JavaScript
Raw Normal View History

import React, { useState, useEffect } from 'react';
import { Carousel } from 'primereact/carousel';
import { parseEvent } from '@/utils/nostr';
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';
const responsiveOptions = [
{
breakpoint: '3000px',
numVisible: 3,
numScroll: 1
},
{
breakpoint: '1462px',
numVisible: 2,
numScroll: 1
},
{
breakpoint: '575px',
numVisible: 1,
numScroll: 1
}
];
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();
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]);
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} />
</>
);
}