import { useState, useEffect, useRef } from 'react'; import { Box, Button, Typography, CircularProgress } from '@mui/material'; import { useTranslation } from 'react-i18next'; import { CardContainer } from '@/components/CardContainer'; import { ProfileImage } from './personalInformation/ProfileImage'; import { InfoRowDisplay } from './personalInformation/InfoRowDisplay'; import { InfoRowEdit } from './personalInformation/InfoRowEdit'; import { PageWrapper } from '../PageWrapper'; import { useApi } from '@/hooks/useApi'; import { Gender, type InfoRowData } from '../../types/settingsType'; import { fetchProfile, saveProfile } from '../../api/settingsApi'; import { useToast } from '@rkheftan/harmony-ui'; export function PersonalInformation() { const { t } = useTranslation('setting'); const [isEditing, setIsEditing] = useState(false); const [uploadedImageUrl, setUploadedImageUrl] = useState(null); const [uploadedImageFile, setUploadedImageFile] = useState(null); const [data, setData] = useState({ firstName: '', lastName: '', nationalCode: '', gender: Gender.None, country: '', }); const [originalData, setOriginalData] = useState(null); const showToast = useToast(); const infoRowEditRef = useRef<{ validateFields: () => boolean }>(null); const { data: profileData, loading: isLoadingProfile, error: fetchProfileError, } = useApi(fetchProfile, { immediate: true }); const { data: saveData, loading: isSavingProfile, error: saveProfileError, execute: executeSaveProfile, } = useApi(saveProfile); useEffect(() => { if (profileData?.success) { const fetchedData = { firstName: profileData.firstName ?? '', lastName: profileData.lastName ?? '', nationalCode: profileData.nationalCode ?? '', gender: Object.values(Gender).includes(profileData.gender as Gender) ? (profileData.gender as Gender) : Gender.None, country: profileData.countryCode ?? '', }; setData(fetchedData); setOriginalData(fetchedData); const imageBaseUrl = import.meta.env.IMAGE_BASE_URL; setUploadedImageUrl( profileData.profileImageUrl ? `${imageBaseUrl}${profileData.profileImageUrl}` : null, ); setUploadedImageFile(null); } }, [profileData]); useEffect(() => { if (saveData?.success) { showToast({ message: t('settingForm.successSaveProfile'), severity: 'success', }); setIsEditing(false); setOriginalData(data); setUploadedImageFile(null); } }, [saveData, data, showToast, t]); const initials = `${data?.firstName?.trim()[0] || ''}‌${data?.lastName?.trim()[0] || ''}`; const handleEditClick = () => setIsEditing(true); const handleCancelClick = () => { setIsEditing(false); if (originalData) setData(originalData); setUploadedImageFile(null); }; const handleSaveClick = () => { if (!data) return; const isValid = infoRowEditRef.current?.validateFields?.(); if (!isValid) return; executeSaveProfile({ data, imageUrl: uploadedImageFile }); }; const getErrorMessage = (error: unknown): string | null => { if (!error) return null; if (error instanceof Error) return error.message; return String(error); }; useEffect(() => { if (saveProfileError) showToast({ message: getErrorMessage(saveProfileError) || t('settingForm.errorSave'), severity: 'error', }); }, [saveProfileError, showToast, t]); useEffect(() => { if (fetchProfileError) showToast({ message: getErrorMessage(fetchProfileError) || t('settingForm.errorFetch'), severity: 'error', }); }, [fetchProfileError, showToast, t]); return ( {isEditing ? ( <> ) : ( )} {getErrorMessage(saveProfileError) && ( {getErrorMessage(saveProfileError)} )} } > {isLoadingProfile ? ( ) : fetchProfileError ? ( {getErrorMessage(fetchProfileError) || t('settingForm.errorFetch')} ) : ( {isEditing && ( { setUploadedImageFile(file); const reader = new FileReader(); reader.onload = () => setUploadedImageUrl(reader.result as string); reader.readAsDataURL(file); }} onRemoveImage={() => { setUploadedImageFile(null); setUploadedImageUrl(null); }} /> )} {data && (isEditing ? ( ) : ( ))} )} ); }