Создал ConditionalRender для рендера дочерних компонентов в зависмотси от роли

This commit is contained in:
ArtChaos189 2023-04-21 16:08:33 +03:00
parent 8eab3d22ab
commit c251a598cc

@ -0,0 +1,46 @@
import axios from "axios";
import React, { useEffect, useState } from "react";
type ConditionalRenderProps = {
isLoading: boolean;
childrenUser?: JSX.Element;
childrenAdmin?: JSX.Element;
childrenManager?: JSX.Element;
};
const ConditionalRender = ({
isLoading,
childrenUser,
childrenAdmin,
childrenManager,
}: ConditionalRenderProps): JSX.Element => {
const [role, setRole] = useState<string>("");
useEffect(() => {
const axiosAccount = async () => {
try {
const { data } = await axios.get("https://admin.pena.digital/user/643e23f3dba63ba17272664d");
setRole(data.role);
} catch (error) {
console.error("Ошибка при получение роли пользавателя");
}
};
axiosAccount();
}, [role]);
if (isLoading) {
if (role === "admin") {
return childrenAdmin ? childrenAdmin : <div>Администратор</div>;
}
if (role === "user") {
return childrenUser ? childrenUser : <div>Пользователь</div>;
}
if (role === "manager") {
return childrenManager ? childrenManager : <div>Менеджер</div>;
}
}
return <React.Fragment />;
};
export default ConditionalRender;