From 2cd51175821ff72913a4a3ea1f019730ff699e0f Mon Sep 17 00:00:00 2001 From: austinkelsay Date: Mon, 29 Jul 2024 17:26:49 -0500 Subject: [PATCH] Finally got full course publishing flow to work, i think... --- .../content/carousels/CoursesCarousel.js | 2 -- src/components/forms/CourseForm.js | 24 +++++++++++-------- src/utils/nostr.js | 1 - 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/components/content/carousels/CoursesCarousel.js b/src/components/content/carousels/CoursesCarousel.js index f230fb5..b5fd68f 100644 --- a/src/components/content/carousels/CoursesCarousel.js +++ b/src/components/content/carousels/CoursesCarousel.js @@ -31,11 +31,9 @@ export default function CoursesCarousel() { const fetch = async () => { try { const fetchedCourses = await fetchCourses(); - console.log('fetchedCourses:', fetchedCourses); if (fetchedCourses && fetchedCourses.length > 0) { // First process the courses to be ready for display const processedCourses = fetchedCourses.map(course => parseCourseEvent(course)); - console.log('processedCourses:', processedCourses); // Fetch zaps for all processed courses at once const allZaps = await fetchZapsForEvents(processedCourses); diff --git a/src/components/forms/CourseForm.js b/src/components/forms/CourseForm.js index 48209d4..4f4ce64 100644 --- a/src/components/forms/CourseForm.js +++ b/src/components/forms/CourseForm.js @@ -84,7 +84,6 @@ const CourseForm = () => { try { // Step 1: Process lessons - console.log('selectedLessons:', selectedLessons); for (const lesson of selectedLessons) { let noteId = lesson.noteId; @@ -110,17 +109,19 @@ const CourseForm = () => { }), axios.delete(`/api/drafts/${lesson.id}`) ]); - - } - - processedLessons.push({ id: lesson?.d || lesson.id }); + // if the lesson was already published we will have d tag, otherwise we will have id + // if the lesson was already published we will have kind tag, otherwise we will use price tag to determine the kind + // if the lesson was already published we will have pubkey tag, otherwise we will use user.pubkey + processedLessons.push({ + d: lesson?.d || lesson.id, + kind: lesson.kind ?? (lesson.price ? 30402 : 30023), + pubkey: lesson.pubkey || user.pubkey + }); } - // Need to set aside a final processed lessons that included lesson.kind, lesson.pubkey, lesson.d - // Step 2: Create and publish course - const courseEvent = createCourseEvent(newCourseId, title, summary, coverImage, selectedLessons); + const courseEvent = createCourseEvent(newCourseId, title, summary, coverImage, processedLessons); const signedCourseEvent = await window.nostr.signEvent(courseEvent); const published = await publish(signedCourseEvent); @@ -133,7 +134,7 @@ const CourseForm = () => { await axios.post('/api/courses', { id: newCourseId, resources: { - connect: processedLessons.map(lesson => ({ id: lesson?.id })) + connect: processedLessons.map(lesson => ({ id: lesson?.d })) }, noteId: signedCourseEvent.id, user: { @@ -142,7 +143,10 @@ const CourseForm = () => { price: price || 0 }); - // Step 4: Show success message and redirect + // step 4: Update all resources to have the course id + await Promise.all(processedLessons.map(lesson => axios.put(`/api/resources/${lesson?.d}`, { courseId: newCourseId }))); + + // Step 5: Show success message and redirect showToast('success', 'Course created successfully'); router.push(`/course/${signedCourseEvent.id}`); diff --git a/src/utils/nostr.js b/src/utils/nostr.js index 2bd9bd1..debea0f 100644 --- a/src/utils/nostr.js +++ b/src/utils/nostr.js @@ -81,7 +81,6 @@ export const parseEvent = (event) => { }; export const parseCourseEvent = (event) => { - console.log('event:', event); // Initialize an object to store the extracted data const eventData = { id: event.id,