feat: refactor the entire learnhouse project

This commit is contained in:
swve 2023-10-13 20:03:27 +02:00
parent f556e41dda
commit 4c215e91d5
247 changed files with 7716 additions and 1013 deletions

View file

@ -0,0 +1,71 @@
import { getAPIUrl } from "@services/config/config";
import { RequestBody, RequestBodyForm, RequestBodyWithAuthHeader } from "@services/utils/ts/requests";
export async function createActivity(data: any, chapter_id: any, org_id: any) {
data.content = {};
// remove chapter_id from data
delete data.chapterId;
const result = await fetch(`${getAPIUrl()}activities/?coursechapter_id=${chapter_id}&org_id=${org_id}`, RequestBody("POST", data, null));
const res = await result.json();
return res;
}
export async function createFileActivity(file: File, type: string, data: any, chapter_id: any) {
// Send file thumbnail as form data
const formData = new FormData();
formData.append("coursechapter_id", chapter_id);
let org_id = "test";
let endpoint = "";
if (type === "video") {
formData.append("name", data.name);
formData.append("video_file", file);
endpoint = `${getAPIUrl()}activities/video?org_id=${org_id}`;
} else if (type === "documentpdf") {
formData.append("pdf_file", file);
formData.append("name", data.name);
endpoint = `${getAPIUrl()}activities/documentpdf?org_id=${org_id}`;
} else {
// Handle other file types here
}
const result: any = await fetch(endpoint, RequestBodyForm("POST", formData, null));
const res = await result.json();
return res;
}
export async function createExternalVideoActivity(data: any, activity: any, chapter_id: any) {
// add coursechapter_id to data
data.coursechapter_id = chapter_id;
data.activity_id = activity.id;
const result = await fetch(`${getAPIUrl()}activities/external_video?coursechapter_id=${chapter_id}`, RequestBody("POST", data, null));
const res = await result.json();
return res;
}
export async function getActivity(activity_id: any, next: any) {
const result = await fetch(`${getAPIUrl()}activities/${activity_id}`, RequestBody("GET", null, next));
const res = await result.json();
return res;
}
export async function deleteActivity(activity_id: any) {
const result = await fetch(`${getAPIUrl()}activities/${activity_id}`, RequestBody("DELETE", null, null));
const res = await result.json();
return res;
}
export async function getActivityWithAuthHeader(activity_id: any, next: any, access_token: string) {
const result = await fetch(`${getAPIUrl()}activities/activity_${activity_id}`, RequestBodyWithAuthHeader("GET", null, next, access_token));
const res = await result.json();
return res;
}
export async function updateActivity(data: any, activity_id: any) {
const result = await fetch(`${getAPIUrl()}activities/${activity_id}`, RequestBody("PUT", data, null));
const res = await result.json();
return res;
}

View file

@ -0,0 +1,25 @@
import { RequestBody, errorHandling } from "@services/utils/ts/requests";
import { getAPIUrl } from "@services/config/config";
/*
This file includes only POST, PUT, DELETE requests
GET requests are called from the frontend using SWR (https://swr.vercel.app/)
*/
export async function startCourse(course_id: string, org_slug: string) {
const result: any = await fetch(`${getAPIUrl()}trail/org_slug/${org_slug}/add_course/${course_id}`, RequestBody("POST", null, null))
const res = await errorHandling(result);
return res;
}
export async function removeCourse(course_id: string, org_slug: string) {
const result: any = await fetch(`${getAPIUrl()}trail/org_slug/${org_slug}/remove_course/${course_id}`, RequestBody("POST", null, null))
const res = await errorHandling(result);
return res;
}
export async function markActivityAsComplete(org_slug: string, course_id: string, activity_id: string) {
const result: any = await fetch(`${getAPIUrl()}trail/org_slug/${org_slug}/add_activity/course_id/${course_id}/activity_id/${activity_id}`, RequestBody("POST", null, null))
const res = await errorHandling(result);
return res;
}

View file

@ -0,0 +1,41 @@
import { getAPIUrl } from "@services/config/config";
import { RequestBody, RequestBodyWithAuthHeader, errorHandling } from "@services/utils/ts/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, next: any) {
const result = await fetch(`${getAPIUrl()}chapters/meta/course_${course_id}`, RequestBody("GET", null, next));
const res = await errorHandling(result);
return res;
}
export async function updateChaptersMetadata(course_id: any, data: any) {
const result: any = await fetch(`${getAPIUrl()}chapters/meta/course_${course_id}`, RequestBody("PUT", data, null));
const res = await errorHandling(result);
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);
return res;
}
export async function deleteChapter(coursechapter_id: any) {
const result: any = await fetch(`${getAPIUrl()}chapters/${coursechapter_id}`, RequestBody("DELETE", null, null));
const res = await errorHandling(result);
return res;
}

