mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +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}>
|
<Draggable key={props.info.list.chapter.id} draggableId={props.info.list.chapter.id} index={props.index}>
|
||||||
{(provided, snapshot) => (
|
{(provided, snapshot) => (
|
||||||
<ChapterWrapper {...provided.dragHandleProps} {...provided.draggableProps} ref={provided.innerRef} isDragging={snapshot.isDragging} key={props.info.list.chapter.id}>
|
<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">
|
<Droppable droppableId={props.info.list.chapter.id} type="element">
|
||||||
{(provided) => (
|
{(provided) => (
|
||||||
<ElementsList {...provided.droppableProps} ref={provided.innerRef}>
|
<ElementsList {...provided.droppableProps} ref={provided.innerRef}>
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,9 @@ export const initialData = {
|
||||||
"element-5": { id: "element-5", content: "Fifth element" },
|
"element-5": { id: "element-5", content: "Fifth element" },
|
||||||
},
|
},
|
||||||
chapters: {
|
chapters: {
|
||||||
"chapter-1": { id: "chapter-1", title: "Chapter 1", elementIds: ["element-1", "element-2", "element-3"] },
|
"chapter-1": { id: "chapter-1", name: "Chapter 1", elementIds: ["element-1", "element-2", "element-3"] },
|
||||||
"chapter-2": { id: "chapter-2", title: "Chapter 2", elementIds: ["element-4"] },
|
"chapter-2": { id: "chapter-2", name: "Chapter 2", elementIds: ["element-4"] },
|
||||||
"chapter-3": { id: "chapter-3", title: "Chapter 3", elementIds: ["element-5"] },
|
"chapter-3": { id: "chapter-3", name: "Chapter 3", elementIds: ["element-5"] },
|
||||||
},
|
},
|
||||||
|
|
||||||
chapterOrder: ["chapter-1", "chapter-2", "chapter-3"],
|
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 { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd";
|
||||||
import { initialData } from "../../../../../../components/drags/data";
|
import { initialData } from "../../../../../../components/drags/data";
|
||||||
import Chapter from "../../../../../../components/drags/chapter";
|
import Chapter from "../../../../../../components/drags/chapter";
|
||||||
|
import { getCourseChaptersMetadata } from "../../../../../../services/chapters";
|
||||||
|
import { useRouter } from "next/router";
|
||||||
|
|
||||||
function CourseEdit() {
|
function CourseEdit() {
|
||||||
|
const router = useRouter();
|
||||||
const [data, setData] = useState(initialData) as any;
|
const [data, setData] = useState(initialData) as any;
|
||||||
const [winReady, setwinReady] = useState(false);
|
const [winReady, setwinReady] = useState(false);
|
||||||
|
const { courseid } = router.query;
|
||||||
|
|
||||||
|
async function getCourseChapters() {
|
||||||
|
const courseChapters = await getCourseChaptersMetadata(courseid);
|
||||||
|
setData(courseChapters);
|
||||||
|
console.log(courseChapters);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
setwinReady(true);
|
setwinReady(true);
|
||||||
}, []);
|
}, [router.isReady]);
|
||||||
|
|
||||||
// get a list of chapters order by chapter order
|
// get a list of chapters order by chapter order
|
||||||
const getChapters = () => {
|
const getChapters = () => {
|
||||||
|
|
@ -68,7 +82,7 @@ function CourseEdit() {
|
||||||
if (start === finish) {
|
if (start === finish) {
|
||||||
// create new arrays for chapters and elements
|
// create new arrays for chapters and elements
|
||||||
const chapter = data.chapters[source.droppableId];
|
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
|
// remove the element from the old position
|
||||||
newElementIds.splice(source.index, 1);
|
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;
|
||||||
|
}
|
||||||
|
|
@ -30,7 +30,6 @@ class CourseChapterInDB(CourseChapter):
|
||||||
updateDate: str
|
updateDate: str
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Frontend
|
# Frontend
|
||||||
class CourseChapterMetaData(BaseModel):
|
class CourseChapterMetaData(BaseModel):
|
||||||
chapterOrder: List[str]
|
chapterOrder: List[str]
|
||||||
|
|
@ -97,10 +96,18 @@ async def get_coursechapters_meta(course_id: str, current_user: PublicUser):
|
||||||
course = courses.find_one({"course_id": course_id})
|
course = courses.find_one({"course_id": course_id})
|
||||||
course = Course(**course)
|
course = Course(**course)
|
||||||
|
|
||||||
|
|
||||||
# chapters
|
# chapters
|
||||||
chapters = [json.loads(json.dumps(chapter, default=str))
|
chapters = {}
|
||||||
for chapter in coursechapters]
|
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 = {
|
final = {
|
||||||
"chapters": chapters,
|
"chapters": chapters,
|
||||||
|
|
@ -124,7 +131,6 @@ async def update_coursechapters_meta(course_id: str, coursechapters_metadata: Co
|
||||||
|
|
||||||
# TODO : update chapters in coursechapters
|
# TODO : update chapters in coursechapters
|
||||||
|
|
||||||
|
|
||||||
return {courseInDB}
|
return {courseInDB}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue