mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-18 11:59:26 +00:00
wip: get chapters
This commit is contained in:
parent
40496f7ced
commit
c2b3e51ad0
5 changed files with 53 additions and 12 deletions
|
|
@ -22,7 +22,7 @@ function Chapter(props: any) {
|
|||
<Draggable key={props.info.list.chapter.id} draggableId={props.info.list.chapter.id} index={props.index}>
|
||||
{(provided, snapshot) => (
|
||||
<ChapterWrapper {...provided.dragHandleProps} {...provided.draggableProps} ref={provided.innerRef} isDragging={snapshot.isDragging} key={props.info.list.chapter.id}>
|
||||
<h3>{props.info.list.chapter.title}</h3>
|
||||
<h3>{props.info.list.chapter.name}</h3>
|
||||
<Droppable droppableId={props.info.list.chapter.id} type="element">
|
||||
{(provided) => (
|
||||
<ElementsList {...provided.droppableProps} ref={provided.innerRef}>
|
||||
|
|
|
|||
|
|
@ -7,9 +7,9 @@ export const initialData = {
|
|||
"element-5": { id: "element-5", content: "Fifth element" },
|
||||
},
|
||||
chapters: {
|
||||
"chapter-1": { id: "chapter-1", title: "Chapter 1", elementIds: ["element-1", "element-2", "element-3"] },
|
||||
"chapter-2": { id: "chapter-2", title: "Chapter 2", elementIds: ["element-4"] },
|
||||
"chapter-3": { id: "chapter-3", title: "Chapter 3", elementIds: ["element-5"] },
|
||||
"chapter-1": { id: "chapter-1", name: "Chapter 1", elementIds: ["element-1", "element-2", "element-3"] },
|
||||
"chapter-2": { id: "chapter-2", name: "Chapter 2", elementIds: ["element-4"] },
|
||||
"chapter-3": { id: "chapter-3", name: "Chapter 3", elementIds: ["element-5"] },
|
||||
},
|
||||
|
||||
chapterOrder: ["chapter-1", "chapter-2", "chapter-3"],
|
||||
|
|
|
|||
|
|
@ -7,14 +7,28 @@ import { Title } from "../../../../../../components/ui/styles/title";
|
|||
import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd";
|
||||
import { initialData } from "../../../../../../components/drags/data";
|
||||
import Chapter from "../../../../../../components/drags/chapter";
|
||||
import { getCourseChaptersMetadata } from "../../../../../../services/chapters";
|
||||
import { useRouter } from "next/router";
|
||||
|
||||
function CourseEdit() {
|
||||
const router = useRouter();
|
||||
const [data, setData] = useState(initialData) as any;
|
||||
const [winReady, setwinReady] = useState(false);
|
||||
const { courseid } = router.query;
|
||||
|
||||
async function getCourseChapters() {
|
||||
const courseChapters = await getCourseChaptersMetadata(courseid);
|
||||
setData(courseChapters);
|
||||
console.log(courseChapters);
|
||||
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
|
||||
|
||||
setwinReady(true);
|
||||
}, []);
|
||||
}, [router.isReady]);
|
||||
|
||||
// get a list of chapters order by chapter order
|
||||
const getChapters = () => {
|
||||
|
|
@ -68,7 +82,7 @@ function CourseEdit() {
|
|||
if (start === finish) {
|
||||
// create new arrays for chapters and elements
|
||||
const chapter = data.chapters[source.droppableId];
|
||||
const newElementIds = Array.from(chapter.elementIds);
|
||||
const newElementIds = Array.from(chapter.elements.element_id);
|
||||
|
||||
// remove the element from the old position
|
||||
newElementIds.splice(source.index, 1);
|
||||
|
|
|
|||
21
front/services/chapters.ts
Normal file
21
front/services/chapters.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import { initialData } from "../components/drags/data";
|
||||
import { getAPIUrl } from "./config";
|
||||
|
||||
export async function getCourseChaptersMetadata(course_id: any) {
|
||||
const HeadersConfig = new Headers({ "Content-Type": "application/json" });
|
||||
|
||||
const requestOptions: any = {
|
||||
method: "GET",
|
||||
headers: HeadersConfig,
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
};
|
||||
|
||||
const data: any = await fetch(`${getAPIUrl()}chapters/meta/course_${course_id}`, requestOptions)
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
|
||||
console.log("data", data);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
|
@ -28,7 +28,6 @@ class CourseChapterInDB(CourseChapter):
|
|||
course_id: str
|
||||
creationDate: str
|
||||
updateDate: str
|
||||
|
||||
|
||||
|
||||
# Frontend
|
||||
|
|
@ -97,10 +96,18 @@ async def get_coursechapters_meta(course_id: str, current_user: PublicUser):
|
|||
course = courses.find_one({"course_id": course_id})
|
||||
course = Course(**course)
|
||||
|
||||
|
||||
# chapters
|
||||
chapters = [json.loads(json.dumps(chapter, default=str))
|
||||
for chapter in coursechapters]
|
||||
chapters = {}
|
||||
for coursechapter in coursechapters:
|
||||
coursechapter = CourseChapterInDB(**coursechapter)
|
||||
coursechapter_elementIds = []
|
||||
|
||||
for element in coursechapter.elements:
|
||||
coursechapter_elementIds.append(element.element_id)
|
||||
|
||||
chapters[coursechapter.coursechapter_id] = {
|
||||
"id": coursechapter.coursechapter_id, "name": coursechapter.name, "elementIds": coursechapter_elementIds
|
||||
}
|
||||
|
||||
final = {
|
||||
"chapters": chapters,
|
||||
|
|
@ -121,9 +128,8 @@ async def update_coursechapters_meta(course_id: str, coursechapters_metadata: Co
|
|||
# update chapters in course
|
||||
courseInDB = courses.update_one({"course_id": course_id}, {
|
||||
"$set": {"chapters": coursechapters_metadata.chapterOrder}})
|
||||
|
||||
|
||||
# TODO : update chapters in coursechapters
|
||||
|
||||
|
||||
return {courseInDB}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue