survey-webapp/SurveyFrontend/src/components/Account/Account.tsx
Tatiana Nikolaeva 1e60984d64 fix results page
2025-06-10 08:42:07 +05:00

46 lines
No EOL
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React, { useEffect, useState } from 'react';
import styles from './Account.module.css';
import AccountImg from '../../assets/account.svg?react';
import { getCurrentUser } from '../../api/AuthApi';
import {handleUnauthorizedError} from "../../api/BaseApi.ts";
interface AccountProps {
href: string;
}
const Account: React.FC<AccountProps> = ({ href }) => {
const [userName, setUserName] = useState<string>();
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
const fetchUserData = async () => {
try {
const userData = await getCurrentUser();
setUserName(`${userData.firstName} ${userData.lastName}`);
} catch (error) {
handleUnauthorizedError(error)
console.error("Ошибка загрузки данных пользователя:", error);
localStorage.removeItem("user");
} finally {
setIsLoading(false);
}
};
fetchUserData();
}, []);
if (isLoading) {
return <div className={styles.account}>Загрузка...</div>;
}
return (
<div className={styles.account}>
<a className={styles.accountText} href={href}>
<AccountImg className={styles.accountImg}/>
{userName}
</a>
</div>
);
};
export default Account;