From f4466d1932f860f6a7faaae4253beae8e8abb59c Mon Sep 17 00:00:00 2001 From: swve Date: Thu, 2 Feb 2023 21:34:45 +0100 Subject: [PATCH] chore: reformat chapters ts service --- front/services/courses/chapters.ts | 67 ++++++------------------------ 1 file changed, 13 insertions(+), 54 deletions(-) diff --git a/front/services/courses/chapters.ts b/front/services/courses/chapters.ts index 79819c95..68c713f6 100644 --- a/front/services/courses/chapters.ts +++ b/front/services/courses/chapters.ts @@ -1,82 +1,41 @@ import { initialData } from "../../components/Drags/data"; import { getAPIUrl } from "@services/config"; +import { RequestBody } from "@services/utils/requests"; +/* + This file includes only POST, PUT, DELETE requests + GET requests are called from the frontend using SWR (https://swr.vercel.app/) +*/ + +//TODO : depreciate this function 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) + const data: any = await fetch(`${getAPIUrl()}chapters/meta/course_${course_id}`, RequestBody("GET", null)) .then((result) => result.json()) .catch((error) => console.log("error", error)); - console.log("data", data); - return data; } export async function updateChaptersMetadata(course_id: any, data: any) { - const HeadersConfig = new Headers({ "Content-Type": "application/json" }); - - const requestOptions: any = { - method: "PUT", - headers: HeadersConfig, - redirect: "follow", - credentials: "include", - body: JSON.stringify(data), - }; - - const result: any = await fetch(`${getAPIUrl()}chapters/meta/course_${course_id}`, requestOptions) + const result: any = await fetch(`${getAPIUrl()}chapters/meta/course_${course_id}`, RequestBody("PUT", data)) .then((result) => result.json()) .catch((error) => console.log("error", error)); - - console.log("result", result); - + return result; } export async function createChapter(data: any, course_id: any) { - console.log("data", data, course_id); - - const HeadersConfig = new Headers({ "Content-Type": "application/json" }); - - const requestOptions: any = { - method: "POST", - headers: HeadersConfig, - redirect: "follow", - credentials: "include", - body: JSON.stringify(data), - }; - - const result: any = await fetch(`${getAPIUrl()}chapters/?course_id=course_${course_id}`, requestOptions) + const result: any = await fetch(`${getAPIUrl()}chapters/?course_id=course_${course_id}`, RequestBody("POST", data)) .then((result) => result.json()) .catch((error) => console.log("error", error)); - console.log("result", 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) +export async function deleteChapter(coursechapter_id: any) { + const result: any = await fetch(`${getAPIUrl()}chapters/${coursechapter_id}`, RequestBody("DELETE", null)) .then((result) => result.json()) .catch((error) => console.log("error", error)); - console.log("result", result); - return result; }