Skip to content

Commit

Permalink
Add type for settings field errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
justinkambic committed Apr 23, 2020
1 parent 8412951 commit 59e60e8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('CertificateForm', () => {
heartbeatIndices: 'heartbeat-8*',
certThresholds: { expiration: 7, age: 36 },
}}
fieldErrors={{}}
fieldErrors={null}
isDisabled={false}
/>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('CertificateForm', () => {
heartbeatIndices: 'heartbeat-8*',
certThresholds: { expiration: 7, age: 36 },
}}
fieldErrors={{}}
fieldErrors={null}
isDisabled={false}
/>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import {
EuiFlexGroup,
EuiFlexItem,
} from '@elastic/eui';
import { DynamicSettings, CertStateThresholds } from '../../../common/runtime_types';
import { CertStateThresholds } from '../../../common/runtime_types';
import { DYNAMIC_SETTINGS_DEFAULTS } from '../../../common/constants';
import { SettingsFormProps } from '../../pages/settings';

interface ChangedValues {
heartbeatIndices?: string;
Expand All @@ -27,14 +28,6 @@ interface ChangedValues {

export type OnFieldChangeType = (changedValues: ChangedValues) => void;

export interface SettingsFormProps {
loading: boolean;
onChange: OnFieldChangeType;
formFields: DynamicSettings | null;
fieldErrors: any;
isDisabled: boolean;
}

export const CertificateExpirationForm: React.FC<SettingsFormProps> = ({
loading,
onChange,
Expand Down Expand Up @@ -70,7 +63,7 @@ export const CertificateExpirationForm: React.FC<SettingsFormProps> = ({
>
<EuiFormRow
describedByIds={['errorState']}
error={fieldErrors?.certificatesThresholds?.errorState}
error={fieldErrors?.certificatesThresholds?.expirationThresholdError}
fullWidth
helpText={
<FormattedMessage
Expand All @@ -83,7 +76,7 @@ export const CertificateExpirationForm: React.FC<SettingsFormProps> = ({
}}
/>
}
isInvalid={!!fieldErrors?.certificatesThresholds?.errorState}
isInvalid={!!fieldErrors?.certificatesThresholds?.expirationThresholdError}
label={
<FormattedMessage
id="xpack.uptime.sourceConfiguration.errorStateLabel"
Expand Down Expand Up @@ -120,7 +113,7 @@ export const CertificateExpirationForm: React.FC<SettingsFormProps> = ({
</EuiFormRow>
<EuiFormRow
describedByIds={['warningState']}
error={fieldErrors?.certificatesThresholds?.warningState}
error={fieldErrors?.certificatesThresholds?.ageThresholdError}
fullWidth
helpText={
<FormattedMessage
Expand All @@ -131,7 +124,7 @@ export const CertificateExpirationForm: React.FC<SettingsFormProps> = ({
}}
/>
}
isInvalid={!!fieldErrors?.certificatesThresholds?.warningState}
isInvalid={!!fieldErrors?.certificatesThresholds?.ageThresholdError}
label={
<FormattedMessage
id="xpack.uptime.sourceConfiguration.warningStateLabel"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
EuiTitle,
EuiSpacer,
} from '@elastic/eui';
import { SettingsFormProps } from './certificate_form';
import { DYNAMIC_SETTINGS_DEFAULTS } from '../../../common/constants';
import { SettingsFormProps } from '../../pages/settings';

export const IndicesForm: React.FC<SettingsFormProps> = ({
onChange,
Expand Down
28 changes: 22 additions & 6 deletions x-pack/legacy/plugins/uptime/public/pages/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,36 @@ import {
} from '../components/settings/certificate_form';
import * as Translations from './translations';

const getFieldErrors = (formFields: DynamicSettings | null) => {
interface SettingsPageFieldErrors {
heartbeatIndices: 'May not be blank' | '';
certificatesThresholds: {
expirationThresholdError: string | null;
ageThresholdError: string | null;
} | null;
}

export interface SettingsFormProps {
loading: boolean;
onChange: OnFieldChangeType;
formFields: DynamicSettings | null;
fieldErrors: SettingsPageFieldErrors | null;
isDisabled: boolean;
}

const getFieldErrors = (formFields: DynamicSettings | null): SettingsPageFieldErrors | null => {
if (formFields) {
const blankStr = 'May not be blank';
const { certThresholds: certificatesThresholds, heartbeatIndices } = formFields;
const heartbeatIndErr = heartbeatIndices.match(/^\S+$/) ? '' : blankStr;
const errorStateErr = certificatesThresholds?.expiration ? null : blankStr;
const warningStateErr = certificatesThresholds?.age ? null : blankStr;
const expirationThresholdError = certificatesThresholds?.expiration ? null : blankStr;
const ageThresholdError = certificatesThresholds?.age ? null : blankStr;
return {
heartbeatIndices: heartbeatIndErr,
certificatesThresholds:
errorStateErr || warningStateErr
expirationThresholdError || ageThresholdError
? {
errorState: errorStateErr,
warningState: warningStateErr,
expirationThresholdError,
ageThresholdError,
}
: null,
};
Expand Down

0 comments on commit 59e60e8

Please sign in to comment.