frondend/utils/index.ts

139 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-10-05 15:04:17 +08:00
2024-10-06 11:52:45 +08:00
import { CoursesProps, CoursesArrayProps, SettingsProps, MessageProps, AboutusProps } from "@/types";
2024-10-05 15:04:17 +08:00
export async function postMessage(data: MessageProps) {
const headers: HeadersInit = {
"Accept": "application/json",
"Content-Type": "application/json",
};
const body = JSON.stringify(data);
2024-10-08 13:35:35 +08:00
const url = `${process.env.NEXT_PUBLIC_API_URL}messages/`;
2024-10-05 15:04:17 +08:00
try {
const response = await fetch(url, {
method: "POST",
headers,
body,
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const contentType = response.headers.get("content-type");
if (contentType && contentType.indexOf("application/json") !== -1) {
return { success: true }
} else {
const text = await response.text();
throw new Error(`Expected JSON, got ${contentType}: ${text}`);
}
} catch (error) {
console.error("Error in postMessage:", error);
return { success: false };
}
}
2024-10-06 11:52:45 +08:00
export async function fetchAboutus() {
const headers: HeadersInit = {
accept: "application/json"
};
2024-10-08 13:35:35 +08:00
const url = `${process.env.NEXT_PUBLIC_API_URL}aboutUs/`;
2024-10-08 15:29:36 +08:00
2024-10-06 11:52:45 +08:00
try {
const response = await fetch(url, { headers });
2024-10-08 15:29:36 +08:00
2024-10-06 11:52:45 +08:00
const result = await response.json();
2024-10-08 15:29:36 +08:00
2024-10-06 11:52:45 +08:00
// Sort the result by index
const sortedResult: AboutusProps[] = result.data.sort((a: any, b: any) => a.index - b.index);
return sortedResult;
}
catch (error) {
console.error('Fetch error:', error);
throw error;
}
}
2024-10-05 15:04:17 +08:00
export async function fetchCourses() {
const headers: HeadersInit = {
accept: "application/json"
};
2024-10-08 13:35:35 +08:00
const url = `${process.env.NEXT_PUBLIC_API_URL}course/?skip=0&limit=100`;
2024-10-05 15:04:17 +08:00
console.log('Fetching from URL:', url);
try {
const response = await fetch(url, { headers });
2024-10-08 15:29:36 +08:00
2024-10-05 15:04:17 +08:00
const result = await response.json();
2024-10-08 15:29:36 +08:00
2024-10-05 15:04:17 +08:00
const coursesArray: CoursesProps[] = result.data.map((course: CoursesProps) => ({
...course,
images: course.images.sort((a: any, b: any) => a.index - b.index),
info_images: course?.info_images?.sort((a: any, b: any) => a.index - b.index),
}));
2024-10-08 13:35:35 +08:00
const sortedCoursesArray = coursesArray.sort((a, b) =>
2024-10-08 13:44:48 +08:00
new Date(a.created_at).getTime() - new Date(a.created_at).getTime()
2024-10-08 13:35:35 +08:00
);
return sortedCoursesArray;
2024-10-05 15:04:17 +08:00
} catch (error) {
2024-10-08 15:29:36 +08:00
2024-10-05 15:04:17 +08:00
throw error;
}
}
export async function fetchCourse(id: string) {
const headers: HeadersInit = {
accept: "application/json"
};
2024-10-08 13:35:35 +08:00
const url = `${process.env.NEXT_PUBLIC_API_URL}course/${id}/`;
2024-10-08 15:29:36 +08:00
2024-10-05 15:04:17 +08:00
try {
const response = await fetch(url, { headers });
2024-10-08 15:29:36 +08:00
2024-10-05 15:04:17 +08:00
var result: CoursesProps = await response.json();
2024-10-08 15:29:36 +08:00
2024-10-05 15:04:17 +08:00
if (result.info_images && Array.isArray(result.info_images)) {
result.info_images.sort((a, b) => a.index - b.index);
}
return result;
} catch (error) {
console.error('Fetch error:', error);
throw error;
}
}
export async function fetchSettings() {
const headers: HeadersInit = {
accept: "application/json"
};
2024-10-08 13:35:35 +08:00
const url = `${process.env.NEXT_PUBLIC_API_URL}setting/`;
2024-10-08 15:29:36 +08:00
2024-10-05 15:04:17 +08:00
try {
const response = await fetch(url, { headers });
2024-10-08 15:29:36 +08:00
2024-10-05 15:04:17 +08:00
const result: SettingsProps = await response.json();
2024-10-08 15:29:36 +08:00
2024-10-05 15:04:17 +08:00
return result;
}
catch (error) {
console.error('Fetch error:', error);
throw error;
}
}