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

107 lines
4.0 KiB
JavaScript
Raw Normal View History

2024-10-16 20:14:40 -05:00
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { Carousel } from 'primereact/carousel';
import { parseEvent } from '@/utils/nostr';
2024-09-15 13:27:37 -05:00
import { DocumentTemplate } from '@/components/content/carousels/templates/DocumentTemplate';
2024-04-22 15:46:52 -05:00
import TemplateSkeleton from '@/components/content/carousels/skeletons/TemplateSkeleton';
import { useDocuments } from '@/hooks/nostr/useDocuments';
2024-09-16 16:10:28 -05:00
import useWindowWidth from '@/hooks/useWindowWidth';
import { nip19 } from 'nostr-tools';
2024-09-16 16:10:28 -05:00
import { Divider } from 'primereact/divider';
const responsiveOptions = [
{
breakpoint: '3000px',
numVisible: 3,
numScroll: 1
},
{
breakpoint: '1462px',
numVisible: 2,
numScroll: 1
},
{
breakpoint: '575px',
numVisible: 1,
numScroll: 1
}
];
export default function DocumentsCarousel() {
const [processedDocuments, setProcessedDocuments] = useState([]);
const [paidLessons, setPaidLessons] = useState([]);
const [freeLessons, setFreeLessons] = useState([]);
const { documents, documentsLoading, documentsError } = useDocuments()
2024-09-16 16:10:28 -05:00
const windowWidth = useWindowWidth();
const isMobileView = windowWidth <= 450;
// todo: cache this in react query
useEffect(() => {
axios.get('/api/lessons').then(res => {
if (res.data) {
res.data.forEach(lesson => {
if (lesson?.resource?.price > 0) {
2024-10-13 18:11:47 -05:00
setPaidLessons(prev => [...prev, lesson?.resourceId]);
} else {
setFreeLessons(prev => [...prev, lesson?.resourceId]);
}
});
}
}).catch(err => {
2024-11-14 16:12:25 -06:00
console.error('err', err);
});
}, []);
useEffect(() => {
2024-04-20 16:16:22 -05:00
const fetch = async () => {
try {
if (documents && documents.length > 0 && paidLessons) {
const processedDocuments = documents.map(document => parseEvent(document));
2024-10-16 20:14:40 -05:00
// Sort documents by created_at in descending order (most recent first)
const sortedDocuments = processedDocuments.sort((a, b) => b.created_at - a.created_at);
// Filter out documents that are in paid lessons and combined resources
const filteredDocuments = sortedDocuments.filter(document =>
!paidLessons.includes(document?.d) &&
!(document.topics?.includes('video') && document.topics?.includes('document'))
);
2024-10-16 20:14:40 -05:00
setProcessedDocuments(filteredDocuments);
} else {
console.log('No documents fetched or empty array returned');
2024-04-22 15:46:52 -05:00
}
2024-04-20 16:16:22 -05:00
} catch (error) {
console.error('Error fetching documents:', error);
2024-04-20 16:16:22 -05:00
}
};
2024-04-20 16:16:22 -05:00
fetch();
}, [documents, paidLessons]);
if (documentsError) {
return <div>Error: {documentsError.message}</div>
}
return (
<>
2024-09-16 16:10:28 -05:00
<h3 className={`ml-[6%] mt-4 max-mob:text-3xl max-mob:ml-10`}>Documents</h3>
<Divider className={`${isMobileView ? '' : 'hidden'}`} />
<Carousel
value={documentsLoading || !processedDocuments.length ? [{}, {}, {}] : [...processedDocuments]}
numVisible={2}
2024-09-16 16:10:28 -05:00
pt={{
previousButton: {
className: isMobileView ? 'm-0' : ''
},
nextButton: {
className: isMobileView ? 'm-0' : ''
}
}}
itemTemplate={(item) =>
processedDocuments.length > 0 ?
2024-10-17 09:23:46 -05:00
<DocumentTemplate key={item.id} document={item} isLesson={freeLessons.includes(item.d)} showMetaTags={false} /> :
<TemplateSkeleton key={Math.random()} />
}
responsiveOptions={responsiveOptions} />
</>
);
}