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

80 lines
3.0 KiB
JavaScript
Raw Normal View History

import React, { useState, useEffect } from 'react';
import { Carousel } from 'primereact/carousel';
import { parseCourseEvent } 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([]);
const { fetchCourses, fetchZapsForEvents } = useNostr();
useEffect(() => {
2024-04-20 16:16:22 -05:00
const fetch = async () => {
try {
2024-04-22 15:46:52 -05:00
const fetchedCourses = await fetchCourses();
if (fetchedCourses && fetchedCourses.length > 0) {
// First process the courses to be ready for display
const processedCourses = fetchedCourses.map(course => parseCourseEvent(course));
2024-04-23 21:30:54 -05:00
// Fetch zaps for all processed courses at once
const allZaps = await fetchZapsForEvents(processedCourses);
console.log('allZaps:', allZaps);
2024-04-23 21:30:54 -05:00
// Process zaps to associate them with their respective courses
const coursesWithZaps = processedCourses.map(course => {
const relevantZaps = allZaps.filter(zap => {
const eTagMatches = zap.tags.find(tag => tag[0] === 'e' && tag[1] === course.id);
const aTag = zap.tags.find(tag => tag[0] === 'a');
const aTagMatches = aTag && course.d === aTag[1].split(':').pop();
return eTagMatches || aTagMatches;
});
return {
...course,
zaps: relevantZaps
};
});
2024-04-23 21:30:54 -05:00
setProcessedCourses(coursesWithZaps);
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();
}, [fetchCourses, fetchZapsForEvents]);
return (
<>
2024-04-22 15:46:52 -05:00
<h2 className="ml-[6%] mt-4">Courses</h2>
2024-04-23 21:30:54 -05:00
<div className={"min-h-[384px]"}>
<Carousel
2024-07-31 16:41:18 -05:00
value={!processedCourses.length > 0 ? [{}, {}, {}] : [...processedCourses]}
2024-04-23 21:30:54 -05:00
numVisible={2}
itemTemplate={!processedCourses.length > 0 ? TemplateSkeleton : CourseTemplate}
responsiveOptions={responsiveOptions} />
</div>
</>
);
}