From c251a598cc7147933b356282159baffeda09f5bf Mon Sep 17 00:00:00 2001 From: ArtChaos189 Date: Fri, 21 Apr 2023 16:08:33 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BB=20Conditio?= =?UTF-8?q?nalRender=20=D0=B4=D0=BB=D1=8F=20=D1=80=D0=B5=D0=BD=D0=B4=D0=B5?= =?UTF-8?q?=D1=80=D0=B0=20=D0=B4=D0=BE=D1=87=D0=B5=D1=80=D0=BD=D0=B8=D1=85?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=BC=D0=BF=D0=BE=D0=BD=D0=B5=D0=BD=D1=82=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=B2=20=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=BC=D0=BE?= =?UTF-8?q?=D1=82=D1=81=D0=B8=20=D0=BE=D1=82=20=D1=80=D0=BE=D0=BB=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/Setting/ConditionalRender.tsx | 46 +++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/pages/Setting/ConditionalRender.tsx diff --git a/src/pages/Setting/ConditionalRender.tsx b/src/pages/Setting/ConditionalRender.tsx new file mode 100644 index 0000000..8ed1537 --- /dev/null +++ b/src/pages/Setting/ConditionalRender.tsx @@ -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(""); + + 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 :
Администратор
; + } + if (role === "user") { + return childrenUser ? childrenUser :
Пользователь
; + } + if (role === "manager") { + return childrenManager ? childrenManager :
Менеджер
; + } + } + + return ; +}; + +export default ConditionalRender;