Skip to content

Commit

Permalink
update types
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 committed Apr 16, 2020
1 parent f703b94 commit aa3b048
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,21 @@ import {
EuiFlexGroup,
EuiFlexItem,
} from '@elastic/eui';
import { defaultDynamicSettings } from '../../../common/runtime_types';
import { defaultDynamicSettings, DynamicSettings } from '../../../common/runtime_types';
import { selectDynamicSettings } from '../../state/selectors';

export const CertificateExpirationForm: React.FC = ({
type NumStr = string | number;

export type OnFieldChangeType = (field: string, value?: NumStr) => void;

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

export const CertificateExpirationForm: React.FC<SettingsFormProps> = ({
onChange,
formFields,
fieldErrors,
Expand Down Expand Up @@ -66,7 +77,7 @@ export const CertificateExpirationForm: React.FC = ({
defaultMessage="The default value is {defaultValue}"
values={{
defaultValue: (
<EuiCode>{defaultDynamicSettings.certificatesThresholds.errorState}</EuiCode>
<EuiCode>{defaultDynamicSettings?.certificatesThresholds?.errorState}</EuiCode>
),
}}
/>
Expand Down Expand Up @@ -110,12 +121,12 @@ export const CertificateExpirationForm: React.FC = ({
defaultMessage="The default value is {defaultValue}"
values={{
defaultValue: (
<EuiCode>{defaultDynamicSettings.certificatesThresholds.warningState}</EuiCode>
<EuiCode>{defaultDynamicSettings?.certificatesThresholds?.warningState}</EuiCode>
),
}}
/>
}
isInvalid={!!fieldErrors?.certificatesThresholds.warningState}
isInvalid={!!fieldErrors?.certificatesThresholds?.warningState}
label={
<FormattedMessage
id="xpack.uptime.sourceConfiguration.warningStateLabel"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ import {
} from '@elastic/eui';
import { defaultDynamicSettings } from '../../../common/runtime_types';
import { selectDynamicSettings } from '../../state/selectors';
import { SettingsFormProps } from './certificate_form';

export const IndicesForm: React.FC = ({ onChange, formFields, fieldErrors, isDisabled }) => {
export const IndicesForm: React.FC<SettingsFormProps> = ({
onChange,
formFields,
fieldErrors,
isDisabled,
}) => {
const dss = useSelector(selectDynamicSettings);

return (
Expand Down
41 changes: 29 additions & 12 deletions x-pack/legacy/plugins/uptime/public/pages/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,42 @@ import {
} from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';
import { useDispatch, useSelector } from 'react-redux';
import { isEqual, set, cloneDeep } from 'lodash';
import { cloneDeep, isEqual, set } from 'lodash';
import { i18n } from '@kbn/i18n';
import { Link } from 'react-router-dom';
import { selectDynamicSettings } from '../state/selectors';
import { getDynamicSettings, setDynamicSettings } from '../state/actions/dynamic_settings';
import { DynamicSettings, DynamicSettingsType } from '../../common/runtime_types';
import { DynamicSettings } from '../../common/runtime_types';
import { useBreadcrumbs } from '../hooks/use_breadcrumbs';
import { OVERVIEW_ROUTE } from '../../common/constants';
import { useKibana } from '../../../../../../src/plugins/kibana_react/public';
import { UptimePage, useUptimeTelemetry } from '../hooks';
import { IndicesForm } from '../components/settings/indices_form';
import { CertificateExpirationForm } from '../components/settings/certificate_form';
import {
CertificateExpirationForm,
OnFieldChangeType,
} from '../components/settings/certificate_form';

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

export const SettingsPage = () => {
const dss = useSelector(selectDynamicSettings);
Expand All @@ -52,18 +76,11 @@ export const SettingsPage = () => {
setFormFields({ ...dss.settings });
}

const blankStr = 'May not be blank';
const fieldErrors = formFields && {
heartbeatIndices: formFields.heartbeatIndices.match(/^\S+$/) ? null : blankStr,
certificatesThresholds: {
errorState: formFields.certificatesThresholds?.errorState ? null : blankStr,
warningState: formFields.certificatesThresholds?.warningState ? null : blankStr,
},
};
const fieldErrors = getFieldErrors(formFields);

const isFormValid = !(fieldErrors && Object.values(fieldErrors).find(v => !!v));

const onChangeFormField = (field: keyof DynamicSettings, value: any) => {
const onChangeFormField: OnFieldChangeType = (field, value) => {
if (formFields) {
const newFormFields = cloneDeep(formFields);
set(newFormFields, field, value);
Expand Down

0 comments on commit aa3b048

Please sign in to comment.