Hide license route based on install and add useConfig hook

This commit is contained in:
Marliana Lara 2020-07-29 13:47:42 -04:00
parent c24e169bf6
commit 42158dea59
No known key found for this signature in database
GPG Key ID: 38C73B40DFA809EE
3 changed files with 32 additions and 10 deletions

View File

@ -1,7 +1,8 @@
import React from 'react';
import React, { useContext } from 'react';
// eslint-disable-next-line import/prefer-default-export
export const ConfigContext = React.createContext({});
export const ConfigProvider = ConfigContext.Provider;
export const Config = ConfigContext.Consumer;
export const useConfig = () => useContext(ConfigContext);

View File

@ -11,17 +11,19 @@ import {
DataListCell,
DataListItemCells,
DataListItemRow,
PageSection as _PageSection,
PageSection,
} from '@patternfly/react-core';
import styled from 'styled-components';
import { BrandName } from '../../variables';
import { useConfig } from '../../contexts/Config';
import ContentLoading from '../../components/ContentLoading/ContentLoading';
// Setting BrandName to a variable here is necessary to get the jest tests
// passing. Attempting to use BrandName in the template literal results
// in failing tests.
const brandName = BrandName;
const PageSection = styled(_PageSection)`
const SplitLayout = styled(PageSection)`
column-count: 1;
column-gap: 24px;
@media (min-width: 576px) {
@ -47,11 +49,12 @@ const CardDescription = styled.div`
`;
function SettingList({ i18n }) {
const config = useConfig();
const settingRoutes = [
{
header: i18n._(t`Authentication`),
description: i18n._(
t`Enable simplified login for your Tower applications`
t`Enable simplified login for your ${brandName} applications`
),
id: 'authentication',
routes: [
@ -87,7 +90,9 @@ function SettingList({ i18n }) {
},
{
header: i18n._(t`Jobs`),
description: i18n._(t`Update settings pertaining to Jobs within Tower`),
description: i18n._(
t`Update settings pertaining to Jobs within ${brandName}`
),
id: 'jobs',
routes: [
{
@ -141,10 +146,20 @@ function SettingList({ i18n }) {
},
];
if (Object.keys(config).length === 0) {
return (
<PageSection>
<Card>
<ContentLoading />
</Card>
</PageSection>
);
}
return (
<PageSection>
<SplitLayout>
{settingRoutes.map(({ description, header, id, routes }) => {
if (id === 'license' && brandName === 'Tower') {
if (id === 'license' && config?.license_info?.license_type === 'open') {
return null;
}
return (
@ -171,7 +186,7 @@ function SettingList({ i18n }) {
</Card>
);
})}
</PageSection>
</SplitLayout>
);
}

View File

@ -1,5 +1,5 @@
import React from 'react';
import { Link, Route, Switch } from 'react-router-dom';
import { Link, Route, Switch, Redirect } from 'react-router-dom';
import { withI18n } from '@lingui/react';
import { t } from '@lingui/macro';
import { PageSection, Card } from '@patternfly/react-core';
@ -19,8 +19,10 @@ import SAML from './SAML';
import SettingList from './SettingList';
import TACACS from './TACACS';
import UI from './UI';
import { useConfig } from '../../contexts/Config';
function Settings({ i18n }) {
const { license_info = {} } = useConfig();
const breadcrumbConfig = {
'/settings': i18n._(t`Settings`),
'/settings/activity_stream': i18n._(t`Activity stream`),
@ -61,7 +63,11 @@ function Settings({ i18n }) {
<LDAP />
</Route>
<Route path="/settings/license">
<License />
{license_info?.license_type === 'open' ? (
<License />
) : (
<Redirect to="/settings" />
)}
</Route>
<Route path="/settings/logging">
<Logging />