2023-05-24 19:23:24 +03:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import { useDispatch, useSelector } from "react-redux";
|
2023-05-31 08:36:15 +03:00
|
|
|
import { NavLink, useNavigate } from "react-router-dom";
|
2023-05-24 19:23:24 +03:00
|
|
|
|
2023-11-05 20:42:03 +03:00
|
|
|
import { auth, getProfileInfo, setProfileInfo } from "@redux/outstaffingSlice";
|
2023-05-30 10:10:34 +03:00
|
|
|
import { getRole } from "@redux/roleSlice";
|
2023-05-24 19:23:24 +03:00
|
|
|
|
2023-05-31 08:36:15 +03:00
|
|
|
import { apiRequest } from "@api/request";
|
|
|
|
|
|
|
|
import { Loader } from "@components/Common/Loader/Loader";
|
|
|
|
|
2023-05-24 19:23:24 +03:00
|
|
|
import "./profileHeader.scss";
|
|
|
|
|
|
|
|
export const ProfileHeader = () => {
|
|
|
|
const navigate = useNavigate();
|
|
|
|
const dispatch = useDispatch();
|
2023-11-05 20:41:40 +03:00
|
|
|
const profileInfo = useSelector(getProfileInfo);
|
2023-05-24 19:23:24 +03:00
|
|
|
const userRole = useSelector(getRole);
|
|
|
|
const [user] = useState(
|
2023-12-04 18:01:04 +03:00
|
|
|
localStorage.getItem("role_status") === "18" ? "partner" : "developer",
|
2023-05-24 19:23:24 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
const [isLoggingOut, setIsLoggingOut] = useState(false);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (localStorage.getItem("role_status") === "18") {
|
|
|
|
return;
|
|
|
|
}
|
2023-11-05 20:41:40 +03:00
|
|
|
if (Object.keys(profileInfo).length) {
|
|
|
|
return;
|
|
|
|
}
|
2023-10-27 18:32:37 +03:00
|
|
|
apiRequest(`/user/me`).then((profileInfo) => {
|
2023-11-05 20:42:03 +03:00
|
|
|
dispatch(
|
|
|
|
setProfileInfo(
|
2023-12-04 18:01:04 +03:00
|
|
|
profileInfo.userCard ? profileInfo.userCard : profileInfo,
|
|
|
|
),
|
2023-11-05 20:42:03 +03:00
|
|
|
);
|
2023-10-27 18:32:37 +03:00
|
|
|
});
|
2023-05-24 19:23:24 +03:00
|
|
|
}, [dispatch]);
|
|
|
|
|
|
|
|
const handler = () => {
|
|
|
|
setIsLoggingOut(true);
|
|
|
|
localStorage.clear();
|
|
|
|
dispatch(auth(false));
|
|
|
|
setIsLoggingOut(false);
|
|
|
|
navigate(userRole === "ROLE_DEV" ? "/authdev" : "/auth");
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<header className="profileHeader">
|
|
|
|
<div className="profileHeader__head">
|
|
|
|
<div className="profileHeader__container">
|
|
|
|
<NavLink to={"/profile"} className="profileHeader__title">
|
|
|
|
itguild.
|
|
|
|
<span>
|
|
|
|
{user === "developer" ? "для разработчиков" : "для партнеров"}
|
|
|
|
</span>
|
|
|
|
</NavLink>
|
|
|
|
<button onClick={handler} className="profileHeader__logout">
|
|
|
|
{isLoggingOut ? <Loader /> : "Выйти"}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</header>
|
|
|
|
);
|
|
|
|
};
|