2024-03-27 14:44:54 -05:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2024-10-13 17:33:30 -05:00
|
|
|
import axios from 'axios';
|
2024-03-27 14:44:54 -05:00
|
|
|
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';
|
2024-09-15 15:15:58 -05:00
|
|
|
import { useDocuments } from '@/hooks/nostr/useDocuments';
|
2024-09-16 16:10:28 -05:00
|
|
|
import useWindowWidth from '@/hooks/useWindowWidth';
|
2024-10-13 17:33:30 -05:00
|
|
|
import { nip19 } from 'nostr-tools';
|
2024-09-16 16:10:28 -05:00
|
|
|
import { Divider } from 'primereact/divider';
|
2024-03-27 14:44:54 -05:00
|
|
|
const responsiveOptions = [
|
|
|
|
{
|
|
|
|
breakpoint: '3000px',
|
|
|
|
numVisible: 3,
|
|
|
|
numScroll: 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
breakpoint: '1462px',
|
|
|
|
numVisible: 2,
|
|
|
|
numScroll: 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
breakpoint: '575px',
|
|
|
|
numVisible: 1,
|
|
|
|
numScroll: 1
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2024-09-15 15:15:58 -05:00
|
|
|
export default function DocumentsCarousel() {
|
|
|
|
const [processedDocuments, setProcessedDocuments] = useState([]);
|
2024-10-13 17:33:30 -05:00
|
|
|
const [paidLessons, setPaidLessons] = useState([]);
|
2024-09-15 15:15:58 -05:00
|
|
|
const { documents, documentsLoading, documentsError } = useDocuments()
|
2024-09-16 16:10:28 -05:00
|
|
|
const windowWidth = useWindowWidth();
|
|
|
|
const isMobileView = windowWidth <= 450;
|
2024-03-27 14:44:54 -05:00
|
|
|
|
2024-10-13 17:33:30 -05:00
|
|
|
// 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) {
|
|
|
|
setPaidLessons(prev => [...prev, lesson]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}).catch(err => {
|
|
|
|
console.log('err', err);
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
2024-03-27 14:44:54 -05:00
|
|
|
useEffect(() => {
|
2024-04-20 16:16:22 -05:00
|
|
|
const fetch = async () => {
|
|
|
|
try {
|
2024-09-15 15:15:58 -05:00
|
|
|
if (documents && documents.length > 0) {
|
|
|
|
const processedDocuments = documents.map(document => parseEvent(document));
|
2024-09-14 18:36:46 -05:00
|
|
|
|
2024-09-15 15:15:58 -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);
|
2024-08-03 13:42:46 -05:00
|
|
|
|
2024-10-13 17:33:30 -05:00
|
|
|
// filter out documents that are in the paid lessons array
|
2024-10-13 17:42:28 -05:00
|
|
|
const filteredDocuments = sortedDocuments.filter(document => !paidLessons.includes(document?.d));
|
2024-10-13 17:33:30 -05:00
|
|
|
|
|
|
|
setProcessedDocuments(filteredDocuments);
|
2024-09-14 18:36:46 -05:00
|
|
|
} else {
|
2024-09-15 15:15:58 -05:00
|
|
|
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) {
|
2024-09-15 15:15:58 -05:00
|
|
|
console.error('Error fetching documents:', error);
|
2024-04-20 16:16:22 -05:00
|
|
|
}
|
2024-04-23 18:52:55 -05:00
|
|
|
};
|
2024-04-20 16:16:22 -05:00
|
|
|
fetch();
|
2024-09-15 15:15:58 -05:00
|
|
|
}, [documents]);
|
2024-08-03 13:42:46 -05:00
|
|
|
|
2024-09-15 15:15:58 -05:00
|
|
|
if (documentsError) {
|
|
|
|
return <div>Error: {documentsError.message}</div>
|
2024-08-03 13:42:46 -05:00
|
|
|
}
|
2024-03-27 14:44:54 -05:00
|
|
|
|
|
|
|
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'}`} />
|
2024-08-05 15:25:04 -05:00
|
|
|
<Carousel
|
2024-09-15 15:15:58 -05:00
|
|
|
value={documentsLoading || !processedDocuments.length ? [{}, {}, {}] : [...processedDocuments]}
|
2024-08-05 15:25:04 -05:00
|
|
|
numVisible={2}
|
2024-09-16 16:10:28 -05:00
|
|
|
pt={{
|
|
|
|
previousButton: {
|
|
|
|
className: isMobileView ? 'm-0' : ''
|
|
|
|
},
|
|
|
|
nextButton: {
|
|
|
|
className: isMobileView ? 'm-0' : ''
|
|
|
|
}
|
|
|
|
}}
|
2024-08-05 15:25:04 -05:00
|
|
|
itemTemplate={(item) =>
|
2024-09-15 15:15:58 -05:00
|
|
|
processedDocuments.length > 0 ?
|
2024-09-12 12:07:38 -05:00
|
|
|
<DocumentTemplate key={item.id} document={item} /> :
|
2024-08-01 17:10:43 -05:00
|
|
|
<TemplateSkeleton key={Math.random()} />
|
2024-08-05 15:25:04 -05:00
|
|
|
}
|
|
|
|
responsiveOptions={responsiveOptions} />
|
2024-03-27 14:44:54 -05:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|