From c2b3e51ad066182e0332dc4e77c59619fefeef96 Mon Sep 17 00:00:00 2001 From: swve Date: Mon, 31 Oct 2022 18:08:38 +0100 Subject: [PATCH] wip: get chapters --- front/components/drags/chapter.tsx | 2 +- front/components/drags/data.ts | 6 +++--- .../course/[courseid]/edit/index.tsx | 18 ++++++++++++++-- front/services/chapters.ts | 21 +++++++++++++++++++ src/services/chapters.py | 18 ++++++++++------ 5 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 front/services/chapters.ts diff --git a/front/components/drags/chapter.tsx b/front/components/drags/chapter.tsx index 07eba77a..56de2aac 100644 --- a/front/components/drags/chapter.tsx +++ b/front/components/drags/chapter.tsx @@ -22,7 +22,7 @@ function Chapter(props: any) { {(provided, snapshot) => ( -

{props.info.list.chapter.title}

+

{props.info.list.chapter.name}

{(provided) => ( diff --git a/front/components/drags/data.ts b/front/components/drags/data.ts index cdf98dd5..d3444117 100644 --- a/front/components/drags/data.ts +++ b/front/components/drags/data.ts @@ -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"], diff --git a/front/pages/org/[orgslug]/course/[courseid]/edit/index.tsx b/front/pages/org/[orgslug]/course/[courseid]/edit/index.tsx index a38389d1..c705b041 100644 --- a/front/pages/org/[orgslug]/course/[courseid]/edit/index.tsx +++ b/front/pages/org/[orgslug]/course/[courseid]/edit/index.tsx @@ -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); diff --git a/front/services/chapters.ts b/front/services/chapters.ts new file mode 100644 index 00000000..a78ef6eb --- /dev/null +++ b/front/services/chapters.ts @@ -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; +} diff --git a/src/services/chapters.py b/src/services/chapters.py index b8ed2237..f0006a44 100644 --- a/src/services/chapters.py +++ b/src/services/chapters.py @@ -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}