mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: init CS + SS course chapter deletion
This commit is contained in:
parent
5331e4db90
commit
0572368a32
4 changed files with 37 additions and 6 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.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">
|
||||
{(provided) => (
|
||||
<ElementsList {...provided.droppableProps} ref={provided.innerRef}>
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import { Title } from "../../../../../../components/ui/styles/title";
|
|||
import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd";
|
||||
import { initialData, initialData2 } from "../../../../../../components/drags/data";
|
||||
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 NewChapterModal from "../../../../../../components/modals/chapters/new";
|
||||
|
||||
|
|
@ -59,6 +59,15 @@ function CourseEdit() {
|
|||
setNewChapterModal(false);
|
||||
};
|
||||
|
||||
const deleteChapterUI = async (chapterId: any) => {
|
||||
console.log("deleteChapter", chapterId);
|
||||
await deleteChapter(chapterId);
|
||||
|
||||
getCourseChapters();
|
||||
};
|
||||
|
||||
|
||||
|
||||
// Close new chapter modal
|
||||
const closeModal = () => {
|
||||
setNewChapterModal(false);
|
||||
|
|
@ -177,7 +186,7 @@ function CourseEdit() {
|
|||
{(provided) => (
|
||||
<div key={"chapters"} {...provided.droppableProps} ref={provided.innerRef}>
|
||||
{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}
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -41,3 +41,22 @@ export async function createChapter(data: any, course_id: any) {
|
|||
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -99,10 +99,10 @@ async def get_coursechapters_meta(course_id: str, current_user: PublicUser):
|
|||
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
|
||||
}
|
||||
|
|
@ -164,6 +164,7 @@ async def delete_coursechapter(coursechapter_id: str, current_user: PublicUser)
|
|||
await check_database()
|
||||
|
||||
coursechapters = learnhouseDB["coursechapters"]
|
||||
courses = learnhouseDB["courses"]
|
||||
|
||||
coursechapter = coursechapters.find_one(
|
||||
{"coursechapter_id": coursechapter_id})
|
||||
|
|
@ -175,7 +176,9 @@ async def delete_coursechapter(coursechapter_id: str, current_user: PublicUser)
|
|||
isDeleted = coursechapters.delete_one(
|
||||
{"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:
|
||||
return {"detail": "coursechapter deleted"}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue