Создал ConditionalRender для рендера дочерних компонентов в зависмотси от роли
This commit is contained in:
parent
8eab3d22ab
commit
c251a598cc
46
src/pages/Setting/ConditionalRender.tsx
Normal file
46
src/pages/Setting/ConditionalRender.tsx
Normal file
@ -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;
|
Loading…
Reference in New Issue
Block a user