import Project from "@/ApiClasses/Project"; import SectionCell from "@/ApiClasses/SectionCell"; import { axiosInstance, defaultApiErrorHandler, defaultApiSuccessHandler } from "@/services/api" axiosInstance.interceptors.response.use( (response) => response, // Directly return successful responses. async (error) => { const originalRequest = error.config; if ( ((error.response && error.response.status === 401) || error.code == "ERR_NETWORK") && !originalRequest._retry && store.authenticated ) { originalRequest._retry = true; // Mark the request as retried to avoid infinite loops. try { await store.refreshUserToken(); // Update the authorization header with the new access token. axiosInstance.defaults.headers.common["Authorization"] = `Bearer ${store.user.token}`; return axiosInstance(originalRequest); // Retry the original request with the new access token. } catch (refreshError) { // Handle refresh token errors by clearing stored tokens and redirecting to the login page. console.error("Token refresh failed:", refreshError); localStorage.removeItem("accessToken"); localStorage.removeItem("refreshToken"); router.push("/login"); return Promise.reject(refreshError); } } return Promise.reject(error); // For all other errors, return the error as is. } ); // Entrepreneurs API function requestProjectCreation( projectDetails: Project, // Replace 'any' with a proper type for project details if available onSuccessHandler?: (response: AxiosResponse) => void, onErrorHandler?: (error: AxiosError) => void ): void { axiosInstance .post("/entrepreneur/projects/request", projectDetails) .then((response) => { if (onSuccessHandler) { onSuccessHandler(response); } else { defaultApiSuccessHandler(response); } }) .catch((error: AxiosError) => { if (onErrorHandler) { onErrorHandler(error); } else { defaultApiErrorHandler(error); } }); } function addSectionCell( sectionCellDetails: SectionCell, onSuccessHandler?: (response: AxiosResponse) => void, onErrorHandler?: (error: AxiosError) => void ): void { axiosInstance .post("/entrepreneur/sectionCells", sectionCellDetails.toPlainObject()) // <-- Ici .then((response) => { if (onSuccessHandler) { onSuccessHandler(response); } else { defaultApiSuccessHandler(response); } }) .catch((error: AxiosError) => { if (onErrorHandler) { onErrorHandler(error); } else { defaultApiErrorHandler(error); } }); } function modifySectionCell( sectionCellId: number, sectionCellDetails: SectionCell, // Replace 'any' with a proper type for section cell details if available onSuccessHandler?: (response: AxiosResponse) => void, onErrorHandler?: (error: AxiosError) => void ): void { axiosInstance .put(`/entrepreneur/sectionCells/${sectionCellId}`, sectionCellDetails) .then((response) => { if (onSuccessHandler) { onSuccessHandler(response); } else { defaultApiSuccessHandler(response); } }) .catch((error: AxiosError) => { if (onErrorHandler) { onErrorHandler(error); } else { defaultApiErrorHandler(error); } }); } function removeSectionCell( sectionCellId: number, onSuccessHandler?: (response: AxiosResponse) => void, onErrorHandler?: (error: AxiosError) => void ): void { axiosInstance .delete(`/entrepreneur/sectionCells/${sectionCellId}`) .then((response) => { if (onSuccessHandler) { onSuccessHandler(response); } else { defaultApiSuccessHandler(response); } }) .catch((error: AxiosError) => { if (onErrorHandler) { onErrorHandler(error); } else { defaultApiErrorHandler(error); } }); } export { requestProjectCreation, addSectionCell, modifySectionCell, removeSectionCell, };