From c7775ade8c33c498204a96f74750fa1448161d5f Mon Sep 17 00:00:00 2001 From: Eduard Gert Date: Wed, 20 Nov 2024 17:50:28 +0100 Subject: [PATCH] Hide groups for regular users (#423) --- src/app/(dashboard)/peer/page.tsx | 33 ++++++------------------- src/contexts/GroupsProvider.tsx | 24 ++++++++++++------ src/modules/peers/PeersTable.tsx | 41 +++++++++++++++++-------------- 3 files changed, 47 insertions(+), 51 deletions(-) diff --git a/src/app/(dashboard)/peer/page.tsx b/src/app/(dashboard)/peer/page.tsx index 3738cf42..250a4890 100644 --- a/src/app/(dashboard)/peer/page.tsx +++ b/src/app/(dashboard)/peer/page.tsx @@ -297,29 +297,12 @@ function PeerOverview() { /> -
- - - Use groups to control what this peer can access. - - - - - {`You don't have the required permissions to update this - setting.`} - -
- } - interactive={false} - className={"w-full block"} - disabled={!isUser} - > + {!isUser && ( +
+ + + Use groups to control what this peer can access. + - -
+ + )} diff --git a/src/contexts/GroupsProvider.tsx b/src/contexts/GroupsProvider.tsx index 7fd3fe77..7346dd93 100644 --- a/src/contexts/GroupsProvider.tsx +++ b/src/contexts/GroupsProvider.tsx @@ -1,6 +1,5 @@ import useFetchApi, { useApiCall } from "@utils/api"; import { merge, sortBy, unionBy } from "lodash"; -import { usePathname } from "next/navigation"; import React, { useEffect, useState } from "react"; import { useLoggedInUser } from "@/contexts/UsersProvider"; import { Group } from "@/interfaces/Group"; @@ -25,18 +24,29 @@ const GroupContext = React.createContext( ); export default function GroupsProvider({ children }: Props) { - const path = usePathname(); - const { permission } = useLoggedInUser(); + const { permission, isUser } = useLoggedInUser(); - return path === "/peers" && permission.dashboard_view == "blocked" ? ( + return permission.dashboard_view == "blocked" ? ( <>{children} ) : ( - {children} + {children} ); } -export function GroupsProviderContent({ children }: Props) { - const { data: groups, mutate, isLoading } = useFetchApi("/groups"); +type ProviderContentProps = { + children: React.ReactNode; + isUser: boolean; +}; + +export function GroupsProviderContent({ + children, + isUser, +}: Readonly) { + const { + data: groups, + mutate, + isLoading, + } = useFetchApi("/groups", false, true, !isUser); const groupRequest = useApiCall("/groups", true); const [dropdownOptions, setDropdownOptions] = useState([]); diff --git a/src/modules/peers/PeersTable.tsx b/src/modules/peers/PeersTable.tsx index e0b517d9..3f98c381 100644 --- a/src/modules/peers/PeersTable.tsx +++ b/src/modules/peers/PeersTable.tsx @@ -246,6 +246,7 @@ export default function PeersTable({ peers, isLoading, headingTarget }: Props) { user_name: false, user_email: false, actions: !isUser, + groups: !isUser, }} isLoading={isLoading} getStartedCard={ @@ -423,30 +424,32 @@ export default function PeersTable({ peers, isLoading, headingTarget }: Props) { - { - table.setPageIndex(0); - if (groups.length == 0) { - table.getColumn("group_names")?.setFilterValue(undefined); - return; - } else { - table.getColumn("group_names")?.setFilterValue(groups); + {!isUser && ( + + onChange={(groups) => { + table.setPageIndex(0); + if (groups.length == 0) { + table.getColumn("group_names")?.setFilterValue(undefined); + return; + } else { + table.getColumn("group_names")?.setFilterValue(groups); + } + resetSelectedRows(); + }} + groups={tableGroups} + /> + )} { - mutate("/groups").then(); + if (!isUser) mutate("/groups").then(); mutate("/users").then(); mutate("/peers").then(); }}