import React, { useState, createContext, useEffect, useContext, useCallback, useMemo, } from "react"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { useNavigate } from "react-router-dom"; import { useDispatch } from "react-redux"; // ------ Components ------- import Breadcrumb from "../../components/common/Breadcrumb"; import TenantsList from "../../components/Tenant/TenantsList"; import TenantFilterPanel from "../../components/Tenant/TenantFilterPanel"; // ------ Context & Utils ------- import { useDebounce } from "../../utils/appUtils"; import { useFab } from "../../Context/FabContext"; import { setCurrentTenant } from "../../slices/globalVariablesSlice"; import { hasUserPermission } from "../../utils/authUtils"; // ------ Schema ------- import { defaultFilterValues, filterSchema, } from "../../components/Tenant/TenantSchema"; // ------ Constants ------- import { MANAGE_TENANTS, SUPPER_TENANT, VIEW_TENANTS, } from "../../utils/constants"; import { useProfile } from "../../hooks/useProfile"; // ---------- Context ---------- export const TenantContext = createContext(); export const useTenantContext = () => { const context = useContext(TenantContext); if (!context) { throw new Error( "useTenantContext must be used within a TenantContext.Provider" ); } return context; }; const TenantPage = () => { const dispatch = useDispatch(); const navigate = useNavigate(); const { profile } = useProfile(); // ---------- State ---------- const [searchText, setSearchText] = useState(""); const [isRefetching, setIsRefetching] = useState(false); const [refetchFn, setRefetchFn] = useState(null); const [filters, setFilters] = useState(); // ---------- Hooks ---------- const debouncedSearch = useDebounce(searchText, 500); const { setOffcanvasContent, setShowTrigger } = useFab(); const isSuperTenant = hasUserPermission(SUPPER_TENANT); const canManageTenants = hasUserPermission(MANAGE_TENANTS); const isSelfTenant = hasUserPermission(VIEW_TENANTS); const methods = useForm({ resolver: zodResolver(filterSchema), defaultValues: defaultFilterValues, }); const { reset } = methods; const handleApplyFilters = useCallback((values) => { setFilters(values); }, []); const filterPanelElement = useMemo( () => , [handleApplyFilters] ); console.log(isSelfTenant) // ---------- Fab Filter Panel ---------- useEffect(() => { if (!isSuperTenant) return; setShowTrigger(true); setOffcanvasContent("Tenant Filters", filterPanelElement); return () => { setShowTrigger(false); setOffcanvasContent("", null); }; }, [isSuperTenant, filterPanelElement, profile]); // ---------- Redirect for Self Tenant ---------- useEffect(() => { if (!isSuperTenant && isSelfTenant) { // Delay navigation to next tick to avoid "update during render" warning setTimeout(() => { navigate("/tenant/self"); }, 0); } }, [isSuperTenant, isSelfTenant, navigate]); // ---------- Handlers ---------- const handleNewTenant = () => { dispatch(setCurrentTenant(null)); navigate("/tenants/new-tenant"); }; // ---------- Context Value ---------- const contextValue = {}; return ( {/* Super Tenant Actions */} {isSuperTenant && ( {/* Search */} setSearchText(e.target.value)} className="form-control form-control-sm" placeholder="Search Tenant" /> {/* Actions */} refetchFn && refetchFn()} > Refresh{" "} )} {/* Tenant List or Access Denied */} {isSuperTenant ? ( ) : !isSelfTenant ? ( Access Denied: You don't have permission to perform this action! ) : null} ); }; export default TenantPage;
Access Denied: You don't have permission to perform this action!