Filter out paid lessons from video and document carousels

This commit is contained in:
austinkelsay 2024-10-13 17:33:30 -05:00
parent cae0396092
commit 9b0def8123
2 changed files with 42 additions and 3 deletions

View File

@ -1,10 +1,12 @@
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { Carousel } from 'primereact/carousel';
import { parseEvent } from '@/utils/nostr';
import { DocumentTemplate } from '@/components/content/carousels/templates/DocumentTemplate';
import TemplateSkeleton from '@/components/content/carousels/skeletons/TemplateSkeleton';
import { useDocuments } from '@/hooks/nostr/useDocuments';
import useWindowWidth from '@/hooks/useWindowWidth';
import { nip19 } from 'nostr-tools';
import { Divider } from 'primereact/divider';
const responsiveOptions = [
{
@ -26,10 +28,26 @@ const responsiveOptions = [
export default function DocumentsCarousel() {
const [processedDocuments, setProcessedDocuments] = useState([]);
const [paidLessons, setPaidLessons] = useState([]);
const { documents, documentsLoading, documentsError } = useDocuments()
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) {
setPaidLessons(prev => [...prev, lesson]);
}
});
}
}).catch(err => {
console.log('err', err);
});
}, []);
useEffect(() => {
const fetch = async () => {
try {
@ -39,7 +57,10 @@ export default function DocumentsCarousel() {
// Sort documents by created_at in descending order (most recent first)
const sortedDocuments = processedDocuments.sort((a, b) => b.created_at - a.created_at);
setProcessedDocuments(sortedDocuments);
// filter out documents that are in the paid lessons array
const filteredDocuments = sortedDocuments.filter(document => !paidLessons.includes(document.id));
setProcessedDocuments(filteredDocuments);
} else {
console.log('No documents fetched or empty array returned');
}

View File

@ -1,4 +1,5 @@
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { Carousel } from 'primereact/carousel';
import { parseEvent } from '@/utils/nostr';
import {VideoTemplate} from '@/components/content/carousels/templates/VideoTemplate';
@ -27,10 +28,25 @@ const responsiveOptions = [
export default function VideosCarousel() {
const [processedVideos, setProcessedVideos] = useState([]);
const [paidLessons, setPaidLessons] = useState([]);
const { videos, videosLoading, videosError } = useVideos();
const windowWidth = useWindowWidth();
const isMobileView = windowWidth <= 450;
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);
});
}, []);
useEffect(() => {
const fetch = async () => {
try {
@ -39,8 +55,10 @@ export default function VideosCarousel() {
const sortedVideos = processedVideos.sort((a, b) => b.created_at - a.created_at);
console.log('Sorted videos:', sortedVideos);
setProcessedVideos(sortedVideos);
// filter out videos that are in the paid lessons array
const filteredVideos = sortedVideos.filter(video => !paidLessons.includes(video.id));
setProcessedVideos(filteredVideos);
} else {
console.log('No videos fetched or empty array returned');
}