View file

@ -0,0 +1,47 @@
import { getAPIUrl } from "../config/config";
import { RequestBody, RequestBodyWithAuthHeader, errorHandling } from "@services/utils/ts/requests";
/*
This file includes only POST, PUT, DELETE requests
GET requests are called from the frontend using SWR (https://swr.vercel.app/)
*/
export async function deleteCollection(collection_id: any) {
const result: any = await fetch(`${getAPIUrl()}collections/${collection_id}`, RequestBody("DELETE", null, null));
const res = await errorHandling(result);
return res;
}
// Create a new collection
export async function createCollection(collection: any) {
const result: any = await fetch(`${getAPIUrl()}collections/`, RequestBody("POST", collection, null));
const res = await errorHandling(result);
return res;
}
// Get a colletion by id
export async function getCollectionById(collection_id: any) {
const result: any = await fetch(`${getAPIUrl()}collections/${collection_id}`, { next: { revalidate: 10 } });
const res = await errorHandling(result);
return res;
}
export async function getCollectionByIdWithAuthHeader(collection_id: any, access_token: string, next: any) {
const result: any = await fetch(`${getAPIUrl()}collections/collection_${collection_id}`, RequestBodyWithAuthHeader("GET", null, next, access_token));
const res = await errorHandling(result);
return res;
}
// Get collections
// TODO : add per org filter
export async function getOrgCollections() {
const result: any = await fetch(`${getAPIUrl()}collections/page/1/limit/10`, { next: { revalidate: 10 } });
const res = await errorHandling(result);
return res;
}
export async function getOrgCollectionsWithAuthHeader(org_id: string, access_token: string, next: any) {
const result: any = await fetch(`${getAPIUrl()}collections/org_id/${org_id}/page/1/limit/10`, RequestBodyWithAuthHeader("GET", null, next, access_token));
const res = await errorHandling(result);
return res;
}

View file

@ -0,0 +1,57 @@
import { getAPIUrl } from "@services/config/config";
import { RequestBody, RequestBodyForm, RequestBodyWithAuthHeader, errorHandling } from "@services/utils/ts/requests";
/*
This file includes only POST, PUT, DELETE requests
GET requests are called from the frontend using SWR (https://swr.vercel.app/)
*/
export async function getOrgCourses(org_id: number, next: any) {
const result: any = await fetch(`${getAPIUrl()}courses/org_slug/${org_id}/page/1/limit/10`, RequestBody("GET", null, next));
const res = await errorHandling(result);
return res;
}
export async function getOrgCoursesWithAuthHeader(org_id: number, next: any, access_token: string) {
const result: any = await fetch(`${getAPIUrl()}courses/org_slug/${org_id}/page/1/limit/10`, RequestBodyWithAuthHeader("GET", null, next, access_token));
const res = await errorHandling(result);
return res;
}
export async function getCourseMetadataWithAuthHeader(course_id: any, next: any, access_token: string) {
const result = await fetch(`${getAPIUrl()}courses/meta/course_${course_id}`, RequestBodyWithAuthHeader("GET", null, next, access_token));
const res = await errorHandling(result);
return res;
}
export async function updateCourse(course_id: any, data: any) {
const result: any = await fetch(`${getAPIUrl()}courses/course_${course_id}`, RequestBody("PUT", data, null));
const res = await errorHandling(result);
return res;
}
export async function getCourse(course_id: string, next: any) {
const result: any = await fetch(`${getAPIUrl()}courses/${course_id}`, RequestBody("GET", null, next));
const res = await errorHandling(result);
return res;
}
export async function createNewCourse(org_id: string, course_body: any, thumbnail: any) {
// Send file thumbnail as form data
const formData = new FormData();
formData.append("thumbnail", thumbnail);
formData.append("name", course_body.name);
formData.append("description", course_body.description);
formData.append("mini_description", "course_body.mini_description");
formData.append("public", "true");
const result = await fetch(`${getAPIUrl()}courses/?org_id=${org_id}`, RequestBodyForm("POST", formData, null));
const res = await errorHandling(result);
return res;
}
export async function deleteCourseFromBackend(course_id: any) {
const result: any = await fetch(`${getAPIUrl()}courses/${course_id}`, RequestBody("DELETE", null, null));
const res = await errorHandling(result);
return res;
}