"use client"; import AccountDetails from "@/components/user-profile/AccountDetails"; import UserInfoCard from "@/components/user-profile/UserInfoCard"; import UserMetaCard from "@/components/user-profile/UserMetaCard"; import { UserMetaCardProps } from "@/interface/user"; import { apiGetCurrentUser } from "@/service/auth"; import { setUserData } from "@/store/features/userSlice"; import { useEffect, useState } from "react"; import { useDispatch, useSelector } from "react-redux"; import { RootState } from "@/store/store"; import StickyHeader from "@/components/ui/StickyHeader"; import { SafeHTMLRenderer } from "@/components/ui/parse/SafeHTMLRenderer"; import { apiGetCurrentUserApplications } from "@/service/userService"; import Loading from "@/app/loading"; export default function Profile() { const currentUser = useSelector((state: RootState) => state.user.data); const dispatch = useDispatch(); const [application, setApplication] = useState(null); const [appLoading, setAppLoading] = useState(false); const isHistorian = !!currentUser?.roles?.some( (role: any) => role.name === "HISTORIAN" ); // Background refresh of user data to ensure eventual consistency useEffect(() => { const fetchUser = async () => { try { const userData = await apiGetCurrentUser(); dispatch(setUserData(userData.data)); } catch (err) { console.error("Lỗi:", err); } }; fetchUser(); }, [dispatch]); // Fetch applications in parallel immediately if user is a historian useEffect(() => { if (isHistorian) { const fetchApp = async () => { try { setAppLoading(true); const res = await apiGetCurrentUserApplications(); if (res?.data) { const approvedApp = res.data.find((app: any) => app.status === "APPROVED") || res.data[0]; setApplication(approvedApp); } } catch (err) { console.error("Lỗi khi tải hồ sơ nhà sử học:", err); } finally { setAppLoading(false); } }; fetchApp(); } }, [isHistorian]); if (!currentUser) { return ; } const userMetaProps: UserMetaCardProps = { data: currentUser ? { id: currentUser.id, email: currentUser.email, profile: currentUser.profile, roles: currentUser.roles?.map((role) => ({ id: Number(role.id) || undefined, name: role.name, })), } : undefined, status: true, }; // Nếu người dùng có role là HISTORIAN if (isHistorian) { return (
{appLoading ? (
) : application ? (
) : (
Không tìm thấy thông tin hồ sơ nhà sử học.
)}
); } return (

Thông tin tài khoản

); }