Order by zapAmount

This commit is contained in:
austinkelsay 2024-10-16 20:06:58 -05:00
parent e40ec068e7
commit 17924d338e
4 changed files with 49 additions and 24 deletions

View File

@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect, useCallback } from 'react';
import axios from 'axios'; import axios from 'axios';
import { Carousel } from 'primereact/carousel'; import { Carousel } from 'primereact/carousel';
import { parseEvent } from '@/utils/nostr'; import { parseEvent } from '@/utils/nostr';
@ -30,10 +30,15 @@ export default function DocumentsCarousel() {
const [processedDocuments, setProcessedDocuments] = useState([]); const [processedDocuments, setProcessedDocuments] = useState([]);
const [paidLessons, setPaidLessons] = useState([]); const [paidLessons, setPaidLessons] = useState([]);
const [freeLessons, setFreeLessons] = useState([]); const [freeLessons, setFreeLessons] = useState([]);
const [zapAmounts, setZapAmounts] = useState({});
const { documents, documentsLoading, documentsError } = useDocuments() const { documents, documentsLoading, documentsError } = useDocuments()
const windowWidth = useWindowWidth(); const windowWidth = useWindowWidth();
const isMobileView = windowWidth <= 450; const isMobileView = windowWidth <= 450;
const handleZapAmountChange = useCallback((documentId, amount) => {
setZapAmounts(prev => ({ ...prev, [documentId]: amount }));
}, []);
// todo: cache this in react query // todo: cache this in react query
useEffect(() => { useEffect(() => {
axios.get('/api/lessons').then(res => { axios.get('/api/lessons').then(res => {
@ -56,12 +61,8 @@ export default function DocumentsCarousel() {
try { try {
if (documents && documents.length > 0 && paidLessons.length > 0) { if (documents && documents.length > 0 && paidLessons.length > 0) {
const processedDocuments = documents.map(document => parseEvent(document)); const processedDocuments = documents.map(document => parseEvent(document));
// Sort documents by created_at in descending order (most recent first) // Filter out documents that are in the paid lessons array
const sortedDocuments = processedDocuments.sort((a, b) => b.created_at - a.created_at); const filteredDocuments = processedDocuments.filter(document => !paidLessons.includes(document?.d));
// filter out documents that are in the paid lessons array
const filteredDocuments = sortedDocuments.filter(document => !paidLessons.includes(document?.d));
setProcessedDocuments(filteredDocuments); setProcessedDocuments(filteredDocuments);
} else { } else {
console.log('No documents fetched or empty array returned'); console.log('No documents fetched or empty array returned');
@ -73,6 +74,15 @@ export default function DocumentsCarousel() {
fetch(); fetch();
}, [documents, paidLessons]); }, [documents, paidLessons]);
useEffect(() => {
if (Object.keys(zapAmounts).length === processedDocuments.length) {
const sortedDocuments = [...processedDocuments].sort((a, b) =>
(zapAmounts[b.id] || 0) - (zapAmounts[a.id] || 0)
);
setProcessedDocuments(sortedDocuments);
}
}, [zapAmounts, processedDocuments]);
if (documentsError) { if (documentsError) {
return <div>Error: {documentsError.message}</div> return <div>Error: {documentsError.message}</div>
} }
@ -94,7 +104,7 @@ export default function DocumentsCarousel() {
}} }}
itemTemplate={(item) => itemTemplate={(item) =>
processedDocuments.length > 0 ? processedDocuments.length > 0 ?
<DocumentTemplate key={item.id} document={item} isLesson={freeLessons.includes(item.d)} /> : <DocumentTemplate key={item.id} document={item} isLesson={freeLessons.includes(item.d)} handleZapAmountChange={handleZapAmountChange} /> :
<TemplateSkeleton key={Math.random()} /> <TemplateSkeleton key={Math.random()} />
} }
responsiveOptions={responsiveOptions} /> responsiveOptions={responsiveOptions} />

View File

@ -1,8 +1,8 @@
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect, useCallback } from 'react';
import axios from 'axios'; import axios from 'axios';
import { Carousel } from 'primereact/carousel'; import { Carousel } from 'primereact/carousel';
import { parseEvent } from '@/utils/nostr'; import { parseEvent } from '@/utils/nostr';
import {VideoTemplate} from '@/components/content/carousels/templates/VideoTemplate'; import { VideoTemplate } from '@/components/content/carousels/templates/VideoTemplate';
import TemplateSkeleton from '@/components/content/carousels/skeletons/TemplateSkeleton'; import TemplateSkeleton from '@/components/content/carousels/skeletons/TemplateSkeleton';
import { useVideos } from '@/hooks/nostr/useVideos'; import { useVideos } from '@/hooks/nostr/useVideos';
import useWindowWidth from '@/hooks/useWindowWidth'; import useWindowWidth from '@/hooks/useWindowWidth';
@ -30,10 +30,15 @@ export default function VideosCarousel() {
const [processedVideos, setProcessedVideos] = useState([]); const [processedVideos, setProcessedVideos] = useState([]);
const [paidLessons, setPaidLessons] = useState([]); const [paidLessons, setPaidLessons] = useState([]);
const [freeLessons, setFreeLessons] = useState([]); const [freeLessons, setFreeLessons] = useState([]);
const [zapAmounts, setZapAmounts] = useState({});
const { videos, videosLoading, videosError } = useVideos(); const { videos, videosLoading, videosError } = useVideos();
const windowWidth = useWindowWidth(); const windowWidth = useWindowWidth();
const isMobileView = windowWidth <= 450; const isMobileView = windowWidth <= 450;
const handleZapAmountChange = useCallback((videoId, amount) => {
setZapAmounts(prev => ({ ...prev, [videoId]: amount }));
}, []);
useEffect(() => { useEffect(() => {
axios.get('/api/lessons').then(res => { axios.get('/api/lessons').then(res => {
if (res.data) { if (res.data) {
@ -55,12 +60,8 @@ export default function VideosCarousel() {
try { try {
if (videos && videos.length > 0 && paidLessons.length > 0) { if (videos && videos.length > 0 && paidLessons.length > 0) {
const processedVideos = videos.map(video => parseEvent(video)); const processedVideos = videos.map(video => parseEvent(video));
// Filter out videos that are in the paid lessons array
const sortedVideos = processedVideos.sort((a, b) => b.created_at - a.created_at); const filteredVideos = processedVideos.filter(video => !paidLessons.includes(video?.d));
// filter out videos that are in the paid lessons array
const filteredVideos = sortedVideos.filter(video => !paidLessons.includes(video?.d));
setProcessedVideos(filteredVideos); setProcessedVideos(filteredVideos);
} else { } else {
console.log('No videos fetched or empty array returned'); console.log('No videos fetched or empty array returned');
@ -72,6 +73,15 @@ export default function VideosCarousel() {
fetch(); fetch();
}, [videos, paidLessons]); }, [videos, paidLessons]);
useEffect(() => {
if (Object.keys(zapAmounts).length === processedVideos.length) {
const sortedVideos = [...processedVideos].sort((a, b) =>
(zapAmounts[b.id] || 0) - (zapAmounts[a.id] || 0)
);
setProcessedVideos(sortedVideos);
}
}, [zapAmounts, processedVideos]);
if (videosError) return <div>Error: {videosError}</div>; if (videosError) return <div>Error: {videosError}</div>;
return ( return (
@ -92,7 +102,12 @@ export default function VideosCarousel() {
itemTemplate={(item) => itemTemplate={(item) =>
!processedVideos.length ? !processedVideos.length ?
<TemplateSkeleton key={Math.random()} /> : <TemplateSkeleton key={Math.random()} /> :
<VideoTemplate key={item.id} video={item} isLesson={freeLessons.includes(item.d)} /> <VideoTemplate
key={item.id}
video={item}
isLesson={freeLessons.includes(item.d)}
onZapAmountChange={handleZapAmountChange}
/>
} }
responsiveOptions={responsiveOptions} responsiveOptions={responsiveOptions}
/> />

View File

@ -14,9 +14,8 @@ import useWindowWidth from "@/hooks/useWindowWidth";
import GenericButton from "@/components/buttons/GenericButton"; import GenericButton from "@/components/buttons/GenericButton";
import appConfig from "@/config/appConfig"; import appConfig from "@/config/appConfig";
export function DocumentTemplate({ document, isLesson }) { export function DocumentTemplate({ document, isLesson, onZapAmountChange }) {
const { zaps, zapsLoading, zapsError } = useZapsSubscription({ event: document }); const { zaps, zapsLoading, zapsError } = useZapsSubscription({ event: document });
const [nAddress, setNAddress] = useState(null);
const [zapAmount, setZapAmount] = useState(0); const [zapAmount, setZapAmount] = useState(0);
const router = useRouter(); const router = useRouter();
const { returnImageProxy } = useImageProxy(); const { returnImageProxy } = useImageProxy();
@ -39,8 +38,9 @@ export function DocumentTemplate({ document, isLesson }) {
if (zaps.length > 0) { if (zaps.length > 0) {
const total = getTotalFromZaps(zaps, document); const total = getTotalFromZaps(zaps, document);
setZapAmount(total); setZapAmount(total);
onZapAmountChange(document.id, total);
} }
}, [zaps, document]); }, [zaps, document, onZapAmountChange]);
if (zapsError) return <div>Error: {zapsError}</div>; if (zapsError) return <div>Error: {zapsError}</div>;

View File

@ -15,9 +15,8 @@ import useWindowWidth from "@/hooks/useWindowWidth";
import GenericButton from "@/components/buttons/GenericButton"; import GenericButton from "@/components/buttons/GenericButton";
import appConfig from "@/config/appConfig"; import appConfig from "@/config/appConfig";
export function VideoTemplate({ video, isLesson }) { export function VideoTemplate({ video, isLesson, onZapAmountChange }) {
const { zaps, zapsLoading, zapsError } = useZapsSubscription({ event: video }); const { zaps, zapsLoading, zapsError } = useZapsSubscription({ event: video });
const [nAddress, setNAddress] = useState(null);
const [zapAmount, setZapAmount] = useState(0); const [zapAmount, setZapAmount] = useState(0);
const router = useRouter(); const router = useRouter();
const { returnImageProxy } = useImageProxy(); const { returnImageProxy } = useImageProxy();
@ -40,8 +39,9 @@ export function VideoTemplate({ video, isLesson }) {
if (zaps.length > 0) { if (zaps.length > 0) {
const total = getTotalFromZaps(zaps, video); const total = getTotalFromZaps(zaps, video);
setZapAmount(total); setZapAmount(total);
onZapAmountChange(video.id, total);
} }
}, [zaps, video]); }, [zaps, video, onZapAmountChange]);
if (zapsError) return <div>Error: {zapsError}</div>; if (zapsError) return <div>Error: {zapsError}</div>;