From 4f82a903a970b0702b668fbe084bcf55c3a9e682 Mon Sep 17 00:00:00 2001 From: Georgii Karataev Date: Mon, 19 Aug 2024 12:32:00 +0200 Subject: [PATCH 1/4] refactor(Routes): Remove * wrapping route --- src/Routes.js | 84 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 35 deletions(-) diff --git a/src/Routes.js b/src/Routes.js index 0323df4c..19b533c0 100644 --- a/src/Routes.js +++ b/src/Routes.js @@ -74,7 +74,7 @@ const TemplateDetail = lazy(() => const PatchRoutes = () => { const generalPermissions = ['patch:*:*', 'patch:*:read']; - const [hasSystems, setHasSystems] = useState(true); + const [hasSystems, setHasSystems] = useState(null); const INVENTORY_TOTAL_FETCH_URL = '/api/inventory/v1/hosts'; const RHEL_ONLY_FILTER = '?filter[system_profile][operating_system][RHEL][version][gte]=0'; @@ -90,7 +90,7 @@ const PatchRoutes = () => { } }, [hasSystems]); - return ( + return !hasSystems ? ( @@ -98,40 +98,54 @@ const PatchRoutes = () => { } > - - Error state} - app="Content_management" - customFetchResults={ hasSystems } - > - - }> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - - - - - } /> - - + Error state} + app="Content_management" + customFetchResults={hasSystems} + /> + ) : ( + + + } + > + } /> + } + /> + } + /> + } /> + } + /> + } /> + } + /> + } + /> + } /> + } + /> + } /> + + ); }; From 92b6ad5c73ff3317fd0032040cd2eb97d85af960 Mon Sep 17 00:00:00 2001 From: Georgii Karataev Date: Mon, 19 Aug 2024 12:41:54 +0200 Subject: [PATCH 2/4] chore: Remove deprecated ErrorComponent prop passing No jira. --- src/Routes.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Routes.js b/src/Routes.js index 19b533c0..d73aaa5a 100644 --- a/src/Routes.js +++ b/src/Routes.js @@ -103,7 +103,6 @@ const PatchRoutes = () => { appName="dashboard" module="./AppZeroState" scope="dashboard" - ErrorComponent={
Error state
} app="Content_management" customFetchResults={hasSystems} /> From fed5d4d7dd04ed2ba8bf87af24d11c10d5e9a345 Mon Sep 17 00:00:00 2001 From: Georgii Karataev Date: Thu, 22 Aug 2024 12:10:21 +0200 Subject: [PATCH 3/4] refactor: Replace PermissionRoute with a plain component --- src/AccessWrapper.js | 28 +++++++++++++++ src/App.js | 5 ++- src/Routes.js | 85 ++++++++++++++++---------------------------- 3 files changed, 62 insertions(+), 56 deletions(-) create mode 100644 src/AccessWrapper.js diff --git a/src/AccessWrapper.js b/src/AccessWrapper.js new file mode 100644 index 00000000..3ad549f3 --- /dev/null +++ b/src/AccessWrapper.js @@ -0,0 +1,28 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import { Bullseye, Spinner } from '@patternfly/react-core'; +import { NotAuthorized } from '@redhat-cloud-services/frontend-components'; +import { usePermissionsWithContext } from '@redhat-cloud-services/frontend-components-utilities/RBACHook'; + +const AccessWrapper = ({ children }) => { + const { hasAccess, isLoading } = usePermissionsWithContext([ + 'patch:*:*', + 'patch:*:read' + ]); + + return isLoading ? ( + + + + ) : hasAccess ? ( + children + ) : ( + + ); +}; + +AccessWrapper.propTypes = { + children: PropTypes.any +}; + +export default AccessWrapper; diff --git a/src/App.js b/src/App.js index ed5489b3..2a7bafec 100644 --- a/src/App.js +++ b/src/App.js @@ -8,6 +8,7 @@ import { changeGlobalTags, changeProfile, globalFilter } from './store/Actions/A import { mapGlobalFilters } from './Utilities/Helpers'; import './App.scss'; import Routes from './Routes'; +import AccessWrapper from './AccessWrapper'; const App = () => { const dispatch = useDispatch(); @@ -46,7 +47,9 @@ const App = () => { - + + + ); diff --git a/src/Routes.js b/src/Routes.js index d73aaa5a..3039cf19 100644 --- a/src/Routes.js +++ b/src/Routes.js @@ -1,29 +1,10 @@ import { Bullseye, Spinner } from '@patternfly/react-core'; -import { NotAuthorized } from '@redhat-cloud-services/frontend-components/NotAuthorized'; -import { usePermissionsWithContext } from '@redhat-cloud-services/frontend-components-utilities/RBACHook'; import AsyncComponent from '@redhat-cloud-services/frontend-components/AsyncComponent'; import axios from 'axios'; -import PropTypes from 'prop-types'; import React, { lazy, Suspense, useEffect, useState } from 'react'; -import { Navigate, Outlet, Route, Routes } from 'react-router-dom'; +import { Navigate, Route, Routes } from 'react-router-dom'; import { NavigateToSystem } from './Utilities/NavigateToSystem'; -const PermissionRoute = ({ requiredPermissions = [] }) => { - const { hasAccess, isLoading } = usePermissionsWithContext(requiredPermissions); - if (!isLoading) { - return hasAccess ? : ; - } else { - return ''; - } -}; - -PermissionRoute.propTypes = { - requiredPermissions: PropTypes.oneOfType([ - PropTypes.arrayOf(PropTypes.string), - PropTypes.string - ]) -}; - const Advisories = lazy(() => import( /* webpackChunkName: "Advisories" */ './SmartComponents/Advisories/Advisories' @@ -73,8 +54,8 @@ const TemplateDetail = lazy(() => ); const PatchRoutes = () => { - const generalPermissions = ['patch:*:*', 'patch:*:read']; const [hasSystems, setHasSystems] = useState(null); + const INVENTORY_TOTAL_FETCH_URL = '/api/inventory/v1/hosts'; const RHEL_ONLY_FILTER = '?filter[system_profile][operating_system][RHEL][version][gte]=0'; @@ -109,41 +90,35 @@ const PatchRoutes = () => { ) : ( + } /> + } + /> + } + /> + } /> + } + /> + } /> + } + /> + } + /> + } /> - } - > - } /> - } - /> - } - /> - } /> - } - /> - } /> - } - /> - } - /> - } /> - } - /> - } /> - + path="/templates/:templateName" + element={} + /> + } /> ); }; From c7be848d351860055f620adbdb0c174e381cff81 Mon Sep 17 00:00:00 2001 From: Georgii Karataev Date: Thu, 29 Aug 2024 11:59:37 +0200 Subject: [PATCH 4/4] refactor: Use relative paths for routes at Routes.js. --- src/Routes.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Routes.js b/src/Routes.js index 3039cf19..942e4668 100644 --- a/src/Routes.js +++ b/src/Routes.js @@ -90,32 +90,32 @@ const PatchRoutes = () => { ) : ( - } /> + } /> } /> } /> - } /> + } /> } /> - } /> + } /> } /> } /> - } /> + } /> } /> } />