Add POST fetch hook and render mention links

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.
This commit is contained in:
Ichsan Kurniawan
2026-03-06 10:51:45 +07:00
parent 60648f312d
commit 32aab111dd
4 changed files with 51 additions and 5 deletions
+1
View File
@@ -42,6 +42,7 @@ export default async function DashboardPage() {
const alreadyCheckin = todayPresence.history.length > 0
const alreadyCheckout = (alreadyCheckin && todayPresence.history[0].presence_checkout) || todayPresence.history.length === 0
// console.log(dataDashboard)
return (
<>
<div className="flex-1 space-y-8 p-6 sm:p-8 pt-3 sm:pt-7 pb-8">
@@ -15,6 +15,8 @@ import PageLoading from '@/components/loader/PageLoading';
import { useRouter } from 'next/navigation';
import { toast } from 'sonner';
import FallbackAvatar from '@/components/loader/FallbackAvatar';
import { useFetchPOSTWithAxios } from '@/lib/axios/useFetch';
import Link from 'next/link';
const ViewAvatar = lazy(() => import('@/components/ViewAvatar'));
const Avatar = lazy(() => import('@/components/ViewAvatar').then(mod => ({ default: mod.Avatar })));
@@ -34,6 +36,7 @@ const ApprovalLeaveSick: FC<Props> = ({ data, className }) => {
const [showConfirm, setShowConfirm] = useState(false)
const [isApproval, setIsApproval] = useState(false)
const [dataPresence, setDataPresence] = useState<any>(null)
const [mentionsTo, setMentionsTo] = useState([])
const [presenceId, setPresenceId] = useState(0)
const [dataFilter, setDataFilter] = useState<any[]>([])
const [search, setSearch] = useState('')
@@ -42,6 +45,8 @@ const ApprovalLeaveSick: FC<Props> = ({ data, className }) => {
const axios = useAxiosAuth()
const router = useRouter()
const { data: TLList } = useFetchPOSTWithAxios(['/user/position'], { position_code: 'P02' })
useEffect(() => {
let newData = [...data.list]
if (!search) {
@@ -71,6 +76,19 @@ const ApprovalLeaveSick: FC<Props> = ({ data, className }) => {
const { data: res } = await axios.get('/application-form/approval/info/' + presenceId)
// console.log(res.data)
setDataPresence(res.data)
const dataTL = TLList || []
const nameDictionary = dataTL.reduce((acc: any, user: any) => {
acc[user.id] = user.fullname;
return acc;
}, {});
const result = res.data.cc.map((id: any) => ({
id: id,
fullname: nameDictionary[id] ?? null
}));
setMentionsTo(result)
setShowDialog(true)
} catch (error: any) {
toast.error(`Fail to fetch presence`, {
@@ -102,7 +120,7 @@ const ApprovalLeaveSick: FC<Props> = ({ data, className }) => {
}
}
console.log(dataPresence)
// console.log(TLList, dataPresence)
// console.log(dataFilter)
return (
<>
@@ -220,7 +238,13 @@ const ApprovalLeaveSick: FC<Props> = ({ data, className }) => {
</div>
<div>
<p className='text-muted-foreground text-xs md:text-sm font-medium'>Mentions to</p>
<p>{dataPresence.cc.length > 0 ? dataPresence.cc.join(', ') : '-'}</p>
{dataPresence.cc && dataPresence.cc.length > 0 ?
mentionsTo.map((item: any, key: number) =>
<Link href={`/performance/${item?.id}`} key={key} className='text-blue-500 text-xs md:text-sm hover:text-sky-800 duration-200 mr-1.5'>@{item?.fullname || item?.id}</Link>
)
:
'-'
}
</div>
</div>
</div>
@@ -236,9 +260,10 @@ const ApprovalLeaveSick: FC<Props> = ({ data, className }) => {
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</AlertDialog >
}
{showConfirm &&
{
showConfirm &&
<AlertDialog open={true}>
<AlertDialogContent>
<AlertDialogHeader>
@@ -36,7 +36,7 @@ const EditProfile: FC<Props> = ({ data }) => {
useEffect(() => {
if (data) {
form.setFieldsValue(data)
if (data.birthday) form.setFieldValue('birthday', dayjs(data.birthday))
if (data.birthday) form.setFieldValue('birthday', dayjs(data.birthday).format('YYYY-MM-DD'))
} else {
form.resetFields()
}
+20
View File
@@ -40,4 +40,24 @@ export function useFetchWithAxios(url: string | null, options?: SWRConfiguration
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
};
}