Files
hats-app/lib/axios/useFetch.tsx
T
Ichsan Kurniawan 7d18e9f38f Add CreateAttendance and update UI/hooks
Add a new CreateAttendance component to allow admins to create manual attendance (Alpha) with confirmation dialog and server API call. Lazy-load the CreateAttendance component in EmployeeAlpha and wrap usages with Suspense plus a loading fallback to reduce initial bundle size. Update Performance page to use a centered Suspense fallback. Improve ImportExcel UX by showing a full-loading state, tweaking button/icon styles, and fixing modal sizing/scroll behavior. Enhance useFetchWithAxios to accept nullable URLs and SWR options, adjust the fetcher signature, and return the nested data payload. Add a "use server" directive and imports in lib/server-api, and apply small type/formatting tweaks (table size cast and minor UI/text refinements).
2026-02-22 23:33:15 +07:00

43 lines
1.2 KiB
TypeScript

import useSWR, { type SWRConfiguration } from 'swr'
import { useSession } from 'next-auth/react';
import { type ResponseData } from '@/types/types';
import useAxiosAuth from './useAxiosAuth';
export default function useFetch(endpoint: string) {
const BASE_URL = process.env.BASE_URL_API
const { data: session } = useSession()
const fetcher = (endpoint: string) => fetch(BASE_URL + endpoint, {
headers: {
Authorization: `Bearer ${session?.user.access_token}`,
}
}).then(res => res.json())
const { data, error, isLoading, mutate } = useSWR<ResponseData<any>>(endpoint, fetcher)
return {
data: data?.data,
isLoading,
error,
mutate
}
}
export function useFetchWithAxios(url: string | null, options?: SWRConfiguration) {
const axios = useAxiosAuth();
// Only define fetcher if url is present
const fetcher = (url: string) => axios.get(url).then(res => res.data)
// Pass url directly. If it is null, SWR will pause.
const { data, error, isLoading, mutate } = useSWR<ResponseData<any>>(url, fetcher, options)
return {
data: data?.data,
isLoading,
error,
mutate
}
}