feat: init CS + SS course chapter deletion

This commit is contained in:
swve 2022-11-04 23:46:55 +01:00
parent 5331e4db90
commit 0572368a32
4 changed files with 37 additions and 6 deletions

View file

@ -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.name}</h3> <h3>{props.info.list.chapter.name} <button onClick={() => {props.deleteChapter(props.info.list.chapter.id)}}>X</button></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}>

View file

@ -7,7 +7,7 @@ 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, initialData2 } from "../../../../../../components/drags/data"; import { initialData, initialData2 } from "../../../../../../components/drags/data";
import Chapter from "../../../../../../components/drags/chapter"; import Chapter from "../../../../../../components/drags/chapter";
import { createChapter, getCourseChaptersMetadata } from "../../../../../../services/chapters"; import { createChapter, deleteChapter, getCourseChaptersMetadata } from "../../../../../../services/chapters";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import NewChapterModal from "../../../../../../components/modals/chapters/new"; import NewChapterModal from "../../../../../../components/modals/chapters/new";
@ -59,6 +59,15 @@ function CourseEdit() {
setNewChapterModal(false); setNewChapterModal(false);
}; };
const deleteChapterUI = async (chapterId: any) => {
console.log("deleteChapter", chapterId);
await deleteChapter(chapterId);
getCourseChapters();
};
// Close new chapter modal // Close new chapter modal
const closeModal = () => { const closeModal = () => {
setNewChapterModal(false); setNewChapterModal(false);
@ -177,7 +186,7 @@ function CourseEdit() {
{(provided) => ( {(provided) => (
<div key={"chapters"} {...provided.droppableProps} ref={provided.innerRef}> <div key={"chapters"} {...provided.droppableProps} ref={provided.innerRef}>
{getChapters().map((info: any, index: any) => ( {getChapters().map((info: any, index: any) => (
<Chapter key={index} info={info} index={index}></Chapter> <Chapter deleteChapter={deleteChapterUI} key={index} info={info} index={index}></Chapter>
))} ))}
{provided.placeholder} {provided.placeholder}
</div> </div>

View file

@ -41,3 +41,22 @@ export async function createChapter(data: any, course_id: any) {
return result; return result;
} }
export async function deleteChapter (coursechapter_id: any) {
const HeadersConfig = new Headers({ "Content-Type": "application/json" });
const requestOptions: any = {
method: "DELETE",
headers: HeadersConfig,
redirect: "follow",
credentials: "include",
};
const result: any = await fetch(`${getAPIUrl()}chapters/${coursechapter_id}`, requestOptions)
.then((result) => result.json())
.catch((error) => console.log("error", error));
console.log("result", result);
return result;
}

View file

@ -99,10 +99,10 @@ async def get_coursechapters_meta(course_id: str, current_user: PublicUser):
for coursechapter in coursechapters: for coursechapter in coursechapters:
coursechapter = CourseChapterInDB(**coursechapter) coursechapter = CourseChapterInDB(**coursechapter)
coursechapter_elementIds = [] coursechapter_elementIds = []
for element in coursechapter.elements: for element in coursechapter.elements:
coursechapter_elementIds.append(element.element_id) coursechapter_elementIds.append(element.element_id)
chapters[coursechapter.coursechapter_id] = { chapters[coursechapter.coursechapter_id] = {
"id": coursechapter.coursechapter_id, "name": coursechapter.name, "elementIds": coursechapter_elementIds "id": coursechapter.coursechapter_id, "name": coursechapter.name, "elementIds": coursechapter_elementIds
} }
@ -164,6 +164,7 @@ async def delete_coursechapter(coursechapter_id: str, current_user: PublicUser)
await check_database() await check_database()
coursechapters = learnhouseDB["coursechapters"] coursechapters = learnhouseDB["coursechapters"]
courses = learnhouseDB["courses"]
coursechapter = coursechapters.find_one( coursechapter = coursechapters.find_one(
{"coursechapter_id": coursechapter_id}) {"coursechapter_id": coursechapter_id})
@ -175,7 +176,9 @@ async def delete_coursechapter(coursechapter_id: str, current_user: PublicUser)
isDeleted = coursechapters.delete_one( isDeleted = coursechapters.delete_one(
{"coursechapter_id": coursechapter_id}) {"coursechapter_id": coursechapter_id})
# TODO : delete coursechapter from course using $pull https://www.mongodb.com/docs/v4.2/reference/operator/update/pull/ # Remove coursechapter from course
courses.update_one({"course_id": coursechapter["course_id"]}, {
"$pull": {"chapters": coursechapter_id}})
if isDeleted: if isDeleted:
return {"detail": "coursechapter deleted"} return {"detail": "coursechapter deleted"}