Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Simplify code

dominik-zeglene07dc7e5530c
diff --git a/saleor/static/dashboard-next/auth/components/SectionRoute.tsx b/saleor/static/dashboard-next/auth/components/SectionRoute.tsx
index 3cec9b4296..7469a7722f 100644
--- a/saleor/static/dashboard-next/auth/components/SectionRoute.tsx
+++ b/saleor/static/dashboard-next/auth/components/SectionRoute.tsx
@@ -4,6 +4,7 @@ import { Route, RouteProps } from "react-router-dom";
import { UserContext } from "..";
import AppRoot from "../../AppRoot";
import NotFound from "../../NotFound";
+import { hasPermission } from "../misc";
interface SectionRouteProps extends RouteProps {
resource?: string;
@@ -15,18 +16,12 @@ export const SectionRoute: React.StatelessComponent<SectionRouteProps> = ({
}) => (
<UserContext.Consumer>
{({ user }) =>
- resource ? (
- user.permissions.map(perm => perm.code).includes(resource) ? (
- <AppRoot>
- <Route {...props} />
- </AppRoot>
- ) : (
- <NotFound />
- )
- ) : (
+ !resource || hasPermission(resource, user) ? (
<AppRoot>
<Route {...props} />
</AppRoot>
+ ) : (
+ <NotFound />
)
}
</UserContext.Consumer>
diff --git a/saleor/static/dashboard-next/auth/misc.ts b/saleor/static/dashboard-next/auth/misc.ts
new file mode 100644
index 0000000000..7e016f5c44
--- /dev/null
+++ b/saleor/static/dashboard-next/auth/misc.ts
@@ -0,0 +1,4 @@
+import { User } from "./types/User";
+
+export const hasPermission = (permission: string, user: User) =>
+ user.permissions.map(perm => perm.code).includes(permission);
diff --git a/saleor/static/dashboard-next/index.tsx b/saleor/static/dashboard-next/index.tsx
index 0a9a224f1b..a553a53cd4 100644
--- a/saleor/static/dashboard-next/index.tsx
+++ b/saleor/static/dashboard-next/index.tsx
@@ -15,6 +15,7 @@ import Auth, { getAuthToken, removeAuthToken } from "./auth";
import AuthProvider from "./auth/AuthProvider";
import LoginLoading from "./auth/components/LoginLoading/LoginLoading";
import SectionRoute from "./auth/components/SectionRoute";
+import { hasPermission } from "./auth/misc";
import CategorySection from "./categories";
import { DateProvider } from "./components/DateFormatter";
import { LocaleProvider } from "./components/Locale";
@@ -123,9 +124,7 @@ render(
component={StaffSection}
/>
{configurationMenu.filter(menuItem =>
- user.permissions
- .map(perm => perm.code)
- .includes(menuItem.resource)
+ hasPermission(menuItem.resource, user)
).length > 0 && (
<SectionRoute
exact