diff --git a/front/components/Pages/CourseEdit/Draggables/Chapter.tsx b/front/components/Pages/CourseEdit/Draggables/Chapter.tsx index 6bd4787f..d0bafbd9 100644 --- a/front/components/Pages/CourseEdit/Draggables/Chapter.tsx +++ b/front/components/Pages/CourseEdit/Draggables/Chapter.tsx @@ -2,10 +2,39 @@ import React from "react"; import styled from "styled-components"; import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd"; import Activity from "./Activity"; -import { Folders, Hexagon, MoreVertical, PlusSquare, Sparkle, Sparkles, Trash, Trash2, X } from "lucide-react"; +import { Folders, Hexagon, MoreVertical, Pencil, PlusSquare, Save, Sparkle, Sparkles, Trash, Trash2, X } from "lucide-react"; import ConfirmationModal from "@components/StyledElements/ConfirmationModal/ConfirmationModal"; +import { useRouter } from "next/navigation"; +import { updateChapter } from "@services/courses/chapters"; +import { mutate } from "swr"; +import { getAPIUrl } from "@services/config/config"; +import { revalidateTags } from "@services/utils/ts/requests"; + +interface ModifiedChapterInterface { + chapterId: string; + chapterName: string; +} function Chapter(props: any) { + const router = useRouter(); + const [modifiedChapter, setModifiedChapter] = React.useState(undefined); + const [selectedChapter, setSelectedChapter] = React.useState(undefined); + + async function updateChapterName(chapterId: string) { + if (modifiedChapter?.chapterId === chapterId) { + setSelectedChapter(undefined); + let modifiedChapterCopy = { + name: modifiedChapter.chapterName, + description: '', + activities: props.info.list.chapter.activityIds, + } + await updateChapter(chapterId, modifiedChapterCopy) + await mutate(`${getAPIUrl()}chapters/meta/course_${props.courseid}`) + await revalidateTags(['courses'], props.orgslug) + router.refresh(); + } + } + return ( {(provided, snapshot) => ( @@ -23,7 +52,17 @@ function Chapter(props: any) { -

{props.info.list.chapter.name}

+
+ + {selectedChapter === props.info.list.chapter.id ? + (
+ setModifiedChapter({ chapterId: props.info.list.chapter.id, chapterName: e.target.value })} /> + +
) : (

{props.info.list.chapter.name}

)} + setSelectedChapter(props.info.list.chapter.id)} /> +
{ async function checkRefreshToken() { - deleteCookie("access_token_cookie"); + //deleteCookie("access_token_cookie"); let data = await getNewAccessTokenUsingRefreshToken(); if (data) { return data.access_token; diff --git a/front/services/courses/chapters.ts b/front/services/courses/chapters.ts index 35a8887c..0049cf08 100644 --- a/front/services/courses/chapters.ts +++ b/front/services/courses/chapters.ts @@ -21,6 +21,12 @@ export async function updateChaptersMetadata(course_id: any, data: any) { return res; } +export async function updateChapter(coursechapter_id: any, data: any) { + const result: any = await fetch(`${getAPIUrl()}chapters/${coursechapter_id}`, RequestBody("PUT", data, null)); + const res = await errorHandling(result); + return res; +} + export async function createChapter(data: any, course_id: any) { const result: any = await fetch(`${getAPIUrl()}chapters/?course_id=course_${course_id}`, RequestBody("POST", data, null)); const res = await errorHandling(result);