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-07-22 17:16:36 -05:00
|
|
|
import { parseCourseEvent } 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-23 18:52:55 -05:00
|
|
|
const { fetchCourses, fetchZapsForEvents } = 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 () => {
|
|
|
|
try {
|
2024-04-22 15:46:52 -05:00
|
|
|
const fetchedCourses = await fetchCourses();
|
|
|
|
if (fetchedCourses && fetchedCourses.length > 0) {
|
2024-04-23 18:52:55 -05:00
|
|
|
// First process the courses to be ready for display
|
2024-07-22 17:16:36 -05:00
|
|
|
const processedCourses = fetchedCourses.map(course => parseCourseEvent(course));
|
2024-04-23 21:30:54 -05:00
|
|
|
|
2024-04-23 18:52:55 -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
|
|
|
|
2024-04-23 18:52:55 -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
|
|
|
|
2024-04-23 18:52:55 -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();
|
2024-04-23 18:52:55 -05:00
|
|
|
}, [fetchCourses, fetchZapsForEvents]);
|
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>
|
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>
|
2023-12-23 20:33:25 -06:00
|
|
|
</>
|
2024-02-27 18:29:57 -06:00
|
|
|
);
|
2023-12-23 20:33:25 -06:00
|
|
|
}
|