2024-11-28 17:23:53 +00:00
|
|
|
"use client";
|
|
|
|
|
import axios from "axios";
|
|
|
|
|
import Cookies from "js-cookie";
|
|
|
|
|
import qs from "qs";
|
|
|
|
|
|
|
|
|
|
import axiosInstance from "./axiosInstance";
|
|
|
|
|
import axiosInstanceJson from "./axiosInstanceJson";
|
|
|
|
|
import { data } from "@/app/[locale]/(protected)/charts/rechart/charts-rechart-bar/data";
|
|
|
|
|
import { url } from "inspector";
|
|
|
|
|
|
|
|
|
|
const baseURL = "https://netidhub.com/api/";
|
|
|
|
|
const tokenAuth = Cookies.get("access_token")
|
|
|
|
|
? Cookies.get("access_token")
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
export async function postAPI(url: any, data: any) {
|
|
|
|
|
const headers = {
|
|
|
|
|
Authorization: `Bearer ${tokenAuth}`,
|
|
|
|
|
};
|
|
|
|
|
const response = await axiosInstance
|
|
|
|
|
.post(url, qs.stringify(data), { headers })
|
|
|
|
|
.catch((error) => error.response);
|
|
|
|
|
if (response?.status > 300) {
|
|
|
|
|
return {
|
|
|
|
|
error: true,
|
|
|
|
|
message: response?.data.error_description,
|
|
|
|
|
data: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
error: false,
|
|
|
|
|
message: "success",
|
|
|
|
|
data: response?.data,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getAPI(url: any, token: any) {
|
|
|
|
|
const headers = {
|
|
|
|
|
Authorization: `Bearer ${token || tokenAuth}`,
|
|
|
|
|
};
|
|
|
|
|
const response = await axios
|
|
|
|
|
.get(baseURL + url, { headers })
|
|
|
|
|
.catch((error) => error.response);
|
|
|
|
|
if (response?.status > 300) {
|
|
|
|
|
return {
|
|
|
|
|
error: true,
|
|
|
|
|
message: response?.data?.error_description,
|
|
|
|
|
data: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
error: false,
|
|
|
|
|
message: "success",
|
|
|
|
|
data: response?.data,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function postAPIWithJson(url: any, data: any, token: any) {
|
|
|
|
|
const headers = {
|
|
|
|
|
Authorization: `Bearer ${token || tokenAuth}`,
|
|
|
|
|
};
|
|
|
|
|
const response = await axiosInstanceJson
|
|
|
|
|
.post(url, data, { headers })
|
|
|
|
|
.catch((error) => error.response);
|
|
|
|
|
if (response?.status > 300) {
|
|
|
|
|
return {
|
|
|
|
|
error: true,
|
|
|
|
|
message: response?.data?.message,
|
|
|
|
|
data: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
error: false,
|
|
|
|
|
message: "success",
|
|
|
|
|
data: response?.data,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function deleteAPIWithJson(url: any) {
|
|
|
|
|
const response = await axiosInstanceJson
|
|
|
|
|
.delete(url)
|
|
|
|
|
.catch((error) => error.response);
|
|
|
|
|
if (response?.status > 300) {
|
|
|
|
|
return {
|
|
|
|
|
error: true,
|
|
|
|
|
message: response?.data?.message,
|
|
|
|
|
data: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
error: false,
|
|
|
|
|
message: "success",
|
|
|
|
|
data: response?.data,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getAPIInterceptor(url: any) {
|
|
|
|
|
const response = await axiosInterceptor
|
|
|
|
|
.get(url)
|
|
|
|
|
.catch((error) => error.response);
|
|
|
|
|
if (response?.status == 401) {
|
|
|
|
|
Object.keys(Cookies.get()).forEach((cookieName) => {
|
|
|
|
|
Cookies.remove(cookieName);
|
|
|
|
|
});
|
2024-12-24 12:55:28 +00:00
|
|
|
// window.location.href = "/in/auth";
|
2024-11-28 17:23:53 +00:00
|
|
|
} else if (response?.status > 300 && response?.status != 401) {
|
|
|
|
|
return {
|
|
|
|
|
error: true,
|
|
|
|
|
message: response?.data?.message,
|
|
|
|
|
data: null,
|
|
|
|
|
};
|
|
|
|
|
} else if (response?.data.success) {
|
|
|
|
|
return {
|
|
|
|
|
error: false,
|
|
|
|
|
message: "success",
|
|
|
|
|
data: response?.data,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
error: true,
|
|
|
|
|
message: response?.data?.message || null,
|
|
|
|
|
data: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-12-04 17:28:40 +00:00
|
|
|
// Fungsi postAPIInterceptor
|
|
|
|
|
export async function postAPIInterceptor(url: string, data: any) {
|
2024-11-28 17:23:53 +00:00
|
|
|
const response = await axiosInterceptor
|
|
|
|
|
.post(url, data)
|
|
|
|
|
.catch((error) => error.response);
|
2024-12-04 17:28:40 +00:00
|
|
|
|
|
|
|
|
if (response?.status === 401) {
|
2024-11-28 17:23:53 +00:00
|
|
|
Object.keys(Cookies.get()).forEach((cookieName) => {
|
|
|
|
|
Cookies.remove(cookieName);
|
|
|
|
|
});
|
2024-12-24 12:55:28 +00:00
|
|
|
// window.location.href = "/";
|
2024-12-04 17:28:40 +00:00
|
|
|
} else if (response?.status > 300 && response?.status !== 401) {
|
2024-11-28 17:23:53 +00:00
|
|
|
return {
|
|
|
|
|
error: true,
|
|
|
|
|
message: response?.data?.message,
|
|
|
|
|
data: null,
|
|
|
|
|
};
|
2024-12-04 17:28:40 +00:00
|
|
|
} else if (response?.data?.success) {
|
2024-11-28 17:23:53 +00:00
|
|
|
return {
|
|
|
|
|
error: false,
|
|
|
|
|
message: "success",
|
|
|
|
|
data: response?.data,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
error: true,
|
2024-12-04 17:28:40 +00:00
|
|
|
message: response?.data?.message,
|
2024-11-28 17:23:53 +00:00
|
|
|
data: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-04 17:28:40 +00:00
|
|
|
// Fungsi createTask
|
|
|
|
|
export async function createTask(data: any) {
|
|
|
|
|
const url = "assignment";
|
|
|
|
|
return postAPIInterceptor(url, data); // Perbaikan: Memisahkan parameter url dan data
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 17:23:53 +00:00
|
|
|
export async function deleteAPIInterceptor(url: any, data: any) {
|
|
|
|
|
const response = await axiosInterceptor
|
|
|
|
|
.delete(url, { data })
|
|
|
|
|
.catch((error) => error.response);
|
|
|
|
|
if (response?.status == 401) {
|
|
|
|
|
Object.keys(Cookies.get()).forEach((cookieName) => {
|
|
|
|
|
Cookies.remove(cookieName);
|
|
|
|
|
});
|
2024-12-24 12:55:28 +00:00
|
|
|
// window.location.href = "/";
|
2024-11-28 17:23:53 +00:00
|
|
|
} else if (response?.status > 300 && response?.status != 401) {
|
|
|
|
|
return {
|
|
|
|
|
error: true,
|
|
|
|
|
message: response?.data?.message,
|
|
|
|
|
data: null,
|
|
|
|
|
};
|
|
|
|
|
} else if (response?.data.success) {
|
|
|
|
|
return {
|
|
|
|
|
error: false,
|
|
|
|
|
message: "success",
|
|
|
|
|
data: response?.data,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
error: true,
|
|
|
|
|
message: response?.data?.message,
|
|
|
|
|
data: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getAPIInterceptorProfile(url: any) {
|
|
|
|
|
const response = await axiosInterceptor
|
|
|
|
|
.get(url)
|
|
|
|
|
.catch((error) => error.response);
|
|
|
|
|
if (response?.status > 300) {
|
|
|
|
|
return {
|
|
|
|
|
error: true,
|
|
|
|
|
message: response?.data?.message,
|
|
|
|
|
data: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
error: false,
|
|
|
|
|
message: response?.data?.message,
|
|
|
|
|
data: response?.data,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// export async function postFileApiInterceptor(url: any, data: any) {
|
|
|
|
|
// const response = await axiosInterceptor
|
|
|
|
|
// .post(url, data, {
|
|
|
|
|
// onUploadProgress: (progressEvent) => {
|
|
|
|
|
// const percentage = Math.round(
|
|
|
|
|
// (progressEvent.loaded * 100) / progressEvent.total
|
|
|
|
|
// );
|
|
|
|
|
// console.log(percentage);
|
|
|
|
|
// },
|
|
|
|
|
// })
|
|
|
|
|
// .catch((error) => error.response);
|
|
|
|
|
// if (response?.status > 300) {
|
|
|
|
|
// return {
|
|
|
|
|
// error: true,
|
|
|
|
|
// message: response?.data?.message,
|
|
|
|
|
// data: null,
|
|
|
|
|
// };
|
|
|
|
|
// }
|
|
|
|
|
// if (response?.data.success) {
|
|
|
|
|
// return {
|
|
|
|
|
// error: false,
|
|
|
|
|
// message: "success",
|
|
|
|
|
// data: response?.data,
|
|
|
|
|
// };
|
|
|
|
|
// }
|
|
|
|
|
// return {
|
|
|
|
|
// error: true,
|
|
|
|
|
// message: response?.data.message,
|
|
|
|
|
// data: null,
|
|
|
|
|
// };
|
|
|
|
|
// }
|