frondend/utils/index.ts

136 lines
3.8 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);
const url = `http://localhost/api/v1/messages/`;
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"
};
const url = `${process.env.api_url}aboutUs`;
console.log('Fetching from URL:', url);
try {
const response = await fetch(url, { headers });
console.log('Response status:', response.status);
const result = await response.json();
console.log('Fetched data:', result);
// 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"
};
const url = `${process.env.api_url}course/?skip=0&limit=100`;
console.log('Fetching from URL:', url);
try {
const response = await fetch(url, { headers });
console.log('Response status:', response.status);
const result = await response.json();
console.log('Fetched data:', result);
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),
}));
return coursesArray;
} catch (error) {
console.error('Fetch error:', error);
throw error;
}
}
export async function fetchCourse(id: string) {
const headers: HeadersInit = {
accept: "application/json"
};
const url = `${process.env.api_url}course/${id}`;
console.log('Fetching from URL:', url);
try {
const response = await fetch(url, { headers });
console.log('Response status:', response.status);
var result: CoursesProps = await response.json();
console.log('Fetched data:', result);
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"
};
const url = `${process.env.api_url}setting`;
console.log('Fetching from URL:', url);
try {
const response = await fetch(url, { headers });
console.log('Response status:', response.status);
const result: SettingsProps = await response.json();
console.log('Fetched data:', result);
return result;
}
catch (error) {
console.error('Fetch error:', error);
throw error;
}
}