mirror of
https://github.com/AustinKelsay/plebdevs.git
synced 2025-06-05 00:32:03 +00:00
fix lesson parsing on course page, also fetches all lessons in one query now
This commit is contained in:
parent
cc86708b6a
commit
fac14d5b77
@ -20,10 +20,6 @@ export default function DesktopCourseDetails({
|
|||||||
showCompletedTag
|
showCompletedTag
|
||||||
}) {
|
}) {
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
console.log('menuItems', menuItems);
|
|
||||||
}, [menuItems]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{/* Header with course image, title and options */}
|
{/* Header with course image, title and options */}
|
||||||
|
@ -90,34 +90,41 @@ const useLessons = (ndk, fetchAuthor, lessonIds, pubkey) => {
|
|||||||
const [lessons, setLessons] = useState([]);
|
const [lessons, setLessons] = useState([]);
|
||||||
const [uniqueLessons, setUniqueLessons] = useState([]);
|
const [uniqueLessons, setUniqueLessons] = useState([]);
|
||||||
const { showToast } = useToast();
|
const { showToast } = useToast();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (lessonIds.length > 0) {
|
if (lessonIds.length > 0 && pubkey) {
|
||||||
const fetchLesson = async lessonId => {
|
const fetchLessons = async () => {
|
||||||
try {
|
try {
|
||||||
await ndk.connect();
|
await ndk.connect();
|
||||||
|
|
||||||
|
// Create a single filter with all lesson IDs to avoid multiple calls
|
||||||
const filter = {
|
const filter = {
|
||||||
'#d': [lessonId],
|
'#d': lessonIds,
|
||||||
kinds: [30023, 30402],
|
kinds: [30023, 30402],
|
||||||
authors: [pubkey],
|
authors: [pubkey],
|
||||||
};
|
};
|
||||||
const event = await ndk.fetchEvent(filter);
|
|
||||||
if (event) {
|
const events = await ndk.fetchEvents(filter);
|
||||||
|
const newLessons = [];
|
||||||
|
|
||||||
|
// Process events without duplicating
|
||||||
|
for (const event of events) {
|
||||||
const author = await fetchAuthor(event.pubkey);
|
const author = await fetchAuthor(event.pubkey);
|
||||||
const parsedLesson = { ...parseEvent(event), author };
|
const parsedLesson = { ...parseEvent(event), author };
|
||||||
setLessons(prev => {
|
|
||||||
// Check if the lesson already exists in the array
|
// Only add if not already in newLessons array
|
||||||
const exists = prev.some(lesson => lesson.id === parsedLesson.id);
|
if (!newLessons.some(lesson => lesson.id === parsedLesson.id)) {
|
||||||
if (!exists) {
|
newLessons.push(parsedLesson);
|
||||||
return [...prev, parsedLesson];
|
}
|
||||||
}
|
|
||||||
return prev;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setLessons(newLessons);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching event:', error);
|
console.error('Error fetching events:', error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
lessonIds.forEach(lessonId => fetchLesson(lessonId));
|
|
||||||
|
fetchLessons();
|
||||||
}
|
}
|
||||||
}, [lessonIds, ndk, fetchAuthor, pubkey]);
|
}, [lessonIds, ndk, fetchAuthor, pubkey]);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user