Skip to content

Commit

Permalink
Pass config to context as an object
Browse files Browse the repository at this point in the history
  • Loading branch information
marshmalien committed Apr 5, 2021
1 parent 9c39b93 commit 0e4d0c3
Show file tree
Hide file tree
Showing 44 changed files with 71 additions and 72 deletions.
7 changes: 4 additions & 3 deletions awx/ui_next/src/components/AppContainer/AppContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ function useStorage(key) {

function AppContainer({ i18n, navRouteConfig = [], children }) {
const history = useHistory();
const [config] = useConfig();
const config = useConfig();

const isReady = !!config.license_info;
const isSidebarVisible = useAuthorizedPath();
const [isAboutModalOpen, setIsAboutModalOpen] = useState(false);

Expand Down Expand Up @@ -153,7 +154,7 @@ function AppContainer({ i18n, navRouteConfig = [], children }) {
/>
);

const simpleHeader = (
const simpleHeader = config.isLoading ? null : (
<PageHeader
logo={<BrandLogo />}
headerTools={
Expand Down Expand Up @@ -197,7 +198,7 @@ function AppContainer({ i18n, navRouteConfig = [], children }) {
header={isSidebarVisible ? header : simpleHeader}
sidebar={isSidebarVisible && sidebar}
>
{Object.keys(config).length ? children : null}
{isReady ? children : null}
</Page>
<About
version={config?.version}
Expand Down
18 changes: 3 additions & 15 deletions awx/ui_next/src/components/AppContainer/AppContainer.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ jest.mock('../../api');

describe('<AppContainer />', () => {
const version = '222';
const config = {
ansible_version,
version,
};

beforeEach(() => {
ConfigAPI.read.mockResolvedValue({
Expand Down Expand Up @@ -56,10 +52,7 @@ describe('<AppContainer />', () => {
{routeConfig.map(({ groupId }) => (
<div key={groupId} id={groupId} />
))}
</AppContainer>,
{
context: { config },
}
</AppContainer>
);
});
wrapper.update();
Expand Down Expand Up @@ -88,7 +81,7 @@ describe('<AppContainer />', () => {
let wrapper;
await act(async () => {
wrapper = mountWithContexts(<AppContainer />, {
context: { config },
context: { config: { version } },
});
});

Expand Down Expand Up @@ -116,12 +109,7 @@ describe('<AppContainer />', () => {

let wrapper;
await act(async () => {
wrapper = mountWithContexts(<AppContainer />, {
context: {
ansible_version,
version,
},
});
wrapper = mountWithContexts(<AppContainer />);
});

// open the user menu
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function InstanceToggle({
onToggle,
i18n,
}) {
const [{ me = {} }] = useConfig();
const { me = {} } = useConfig();
const [isEnabled, setIsEnabled] = useState(instance.enabled);
const [showError, setShowError] = useState(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function PromptProjectDetail({ i18n, resource }) {
value={`${scm_update_cache_timeout} ${i18n._(t`Seconds`)}`}
/>
<Config>
{([{ project_base_dir }]) => (
{({ project_base_dir }) => (
<Detail
label={i18n._(t`Project Base Path`)}
value={project_base_dir}
Expand Down
14 changes: 10 additions & 4 deletions awx/ui_next/src/contexts/Config.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useContext, useEffect } from 'react';
import React, { useCallback, useContext, useEffect, useMemo } from 'react';
import { useLocation, useRouteMatch } from 'react-router-dom';
import { withI18n } from '@lingui/react';
import { t } from '@lingui/macro';
Expand Down Expand Up @@ -26,6 +26,7 @@ export const ConfigProvider = withI18n()(({ i18n, children }) => {

const {
error: configError,
isLoading,
request,
result: config,
setValue: setConfig,
Expand Down Expand Up @@ -58,8 +59,14 @@ export const ConfigProvider = withI18n()(({ i18n, children }) => {
}
}, [error]);

const value = useMemo(() => ({ ...config, isLoading, setConfig }), [
config,
isLoading,
setConfig,
]);

return (
<ConfigContext.Provider value={[config, setConfig]}>
<ConfigContext.Provider value={value}>
{error && (
<AlertModal
isOpen={error}
Expand All @@ -78,10 +85,9 @@ export const ConfigProvider = withI18n()(({ i18n, children }) => {
});

export const useAuthorizedPath = () => {
const [config] = useConfig();
const config = useConfig();
const subscriptionMgmtRoute = useRouteMatch({
path: '/subscription_management',
});

return !!config.license_info?.valid_key && !subscriptionMgmtRoute;
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useConfig } from '../../../contexts/Config';
function CredentialEdit({ credential }) {
const history = useHistory();
const { id: credId } = useParams();
const [{ me = {} }] = useConfig();
const { me = {} } = useConfig();
const [isOrgLookupDisabled, setIsOrgLookupDisabled] = useState(false);

const { error: submitError, request: submitRequest, result } = useRequest(
Expand Down
2 changes: 1 addition & 1 deletion awx/ui_next/src/screens/Credential/Credentials.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function Credentials({ i18n }) {
/>
<Switch>
<Route path="/credentials/add">
<Config>{([{ me }]) => <CredentialAdd me={me || {}} />}</Config>
<Config>{({ me }) => <CredentialAdd me={me || {}} />}</Config>
</Route>
<Route path="/credentials/:id">
<Credential setBreadcrumb={buildBreadcrumbConfig} />
Expand Down
2 changes: 1 addition & 1 deletion awx/ui_next/src/screens/Host/Hosts.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function Hosts({ i18n }) {
</Route>
<Route path="/hosts/:id">
<Config>
{([{ me }]) => (
{({ me }) => (
<Host setBreadcrumb={buildBreadcrumbConfig} me={me || {}} />
)}
</Config>
Expand Down
2 changes: 1 addition & 1 deletion awx/ui_next/src/screens/Inventory/Inventories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function Inventories({ i18n }) {
</Route>
<Route path="/inventories/inventory/:id">
<Config>
{([{ me }]) => (
{({ me }) => (
<Inventory setBreadcrumb={setBreadcrumbConfig} me={me || {}} />
)}
</Config>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function InventorySources({ inventory, setBreadcrumb }) {
</Route>
<Route path="/inventories/inventory/:id/sources/:sourceId">
<Config>
{([{ me }]) => (
{({ me }) => (
<InventorySource
inventory={inventory}
setBreadcrumb={setBreadcrumb}
Expand Down
2 changes: 1 addition & 1 deletion awx/ui_next/src/screens/Organization/Organizations.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function Organizations({ i18n }) {
</Route>
<Route path={`${match.path}/:id`}>
<Config>
{([{ me }]) => (
{({ me }) => (
<Organization setBreadcrumb={setBreadcrumb} me={me || {}} />
)}
</Config>
Expand Down
2 changes: 1 addition & 1 deletion awx/ui_next/src/screens/Project/Project.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import ProjectJobTemplatesList from './ProjectJobTemplatesList';
import { OrganizationsAPI, ProjectsAPI } from '../../api';

function Project({ i18n, setBreadcrumb }) {
const [{ me = {} }] = useConfig();
const { me = {} } = useConfig();
const { id } = useParams();
const location = useLocation();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function ProjectDetail({ project, i18n }) {
isDefaultEnvironment
/>
<Config>
{([{ project_base_dir }]) => (
{({ project_base_dir }) => (
<Detail
label={i18n._(t`Project Base Path`)}
value={project_base_dir}
Expand Down
2 changes: 1 addition & 1 deletion awx/ui_next/src/screens/Project/shared/ProjectForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ function ProjectFormFields({
function ProjectForm({ i18n, project, submitError, ...props }) {
const { handleCancel, handleSubmit } = props;
const { summary_fields = {} } = project;
const [{ project_base_dir, project_local_paths }] = useConfig();
const { project_base_dir, project_local_paths } = useConfig();
const [contentError, setContentError] = useState(null);
const [isLoading, setIsLoading] = useState(true);
const [scmSubFormState, setScmSubFormState] = useState({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import ActivityStreamEdit from './ActivityStreamEdit';

function ActivityStream({ i18n }) {
const baseURL = '/settings/activity_stream';
const [{ me }] = useConfig();
const { me } = useConfig();

return (
<PageSection>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { SettingsAPI } from '../../../../api';
import { SettingDetail } from '../../shared';

function ActivityStreamDetail({ i18n }) {
const [{ me }] = useConfig();
const { me } = useConfig();
const { GET: options } = useSettings();

const { isLoading, error, request, result: activityStream } = useRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { SettingsAPI } from '../../../../api';
import { SettingDetail } from '../../shared';

function AzureADDetail({ i18n }) {
const [{ me }] = useConfig();
const { me } = useConfig();
const { GET: options } = useSettings();

const { isLoading, error, request, result: azure } = useRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { SettingsAPI } from '../../../../api';
import { SettingDetail } from '../../shared';

function GitHubDetail({ i18n }) {
const [{ me }] = useConfig();
const { me } = useConfig();
const { GET: options } = useSettings();

const baseURL = '/settings/github';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { useSettings } from '../../../../contexts/Settings';
import { SettingDetail } from '../../shared';

function GoogleOAuth2Detail({ i18n }) {
const [{ me }] = useConfig();
const { me } = useConfig();
const { GET: options } = useSettings();

const { isLoading, error, request, result: googleOAuth2 } = useRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { sortNestedDetails } from '../../shared/settingUtils';
import { SettingDetail } from '../../shared';

function JobsDetail({ i18n }) {
const [{ me }] = useConfig();
const { me } = useConfig();
const { GET: options } = useSettings();

const { isLoading, error, request, result: jobs } = useRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function filterByPrefix(data, prefix) {
}

function LDAPDetail({ i18n }) {
const [{ me }] = useConfig();
const { me } = useConfig();
const { GET: options } = useSettings();
const {
path,
Expand Down
2 changes: 1 addition & 1 deletion awx/ui_next/src/screens/Setting/Logging/Logging.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import LoggingEdit from './LoggingEdit';

function Logging({ i18n }) {
const baseURL = '/settings/logging';
const [{ me }] = useConfig();
const { me } = useConfig();

return (
<PageSection>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { SettingDetail } from '../../shared';
import { sortNestedDetails, pluck } from '../../shared/settingUtils';

function LoggingDetail({ i18n }) {
const [{ me }] = useConfig();
const { me } = useConfig();
const { GET: options } = useSettings();

const { isLoading, error, request, result: logging } = useRequest(
Expand Down
2 changes: 1 addition & 1 deletion awx/ui_next/src/screens/Setting/MiscSystem/MiscSystem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import MiscSystemEdit from './MiscSystemEdit';

function MiscSystem({ i18n }) {
const baseURL = '/settings/miscellaneous_system';
const [{ me }] = useConfig();
const { me } = useConfig();

return (
<PageSection>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { SettingDetail } from '../../shared';
import { sortNestedDetails, pluck } from '../../shared/settingUtils';

function MiscSystemDetail({ i18n }) {
const [{ me }] = useConfig();
const { me } = useConfig();
const { GET: allOptions } = useSettings();

const { isLoading, error, request, result: system } = useRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { useSettings } from '../../../../contexts/Settings';
import { SettingDetail } from '../../shared';

function RADIUSDetail({ i18n }) {
const [{ me }] = useConfig();
const { me } = useConfig();
const { GET: options } = useSettings();

const { isLoading, error, request, result: radius } = useRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { useSettings } from '../../../../contexts/Settings';
import { SettingDetail } from '../../shared';

function SAMLDetail({ i18n }) {
const [{ me }] = useConfig();
const { me } = useConfig();
const { GET: options } = useSettings();
options.SOCIAL_AUTH_SAML_SP_PUBLIC_CERT.type = 'certificate';

Expand Down
2 changes: 1 addition & 1 deletion awx/ui_next/src/screens/Setting/SettingList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const CardDescription = styled.div`
`;

function SettingList({ i18n }) {
const [config] = useConfig();
const config = useConfig();
const settingRoutes = [
{
header: i18n._(t`Authentication`),
Expand Down
2 changes: 1 addition & 1 deletion awx/ui_next/src/screens/Setting/Settings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { SettingsAPI } from '../../api';
import useRequest from '../../util/useRequest';

function Settings({ i18n }) {
const [{ license_info = {}, me }] = useConfig();
const { license_info = {}, me } = useConfig();

const { request, result, isLoading, error } = useRequest(
useCallback(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from '../../../../util/dates';

function SubscriptionDetail({ i18n }) {
const [{ license_info, version }] = useConfig();
const { license_info, version } = useConfig();
const baseURL = '/settings/subscription';
const tabsArray = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useConfig } from '../../../../contexts/Config';
const ANALYTICSLINK = 'https://www.ansible.com/products/automation-analytics';

function AnalyticsStep({ i18n }) {
const [config] = useConfig();
const config = useConfig();
const [manifest] = useField({
name: 'manifest_file',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useConfig } from '../../../../contexts/Config';
import { CheckboxField } from '../../../../components/FormField';

function EulaStep({ i18n }) {
const [{ eula, me }] = useConfig();
const { eula, me } = useConfig();
const [, meta] = useField('eula');
const isValid = !(meta.touched && meta.error);
return (
Expand Down
Loading

0 comments on commit 0e4d0c3

Please sign in to comment.