import { forwardRef, useImperativeHandle, useEffect, useState, useRef, useCallback } from 'react' import { useTranslation } from 'react-i18next' import { useParams } from 'react-router-dom' import { Skeleton } from 'antd'; import dayjs from 'dayjs' import RbCard from '@/components/RbCard/Card' import { getEndUserProfile, } from '@/api/memory' import EndUserProfileModal from './EndUserProfileModal' import type { EndUser, EndUserProfileModalRef, EndUserProfileRef } from '../types' interface EndUserProfileProps { onDataLoaded?: (data: { other_name?: string; id: string }) => void } const EndUserProfile = forwardRef(({ onDataLoaded }, ref) => { const { t } = useTranslation() const { id } = useParams() const endUserProfileModalRef = useRef(null) const [loading, setLoading] = useState(false) const [data, setData] = useState(null) useEffect(() => { if (!id) return getData() }, [id]) const getData = () => { if (!id) return setLoading(true) getEndUserProfile(id).then((res) => { const userData = res as EndUser setData(userData) onDataLoaded?.({ other_name: userData.other_name, id: userData.id }) setLoading(false) }) .finally(() => { setLoading(false) }) } const formatItems = useCallback(() => { return ['other_name', 'position', 'department', 'contact', 'phone', 'hire_date'].map(key => ({ key, label: t(`userMemory.${key}`), children: key === 'hire_date' && data?.[key] ? dayjs(data[key as keyof EndUser]).format('YYYY-MM-DD') : String(data?.[key as keyof EndUser] || '-'), })) }, [data]) const handleEdit = () => { if (!data) return endUserProfileModalRef.current?.handleOpen(data) } useImperativeHandle(ref, () => ({ data })); return ( } > {loading ? :
{formatItems().map(vo => (
{vo.label}
{vo.children}
))}
{t('userMemory.updated_at')}: {data?.updatetime_profile ? dayjs(data?.updatetime_profile).format('YYYY/MM/DD HH:mm:ss') : ''}
}
) }) export default EndUserProfile