mirror of
https://github.com/AustinKelsay/plebdevs.git
synced 2025-06-06 18:31:00 +00:00
Progress on course edit flow, need to reconcile courseId with draftcourses relation
This commit is contained in:
parent
9046bce91f
commit
14ead7b35c
@ -45,6 +45,10 @@ const CourseForm = ({ draft = null, isPublished = false }) => {
|
|||||||
}
|
}
|
||||||
}, [session]);
|
}, [session]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
console.log('selectedLessons:', selectedLessons);
|
||||||
|
}, [selectedLessons]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchLessons = async () => {
|
const fetchLessons = async () => {
|
||||||
if (draft && draft?.resources) {
|
if (draft && draft?.resources) {
|
||||||
@ -54,7 +58,7 @@ const CourseForm = ({ draft = null, isPublished = false }) => {
|
|||||||
return parsedLesson;
|
return parsedLesson;
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
setSelectedLessons(parsedLessons);
|
setSelectedLessons([...selectedLessons, ...parsedLessons]);
|
||||||
setLoadingLessons(false); // Data is loaded
|
setLoadingLessons(false); // Data is loaded
|
||||||
} else {
|
} else {
|
||||||
setLoadingLessons(false); // No draft means no lessons to load
|
setLoadingLessons(false); // No draft means no lessons to load
|
||||||
@ -62,17 +66,14 @@ const CourseForm = ({ draft = null, isPublished = false }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fetchLessons();
|
fetchLessons();
|
||||||
}, [draft]);
|
}, [draft]); // Only depend on draft
|
||||||
|
|
||||||
const fetchLessonEventFromNostr = async (eventId) => {
|
const fetchLessonEventFromNostr = async (eventId) => {
|
||||||
try {
|
try {
|
||||||
await ndk.connect();
|
await ndk.connect();
|
||||||
|
|
||||||
const fetchedEvent = await ndk.fetchEvent(eventId);
|
const fetchedEvent = await ndk.fetchEvent(eventId);
|
||||||
|
|
||||||
if (fetchedEvent) {
|
if (fetchedEvent) {
|
||||||
const parsedEvent = parseEvent(fetchedEvent);
|
return parseEvent(fetchedEvent);
|
||||||
return parsedEvent;
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
showToast('error', 'Error', `Failed to fetch lesson: ${eventId}`);
|
showToast('error', 'Error', `Failed to fetch lesson: ${eventId}`);
|
||||||
@ -142,6 +143,22 @@ const CourseForm = ({ draft = null, isPublished = false }) => {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
// if this is a draft any added resources should be updated with courseId
|
||||||
|
if (draft) {
|
||||||
|
resources.forEach(resource => {
|
||||||
|
console.log('each resource:', resource);
|
||||||
|
if (!draft.resources.includes(resource.id)) {
|
||||||
|
axios.put(`/api/resources/${resource.id}`, { courseId: draft.id })
|
||||||
|
.then(response => {
|
||||||
|
console.log('resource updated:', response);
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error('error updating resource:', error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const payload = {
|
const payload = {
|
||||||
user: {
|
user: {
|
||||||
connect: { id: user.id },
|
connect: { id: user.id },
|
||||||
@ -167,7 +184,8 @@ const CourseForm = ({ draft = null, isPublished = false }) => {
|
|||||||
response = await axios.post('/api/courses/drafts', payload);
|
response = await axios.post('/api/courses/drafts', payload);
|
||||||
showToast('success', 'Success', 'Course draft saved successfully');
|
showToast('success', 'Success', 'Course draft saved successfully');
|
||||||
}
|
}
|
||||||
router.push(`/course/${response.data.id}/draft`);
|
console.log('response:', response);
|
||||||
|
// router.push(`/course/${response.data.id}/draft`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error saving course draft:', error);
|
console.error('Error saving course draft:', error);
|
||||||
showToast('error', 'Failed to save course draft. Please try again.');
|
showToast('error', 'Failed to save course draft. Please try again.');
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { getResourceById, updateResource, deleteResource, isResourcePartOfAnyCourse } from "@/db/models/resourceModels";
|
import { getResourceById, updateResource, deleteResource, isResourcePartOfAnyCourse, updateLessonInCourse } from "@/db/models/resourceModels";
|
||||||
|
|
||||||
export default async function handler(req, res) {
|
export default async function handler(req, res) {
|
||||||
const { slug } = req.query;
|
const { slug } = req.query;
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
import React, { useState, useEffect } from "react";
|
import React, { useState, useEffect, use } from "react";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
import { parseEvent } from "@/utils/nostr";
|
|
||||||
import ResourceForm from "@/components/forms/ResourceForm";
|
|
||||||
import WorkshopForm from "@/components/forms/WorkshopForm";
|
|
||||||
import CourseForm from "@/components/forms/CourseForm";
|
import CourseForm from "@/components/forms/CourseForm";
|
||||||
import { useNDKContext } from "@/context/NDKContext";
|
import { useNDKContext } from "@/context/NDKContext";
|
||||||
import { useToast } from "@/hooks/useToast";
|
import { useToast } from "@/hooks/useToast";
|
||||||
@ -15,6 +12,8 @@ export default function Edit() {
|
|||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { showToast } = useToast();
|
const { showToast } = useToast();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (router.isReady) {
|
if (router.isReady) {
|
||||||
const { slug } = router.query;
|
const { slug } = router.query;
|
||||||
|
@ -42,6 +42,7 @@ const DraftCourse = () => {
|
|||||||
.then(res => {
|
.then(res => {
|
||||||
console.log('res:', res.data);
|
console.log('res:', res.data);
|
||||||
setCourse(res.data);
|
setCourse(res.data);
|
||||||
|
console.log('coursesssss:', res.data);
|
||||||
setLessons(res.data.resources); // Set the raw lessons
|
setLessons(res.data.resources); // Set the raw lessons
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user