mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
chore: refactor courses ts
This commit is contained in:
parent
f4466d1932
commit
ab96e34029
2 changed files with 19 additions and 38 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import { getAPIUrl } from "@services/config";
|
||||
import { RequestBody, RequestBodyForm } from "@services/utils/requests";
|
||||
|
||||
/*
|
||||
This file includes only POST, PUT, DELETE requests
|
||||
|
|
@ -6,39 +7,22 @@ import { getAPIUrl } from "@services/config";
|
|||
*/
|
||||
|
||||
export async function getOrgCourses(org_id: number) {
|
||||
const HeadersConfig = new Headers({ "Content-Type": "application/json" });
|
||||
|
||||
|
||||
const requestOptions: any = {
|
||||
method: "GET",
|
||||
headers: HeadersConfig,
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
};
|
||||
|
||||
return fetch(`${getAPIUrl()}courses/${org_id}/page/1/limit/10`, requestOptions)
|
||||
return fetch(`${getAPIUrl()}courses/${org_id}/page/1/limit/10`, RequestBody("GET", null))
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
}
|
||||
|
||||
export async function getCourse(course_id: string) {
|
||||
const HeadersConfig = new Headers({ "Content-Type": "application/json" });
|
||||
|
||||
const requestOptions: any = {
|
||||
method: "GET",
|
||||
headers: HeadersConfig,
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
};
|
||||
|
||||
// todo : add course id to url
|
||||
return fetch(`${getAPIUrl()}courses/${course_id}`, requestOptions)
|
||||
return fetch(`${getAPIUrl()}courses/${course_id}`, RequestBody("GET", null))
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
}
|
||||
|
||||
|
||||
export async function createNewCourse(org_id: string, course_body: any, thumbnail: any) {
|
||||
const HeadersConfig = new Headers();
|
||||
|
||||
// Send file thumbnail as form data
|
||||
const formData = new FormData();
|
||||
|
|
@ -48,30 +32,16 @@ export async function createNewCourse(org_id: string, course_body: any, thumbnai
|
|||
formData.append("mini_description", "course_body.mini_description");
|
||||
formData.append("public", "true");
|
||||
|
||||
const requestOptions: any = {
|
||||
method: "POST",
|
||||
headers: HeadersConfig,
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
body: formData,
|
||||
};
|
||||
|
||||
return fetch(`${getAPIUrl()}courses/?org_id=${org_id}`, requestOptions)
|
||||
return fetch(`${getAPIUrl()}courses/?org_id=${org_id}`, RequestBodyForm("POST", formData))
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
}
|
||||
|
||||
export async function deleteCourseFromBackend(course_id: any) {
|
||||
const HeadersConfig = new Headers({ "Content-Type": "application/json" });
|
||||
|
||||
|
||||
const requestOptions: any = {
|
||||
method: "DELETE",
|
||||
headers: HeadersConfig,
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
};
|
||||
|
||||
return fetch(`${getAPIUrl()}courses/${course_id}`, requestOptions)
|
||||
return fetch(`${getAPIUrl()}courses/${course_id}`, RequestBody("DELETE", null))
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,8 +12,19 @@ export const RequestBody = (method: string, data: any) => {
|
|||
return options;
|
||||
};
|
||||
|
||||
export const swrFetcher = async (url: string, body: any) => {
|
||||
export const RequestBodyForm = (method: string, data: any) => {
|
||||
let HeadersConfig = new Headers({});
|
||||
let options: any = {
|
||||
method: method,
|
||||
headers: HeadersConfig,
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
body: data,
|
||||
};
|
||||
return options;
|
||||
};
|
||||
|
||||
export const swrFetcher = async (url: string, body: any) => {
|
||||
// Create the request options
|
||||
let HeadersConfig = new Headers({ "Content-Type": "application/json" });
|
||||
let options: any = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue