import useSWR from "swr"; import type { SWRConfiguration } from "swr"; import type { AxiosRequestConfig } from "axios"; import { get, patch, post, put, deleteOp } from "@/Utils/ApiClient"; import { useState } from "react"; import { useToast } from "@/Hooks/UseToast"; import { logger } from "@/Utils/logger"; interface ApiResponse { success: boolean; msg: string; data: T; } const fetcher = async (url: string, config?: AxiosRequestConfig) => { const res = await get>(url, config); return res.data; }; export const useGet = ( url: string | null | undefined, axiosConfig?: AxiosRequestConfig, swrConfig?: SWRConfiguration ) => { const { data, error, isLoading, isValidating, mutate } = useSWR>( url, (url: string) => fetcher(url, axiosConfig), swrConfig ); return { data: data?.data ?? null, isLoading, isValidating, error, refetch: mutate, }; }; export const usePost = () => { const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const { toastError, toastSuccess } = useToast(); const postFn = async ( endpoint: string, body: B, config?: AxiosRequestConfig ): Promise | null> => { setLoading(true); setError(null); try { const res = await post>(endpoint, body, { ...config, headers: { ...config?.headers, }, }); toastSuccess(res.data?.msg || "Operation successful"); return res.data; } catch (err: any) { const errMsg = err?.response?.data?.msg || err.message || "An error occurred"; logger.error("POST request failed", err, { endpoint, body }); toastError(errMsg); setError(errMsg); return null; } finally { setLoading(false); } }; return { post: postFn, loading, error }; }; export const usePatch = () => { const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const { toastError, toastSuccess } = useToast(); const patchFn = async ( endpoint: string, body?: B, config?: AxiosRequestConfig ): Promise | null> => { setLoading(true); setError(null); try { const res = await patch>(endpoint, body, { ...config, headers: { ...config?.headers, }, }); toastSuccess(res.data?.msg || "Operation successful"); return res.data; } catch (err: any) { logger.error("PATCH request failed", err, { endpoint, body }); const errMsg = err?.response?.data?.msg || err.message || "An error occurred"; toastError(errMsg); setError(errMsg); return null; } finally { setLoading(false); } }; return { patch: patchFn, loading, error }; }; export const usePut = () => { const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const { toastError, toastSuccess } = useToast(); const putFn = async ( endpoint: string, body?: B, config?: AxiosRequestConfig ): Promise | null> => { setLoading(true); setError(null); try { const res = await put>(endpoint, body, { ...config, headers: { ...config?.headers, }, }); toastSuccess(res.data?.msg || "Operation successful"); return res.data; } catch (err: any) { logger.error("PUT request failed", err, { endpoint, body }); const errMsg = err?.response?.data?.msg || err.message || "An error occurred"; toastError(errMsg); setError(errMsg); return null; } finally { setLoading(false); } }; return { put: putFn, loading, error }; }; export const useDelete = () => { const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const { toastError, toastSuccess } = useToast(); const deleteFn = async ( endpoint: string, config?: AxiosRequestConfig ): Promise | null> => { setLoading(true); setError(null); try { const res = await deleteOp>(endpoint, { ...config, headers: { ...config?.headers, }, }); toastSuccess(res.data?.msg || "Operation successful"); return res.data; } catch (err: any) { logger.error("DELETE request failed", err, { endpoint }); const errMsg = err?.response?.data?.message || err.message || "An error occurred"; toastError(errMsg); setError(errMsg); return null; } finally { setLoading(false); } }; return { deleteFn, loading, error }; }; export const useLazyGet = () => { const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const { toastError } = useToast(); const getFn = async ( endpoint: string, config?: AxiosRequestConfig ): Promise | null> => { setLoading(true); setError(null); try { const res = await get>(endpoint, { ...config, headers: { ...config?.headers, }, }); return res.data; } catch (err: any) { logger.error("GET request failed", err, { endpoint }); const errMsg = err?.response?.data?.msg || err.message || "An error occurred"; toastError(errMsg); setError(errMsg); return null; } finally { setLoading(false); } }; return { get: getFn, loading, error }; };