saleor-dashboard/src/auth/components/SectionRoute.tsx

41 lines
1 KiB
TypeScript
Raw Normal View History

Use graphql-codegen (#1874) * Use generated hooks in apps * Remove unused files * Use proper types in apps * Use generated hooks in attributes * Use generated hooks in auth module * Use generated hooks in categories * Use generated hooks in channels * Use generated types in collections * Remove legacy types from background tasks * Use generated hooks in customers * Use generated hooks in discounts * Use generated hook in file upload * Use generated types in gift cards * Use generated types in home * Use generated hooks in navigation * Use generated hooks in orders * Use generated hooks in pages * Use generated hooks in page types * Use generated hooks in permission groups * Use generated hooks in plugins * Use generated hooks in products * Use fragment to mark product variants * Improve code a bit * Use generated hooks in page types * Use generated types in searches * Use generated hooks in shipping * Use generated hooks in site settings * Use generated hooks in staff members * Use generated hooks in taxes * Place all gql generated files in one directory * Use generated hooks in translations * Use global types from new generated module * Use generated hooks in warehouses * Use generated hooks in webhooks * Use generated fragment types * Unclutter types * Remove hoc components * Split hooks and types * Fetch introspection file * Delete obsolete schema file * Fix rebase artifacts * Fix autoreplace * Fix auth provider tests * Fix urls * Remove leftover types * Fix rebase artifacts
2022-03-09 08:56:55 +00:00
import { PermissionEnum } from "@saleor/graphql";
2019-08-09 10:26:22 +00:00
import React from "react";
2019-06-19 14:40:52 +00:00
import { Route, RouteProps } from "react-router-dom";
import { useUser } from "..";
2019-06-19 14:40:52 +00:00
import NotFound from "../../NotFound";
import { hasAllPermissions, hasAnyPermissions } from "../misc";
type MatchPermissionType = "all" | "any";
2019-06-19 14:40:52 +00:00
interface SectionRouteProps extends RouteProps {
permissions?: PermissionEnum[];
matchPermission?: MatchPermissionType;
2019-06-19 14:40:52 +00:00
}
const matchAll = (match: MatchPermissionType) => match === "all";
export const SectionRoute: React.FC<SectionRouteProps> = ({
2019-06-19 14:40:52 +00:00
permissions,
matchPermission = "all",
2019-06-19 14:40:52 +00:00
...props
2019-10-21 11:44:13 +00:00
}) => {
const { user } = useUser();
const hasSectionPermissions = () => {
if (!permissions) {
return true;
}
if (matchAll(matchPermission)) {
return hasAllPermissions(permissions, user);
}
return hasAnyPermissions(permissions, user);
};
return hasSectionPermissions() ? <Route {...props} /> : <NotFound />;
2019-10-21 11:44:13 +00:00
};
2019-06-19 14:40:52 +00:00
SectionRoute.displayName = "Route";
export default SectionRoute;