Skip to content

Commit

Permalink
Fix misc errors in web console (#951)
Browse files Browse the repository at this point in the history
* fix misc bugs in CI dropdown

* fix app registration form

* make refetch optional

* change isEqual to not mutate arrays to prevent error

* fix reported lint issue
  • Loading branch information
nilsgstrabo authored Jan 25, 2024
1 parent 4938732 commit 262df06
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 59 deletions.
39 changes: 17 additions & 22 deletions src/components/app-config-ci/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Typography } from '@equinor/eds-core-react';
import { debounce } from 'lodash';
import * as PropTypes from 'prop-types';
import { useRef, useState } from 'react';
import { MultiValue, SingleValue, StylesConfig } from 'react-select';
import { useEffect, useRef, useState } from 'react';
import { MultiValue, SingleValue } from 'react-select';

import { ConfigurationItemPopover } from './ci-popover';
import { ConfigurationItemSelect } from './ci-select';
Expand All @@ -16,7 +16,7 @@ import {
GetApplicationsApiResponse,
Application,
} from '../../store/service-now-api';
import { getFetchErrorCode, getFetchErrorMessage } from '../../store/utils';
import { getFetchErrorMessage } from '../../store/utils';

export type OnConfigurationItemChangeCallback = (ci?: Application) => void;
type GetApplicationsFunction = ReturnType<
Expand Down Expand Up @@ -49,34 +49,30 @@ export function AppConfigConfigurationItem({
configurationItemChangeCallback,
disabled,
}: Props) {
const [currentCI, setCurrentCI] = useState<Application | null>(null);
const [selectedCI, setSelectedCI] = useState<Application | null>(null);
const [popoverCI, setPopoverCI] = useState<Application>();
const [popoverOpen, setPopoverOpen] = useState(false);
const [getApplications] =
serviceNowApi.endpoints.getApplications.useLazyQuery();

const { data, ...state } = useGetApplicationQuery({
appId: configurationItem,
});
const { data: currentCI, ...currentCIState } = useGetApplicationQuery(
{
appId: configurationItem,
},
{ skip: !configurationItem }
);

useEffect(() => {
setSelectedCI(currentCI);
}, [currentCI]);
const containerRef = useRef<HTMLDivElement>(null);

function onChange(newValue?: Application): void {
configurationItemChangeCallback(newValue);
setCurrentCI(newValue);
setSelectedCI(newValue);
setPopoverOpen(false);
}

const selectStyle: StylesConfig = {
singleValue: (styles) => {
if (state.error && getFetchErrorCode(state.error) === 404) {
styles.backgroundColor = 'var(--eds_interactive_danger__highlight)';
styles.color = 'var(--eds_interactive_danger__text)';
}
return styles;
},
};

return (
<div className="configuration-item-select">
<Typography className="label" group="input" variant="text">
Expand All @@ -93,7 +89,6 @@ export function AppConfigConfigurationItem({
setPopoverOpen(!popoverOpen);
}}
containerRef={containerRef}
styles={selectStyle}
name="ConfigurationItem"
menuPosition="fixed"
closeMenuOnScroll={({ target }: Event) =>
Expand All @@ -110,18 +105,18 @@ export function AppConfigConfigurationItem({
getOptionValue={({ id }) => id}
isClearable
closeMenuOnSelect={false}
value={currentCI || data}
value={selectedCI}
isDisabled={disabled}
/>
<Typography className="helpertext" group="input" variant="text">
Application from IT Software Inventory (type 3 characters to search)
</Typography>

{state.isError && (
{currentCIState.isError && (
<div>
<Alert type="danger">
<Typography>
Failed to load. {getFetchErrorMessage(state.error)}
Failed to load. {getFetchErrorMessage(currentCIState.error)}
</Typography>
</Alert>
</div>
Expand Down
11 changes: 5 additions & 6 deletions src/components/app-list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ export default function AppList() {
[]
);

const {
data: appsData,
refetch,
...appsState
} = useShowApplicationsQuery({}, { pollingInterval });
const { data: appsData, ...appsState } = useShowApplicationsQuery(
{},
{ pollingInterval }
);
const { data: favsData, ...favsState } = useGetSearchApplicationsQuery(
{
apps: favourites?.join(','),
Expand Down Expand Up @@ -78,7 +77,7 @@ export default function AppList() {
<div className="app-list__header">
<Typography variant="body_short_bold">Favourites</Typography>
<div className="create-app">
<PageCreateApplication refetch={refetch} />
<PageCreateApplication />
</div>
</div>
<div className="app-list">
Expand Down
8 changes: 4 additions & 4 deletions src/components/component/toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Props = {
component?: Component;
startEnabled?: boolean;
stopEnabled?: boolean;
refetch: Function;
refetch?: Function;
};
export function Toolbar({
appName,
Expand Down Expand Up @@ -55,7 +55,7 @@ export function Toolbar({
envName,
componentName: component.name,
}).unwrap();
await refetch();
await refetch?.();
} catch (error) {
errorToast(`Failed to start component. ${getFetchErrorMessage(error)}`);
}
Expand All @@ -67,7 +67,7 @@ export function Toolbar({
envName,
componentName: component.name,
}).unwrap();
await refetch();
await refetch?.();
} catch (error) {
errorToast(`Failed to stop component. ${getFetchErrorMessage(error)}`);
}
Expand All @@ -79,7 +79,7 @@ export function Toolbar({
envName,
componentName: component.name,
}).unwrap();
await refetch();
await refetch?.();
} catch (error) {
errorToast(`Failed to restart component. ${getFetchErrorMessage(error)}`);
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/configure-application-github/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const radixZoneDNS = configVariables.RADIX_CLUSTER_BASE;

interface Props {
app: ApplicationRegistration;
refetch: Function;
refetch?: Function;
onDeployKeyChange: (appName: string) => void;
startVisible?: boolean;
useOtherCiToolOptionVisible?: boolean;
Expand Down Expand Up @@ -68,7 +68,7 @@ export const ConfigureApplicationGithub = ({
regenerateDeployKeyAndSecretData: { sharedSecret: nanoid() },
}).unwrap();
await refetchSecrets();
await refetch();
await refetch?.();
});

return (
Expand Down
6 changes: 3 additions & 3 deletions src/components/create-application-form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function sanitizeName(name: string): string {
}

type Props = {
onCreated: (application: ApplicationRegistration) => Promise<void>;
onCreated: (application: ApplicationRegistration) => void;
};
export default function CreateApplicationForm({ onCreated }: Props) {
const [acknowledgeWarnings, setAcknowledgeWarnings] = useState(false);
Expand Down Expand Up @@ -92,8 +92,8 @@ export default function CreateApplicationForm({ onCreated }: Props) {
}).unwrap();

//Only call onCreated when created without warnings, or created with ack warnings
if (response.warnings?.length === 0 || acknowledgeWarnings) {
await onCreated(response.applicationRegistration);
if (!response.warnings?.length || acknowledgeWarnings) {
onCreated(response.applicationRegistration);
successToast('Saved');
} else {
warningToast('Registration had warnings');
Expand Down
9 changes: 5 additions & 4 deletions src/components/page-configuration/change-admin-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import {
useModifyRegistrationDetailsMutation,
} from '../../store/radix-api';
import { handlePromiseWithToast } from '../global-top-nav/styled-toaster';
import { difference } from 'lodash';

const isEqual = (a: Array<unknown>, b: Array<unknown>) =>
JSON.stringify(a.sort()) === JSON.stringify(b.sort());
a.length == b.length && difference(a, b).length === 0;

interface Props {
registration: ApplicationRegistration;
refetch: Function;
refetch?: Function;
}
export default function ChangeAdminForm({ registration, refetch }: Props) {
const [adminAdGroup, setAdminAdGroup] = useState<Array<string>>();
Expand All @@ -40,7 +41,7 @@ export default function ChangeAdminForm({ registration, refetch }: Props) {
},
},
}).unwrap();
await refetch();
await refetch?.();
setAdminAdGroup(undefined);
setReaderAdGroup(undefined);
}
Expand Down Expand Up @@ -101,5 +102,5 @@ export default function ChangeAdminForm({ registration, refetch }: Props) {

ChangeAdminForm.proptypes = {
registration: PropTypes.object.isRequired,
refetch: PropTypes.func.isRequired,
refetch: PropTypes.func,
};
6 changes: 3 additions & 3 deletions src/components/page-configuration/change-ci-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Application } from '../../store/service-now-api';
interface Props {
appName: string;
configurationItem?: string;
refetch: Function;
refetch?: Function;
}

export const ChangeConfigurationItemForm = ({
Expand All @@ -40,7 +40,7 @@ export const ChangeConfigurationItemForm = ({
},
}).unwrap();

await refetch();
await refetch?.();
});

return (
Expand Down Expand Up @@ -93,5 +93,5 @@ export const ChangeConfigurationItemForm = ({
ChangeConfigurationItemForm.propTypes = {
appName: PropTypes.string.isRequired,
configurationItem: PropTypes.string,
refetch: PropTypes.func.isRequired,
refetch: PropTypes.func,
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { getFetchErrorMessage } from '../../store/utils';
export interface ChangeConfigBranchFormProps {
appName: string;
configBranch: string;
refetch: Function;
refetch?: Function;
}

export const ChangeConfigBranchForm: FunctionComponent<
Expand All @@ -41,7 +41,7 @@ export const ChangeConfigBranchForm: FunctionComponent<
},
}).unwrap();

await refetch();
await refetch?.();
});

return (
Expand Down
6 changes: 3 additions & 3 deletions src/components/page-configuration/change-config-file-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { getFetchErrorMessage } from '../../store/utils';
export interface ChangeConfigFileFormProps {
appName: string;
radixConfigFullName?: string;
refetch: Function;
refetch?: Function;
}

const defaultConfigName = 'radixconfig.yaml';
Expand All @@ -39,7 +39,7 @@ export const ChangeConfigFileForm: FunctionComponent<
},
}).unwrap();

await refetch();
await refetch?.();
});

return (
Expand Down Expand Up @@ -104,5 +104,5 @@ export const ChangeConfigFileForm: FunctionComponent<
ChangeConfigFileForm.propTypes = {
appName: PropTypes.string.isRequired,
radixConfigFullName: PropTypes.string,
refetch: PropTypes.func.isRequired,
refetch: PropTypes.func,
};
6 changes: 3 additions & 3 deletions src/components/page-configuration/change-repository-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const DeployKey = ({ appName }: { appName: string }) => {
interface Props {
appName: string;
repository: string;
refetch: Function;
refetch?: Function;
sharedSecret: string;
}
export function ChangeRepositoryForm({
Expand Down Expand Up @@ -71,7 +71,7 @@ export function ChangeRepositoryForm({
},
}).unwrap();

await refetch();
await refetch?.();
});

return (
Expand Down Expand Up @@ -254,6 +254,6 @@ export function ChangeRepositoryForm({
ChangeRepositoryForm.propTypes = {
appName: PropTypes.string.isRequired,
repository: PropTypes.string.isRequired,
refetch: PropTypes.func.isRequired,
refetch: PropTypes.func,
sharedSecret: PropTypes.string.isRequired,
};
2 changes: 1 addition & 1 deletion src/components/page-create-application/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export default (
padding: '16px',
}}
>
<PageCreateApplication refetch={() => {}} />
<PageCreateApplication />
</div>
);
10 changes: 4 additions & 6 deletions src/components/page-create-application/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@ function scrollToPosition(elementRef: Element, x: number, y: number): void {
elementRef.scrollTo?.(x, y);
}

type Props = { refetch: Function };
export default function PageCreateApplication({ refetch }: Props) {
export default function PageCreateApplication() {
const [visibleScrim, setVisibleScrim] = useState(false);
const containerRef = useRef<HTMLDivElement>(null);
const [registration, setRegistration] =
useState<ApplicationRegistration | null>(null);

const onCloseScrim = () => {
setVisibleScrim(false);
setRegistration(null);
};

const onCreated = async (newRegistration: ApplicationRegistration) => {
setRegistration(newRegistration);
await refetch();
const onCreated = (newRegistration: ApplicationRegistration) => {
scrollToPosition(containerRef.current, 0, 0);
setRegistration(newRegistration);
};

return (
Expand Down Expand Up @@ -59,7 +58,6 @@ export default function PageCreateApplication({ refetch }: Props) {
set up
</Typography>
<ConfigureApplicationGithub
refetch={refetch}
app={registration}
startVisible
onDeployKeyChange={() => void 0}
Expand Down

0 comments on commit 262df06

Please sign in to comment.