mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:56:28 +00:00
32aab111dd
Introduce useFetchPOSTWithAxios (SWR + axios POST) and use it in ApprovalLeaveSick to fetch TLs (position_code P02). Map cc IDs to fullname entries, store them in state (mentionsTo) and render linked mention badges to /performance/:id instead of raw IDs. Also format birthday as 'YYYY-MM-DD' in EditProfile form, comment out a dashboard console.log, and apply small JSX/spacing cleanups.
63 lines
1.7 KiB
TypeScript
63 lines
1.7 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
|
|
}
|
|
}
|
|
|
|
export function useFetchPOSTWithAxios(endpointKey: [string, ...any[]] | null, payload: any = {}, options?: SWRConfiguration) {
|
|
const axios = useAxiosAuth();
|
|
|
|
const fetcher = ([url, ..._]: [string, ...any[]]) =>
|
|
axios.post(url, payload).then(res => res.data);
|
|
|
|
const { data, error, isLoading, mutate } = useSWR<any>(
|
|
endpointKey, // Keep it as an array for SWR to track
|
|
fetcher,
|
|
options
|
|
);
|
|
|
|
return {
|
|
data: data?.data,
|
|
isLoading,
|
|
error,
|
|
mutate
|
|
};
|
|
} |