mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: reformat utility req functions to use next
This commit is contained in:
parent
1f6020efc0
commit
bb0f9166f9
14 changed files with 43 additions and 41 deletions
|
|
@ -7,14 +7,14 @@ export async function uploadNewImageFile(file: any, activity_id: string) {
|
|||
formData.append("file_object", file);
|
||||
formData.append("activity_id", activity_id);
|
||||
|
||||
return fetch(`${getAPIUrl()}blocks/image`, RequestBodyForm("POST", formData))
|
||||
return fetch(`${getAPIUrl()}blocks/image`, RequestBodyForm("POST", formData, null))
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
}
|
||||
|
||||
export async function getImageFile(file_id: string) {
|
||||
// todo : add course id to url
|
||||
return fetch(`${getAPIUrl()}blocks/image?file_id=${file_id}`, RequestBody("GET", null))
|
||||
return fetch(`${getAPIUrl()}blocks/image?file_id=${file_id}`, RequestBody("GET", null, null))
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,14 +7,14 @@ export async function uploadNewPDFFile(file: any, activity_id: string) {
|
|||
formData.append("file_object", file);
|
||||
formData.append("activity_id", activity_id);
|
||||
|
||||
return fetch(`${getAPIUrl()}blocks/pdf`, RequestBodyForm("POST", formData))
|
||||
return fetch(`${getAPIUrl()}blocks/pdf`, RequestBodyForm("POST", formData, null))
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
}
|
||||
|
||||
export async function getPDFFile(file_id: string) {
|
||||
// todo : add course id to url
|
||||
return fetch(`${getAPIUrl()}blocks/pdf?file_id=${file_id}`, RequestBody("GET", null))
|
||||
return fetch(`${getAPIUrl()}blocks/pdf?file_id=${file_id}`, RequestBody("GET", null, null))
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import { RequestBody, RequestBodyForm } from "@services/utils/ts/requests";
|
|||
|
||||
|
||||
export async function submitQuizBlock(activity_id: string, data: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}blocks/quiz/${activity_id}"`, RequestBody("POST", data))
|
||||
const result: any = await fetch(`${getAPIUrl()}blocks/quiz/${activity_id}"`, RequestBody("POST", data, null))
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
return result;
|
||||
|
|
|
|||
|
|
@ -7,13 +7,13 @@ export async function uploadNewVideoFile(file: any, activity_id: string) {
|
|||
formData.append("file_object", file);
|
||||
formData.append("activity_id", activity_id);
|
||||
|
||||
return fetch(`${getAPIUrl()}blocks/video`, RequestBodyForm("POST", formData))
|
||||
return fetch(`${getAPIUrl()}blocks/video`, RequestBodyForm("POST", formData, null))
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
}
|
||||
|
||||
export async function getVideoFile(file_id: string) {
|
||||
return fetch(`${getAPIUrl()}blocks/video?file_id=${file_id}`, RequestBody("GET", null))
|
||||
return fetch(`${getAPIUrl()}blocks/video?file_id=${file_id}`, RequestBody("GET", null, null))
|
||||
.then((result) => result.json())
|
||||
.catch((error) => console.log("error", error));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ export async function createActivity(data: any, chapter_id: any, org_id: any) {
|
|||
// 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));
|
||||
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;
|
||||
}
|
||||
|
|
@ -31,7 +31,7 @@ export async function createFileActivity(file: File, type: string, data: any, ch
|
|||
// Handle other file types here
|
||||
}
|
||||
|
||||
const result: any = await fetch(endpoint, RequestBodyForm("POST", formData));
|
||||
const result: any = await fetch(endpoint, RequestBodyForm("POST", formData, null));
|
||||
const res = await result.json();
|
||||
return res;
|
||||
}
|
||||
|
|
@ -41,19 +41,19 @@ export async function createExternalVideoActivity(data: any, activity: any, chap
|
|||
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));
|
||||
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) {
|
||||
const result = await fetch(`${getAPIUrl()}activities/${activity_id}`, RequestBody("GET", null));
|
||||
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 updateActivity(data: any, activity_id: any) {
|
||||
const result = await fetch(`${getAPIUrl()}activities/${activity_id}`, RequestBody("PUT", data));
|
||||
const result = await fetch(`${getAPIUrl()}activities/${activity_id}`, RequestBody("PUT", data, null));
|
||||
const res = await result.json();
|
||||
return res;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,19 +7,19 @@ import { getAPIUrl } from "@services/config/config";
|
|||
*/
|
||||
|
||||
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))
|
||||
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))
|
||||
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))
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,27 +7,27 @@ import { RequestBody, errorHandling } from "@services/utils/ts/requests";
|
|||
*/
|
||||
|
||||
//TODO : depreciate this function
|
||||
export async function getCourseChaptersMetadata(course_id: any) {
|
||||
const result = await fetch(`${getAPIUrl()}chapters/meta/course_${course_id}`, RequestBody("GET", null));
|
||||
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));
|
||||
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 createChapter(data: any, course_id: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}chapters/?course_id=course_${course_id}`, RequestBody("POST", data));
|
||||
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));
|
||||
const result: any = await fetch(`${getAPIUrl()}chapters/${coursechapter_id}`, RequestBody("DELETE", null, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,14 +7,14 @@ import { RequestBody, errorHandling } from "@services/utils/ts/requests";
|
|||
*/
|
||||
|
||||
export async function deleteCollection(collection_id: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}collections/${collection_id}`, RequestBody("DELETE", null));
|
||||
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));
|
||||
const result: any = await fetch(`${getAPIUrl()}collections/`, RequestBody("POST", collection, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
|
@ -22,7 +22,7 @@ export async function createCollection(collection: any) {
|
|||
// Get collections
|
||||
// TODO : add per org filter
|
||||
export async function getOrgCollections() {
|
||||
const result: any = await fetch(`${getAPIUrl()}collections/page/1/limit/10`);
|
||||
const result: any = await fetch(`${getAPIUrl()}collections/page/1/limit/10`, { next: { revalidate: 10 } });
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,15 +6,15 @@ import { RequestBody, RequestBodyForm, errorHandling } from "@services/utils/ts/
|
|||
GET requests are called from the frontend using SWR (https://swr.vercel.app/)
|
||||
*/
|
||||
|
||||
export async function getOrgCourses(org_id: number) {
|
||||
const result: any = await fetch(`${getAPIUrl()}courses/org_slug/${org_id}/page/1/limit/10`, RequestBody("GET", null));
|
||||
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 getCourse(course_id: string) {
|
||||
const result: any = await fetch(`${getAPIUrl()}courses/${course_id}`, RequestBody("GET", null));
|
||||
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;
|
||||
}
|
||||
|
|
@ -28,13 +28,13 @@ export async function createNewCourse(org_id: string, course_body: any, thumbnai
|
|||
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));
|
||||
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));
|
||||
const result: any = await fetch(`${getAPIUrl()}courses/${course_id}`, RequestBody("DELETE", null, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,19 +7,19 @@ import { RequestBody, errorHandling } from "@services/utils/ts/requests";
|
|||
*/
|
||||
|
||||
export async function createNewOrganization(body: any) {
|
||||
const result = await fetch(`${getAPIUrl()}orgs/`, RequestBody("POST", body));
|
||||
const result = await fetch(`${getAPIUrl()}orgs/`, RequestBody("POST", body, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function deleteOrganizationFromBackend(org_id: any) {
|
||||
const result = await fetch(`${getAPIUrl()}orgs/${org_id}`, RequestBody("DELETE", null));
|
||||
const result = await fetch(`${getAPIUrl()}orgs/${org_id}`, RequestBody("DELETE", null, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
||||
export async function getOrganizationContextInfo(org_slug: any) {
|
||||
const result = await fetch(`${getAPIUrl()}orgs/slug/${org_slug}`, RequestBody("GET", null));
|
||||
export async function getOrganizationContextInfo(org_slug: any, next: any) {
|
||||
const result = await fetch(`${getAPIUrl()}orgs/slug/${org_slug}`, RequestBody("GET", null,next));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import { RequestBody, errorHandling } from "@services/utils/ts/requests";
|
|||
*/
|
||||
|
||||
export async function updateOrganization(org_id: string, data: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}orgs/` + org_id, RequestBody("PUT", data));
|
||||
const result: any = await fetch(`${getAPIUrl()}orgs/` + org_id, RequestBody("PUT", data, null));
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import { RequestBody, errorHandling } from "@services/utils/ts/requests";
|
|||
*/
|
||||
|
||||
export async function updatePassword(user_id : string, data: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}users/password/user_id/` + user_id, RequestBody("PUT", data))
|
||||
const result: any = await fetch(`${getAPIUrl()}users/password/user_id/` + user_id, RequestBody("PUT", data, null))
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import { RequestBody, errorHandling } from "@services/utils/ts/requests";
|
|||
*/
|
||||
|
||||
export async function updateProfile(data: any) {
|
||||
const result: any = await fetch(`${getAPIUrl()}users/user_id/` + data.user_id, RequestBody("PUT", data))
|
||||
const result: any = await fetch(`${getAPIUrl()}users/user_id/` + data.user_id, RequestBody("PUT", data, null))
|
||||
const res = await errorHandling(result);
|
||||
return res;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
import { AppRouterInstance } from "next/dist/shared/lib/app-router-context";
|
||||
import { denyAccessToUser } from "../react/middlewares/views";
|
||||
|
||||
export const RequestBody = (method: string, data: any) => {
|
||||
export const RequestBody = (method: string, data: any, next: any) => {
|
||||
let HeadersConfig = new Headers({ "Content-Type": "application/json" });
|
||||
let options: any = {
|
||||
method: method,
|
||||
headers: HeadersConfig,
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
// Next.js
|
||||
cache: 'no-store'
|
||||
// Next.js
|
||||
next: next,
|
||||
};
|
||||
if (data) {
|
||||
options.body = JSON.stringify(data);
|
||||
|
|
@ -17,7 +17,7 @@ export const RequestBody = (method: string, data: any) => {
|
|||
return options;
|
||||
};
|
||||
|
||||
export const RequestBodyForm = (method: string, data: any) => {
|
||||
export const RequestBodyForm = (method: string, data: any, next: any) => {
|
||||
let HeadersConfig = new Headers({});
|
||||
let options: any = {
|
||||
method: method,
|
||||
|
|
@ -25,6 +25,8 @@ export const RequestBodyForm = (method: string, data: any) => {
|
|||
redirect: "follow",
|
||||
credentials: "include",
|
||||
body: data,
|
||||
// Next.js
|
||||
next: next,
|
||||
};
|
||||
return options;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue