Skip to content

Commit

Permalink
Create jira alerts flyout
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Aug 5, 2020
1 parent 75b2dcd commit a99a0da
Show file tree
Hide file tree
Showing 2 changed files with 220 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import React, { useCallback } from 'react';

import {
EuiFieldText,
EuiFlexGroup,
EuiFlexItem,
EuiFormRow,
EuiFieldPassword,
EuiSpacer,
} from '@elastic/eui';

import { isEmpty } from 'lodash';
import { ActionConnectorFieldsProps } from '../../../../types';
import * as i18n from './translations';
import { ResilientActionConnector, CasesConfigurationMapping } from './types';
import { connectorConfiguration } from './config';
import { FieldMapping } from './case_mappings/field_mapping';

const ResilientConnectorFields: React.FC<ActionConnectorFieldsProps<ResilientActionConnector>> = ({
action,
editActionSecrets,
editActionConfig,
errors,
consumer,
readOnly,
docLinks,
}) => {
// TODO: remove incidentConfiguration later, when Case ServiceNow will move their fields to the level of action execution
const { apiUrl, orgId, incidentConfiguration, isCaseOwned } = action.config;
const mapping = incidentConfiguration ? incidentConfiguration.mapping : [];

const isApiUrlInvalid: boolean = errors.apiUrl.length > 0 && apiUrl != null;

const { apiKeyId, apiKeySecret } = action.secrets;

const isOrgIdInvalid: boolean = errors.orgId.length > 0 && orgId != null;
const isApiKeyInvalid: boolean = errors.apiKeyId.length > 0 && apiKeyId != null;
const isApiKeySecretInvalid: boolean = errors.apiKeySecret.length > 0 && apiKeySecret != null;

// TODO: remove this block later, when Case ServiceNow will move their fields to the level of action execution
if (consumer === 'case') {
if (isEmpty(mapping)) {
editActionConfig('incidentConfiguration', {
mapping: createDefaultMapping(connectorConfiguration.fields as any),
});
}
if (!isCaseOwned) {
editActionConfig('isCaseOwned', true);
}
}

const handleOnChangeActionConfig = useCallback(
(key: string, value: string) => editActionConfig(key, value),
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
);

const handleOnChangeSecretConfig = useCallback(
(key: string, value: string) => editActionSecrets(key, value),
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
);

const handleOnChangeMappingConfig = useCallback(
(newMapping: CasesConfigurationMapping[]) =>
editActionConfig('incidentConfiguration', {
...action.config.incidentConfiguration,
mapping: newMapping,
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[action.config]
);

return (
<>
<EuiFlexGroup>
<EuiFlexItem>
<EuiFormRow
id="apiUrl"
fullWidth
error={errors.apiUrl}
isInvalid={isApiUrlInvalid}
label={i18n.API_URL_LABEL}
>
<EuiFieldText
fullWidth
isInvalid={isApiUrlInvalid}
name="apiUrl"
readOnly={readOnly}
value={apiUrl || ''} // Needed to prevent uncontrolled input error when value is undefined
data-test-subj="apiUrlFromInput"
placeholder="https://<site-url>"
onChange={(evt) => handleOnChangeActionConfig('apiUrl', evt.target.value)}
onBlur={() => {
if (!apiUrl) {
editActionConfig('apiUrl', '');
}
}}
/>
</EuiFormRow>
</EuiFlexItem>
</EuiFlexGroup>
<EuiSpacer size="m" />
<EuiFlexGroup>
<EuiFlexItem>
<EuiFormRow
id="connector-resilient-orgId-key"
fullWidth
error={errors.orgId}
isInvalid={isOrgIdInvalid}
label={i18n.ORG_ID_LABEL}
>
<EuiFieldText
fullWidth
isInvalid={isOrgIdInvalid}
name="connector-resilient-org-id"
value={orgId || ''} // Needed to prevent uncontrolled input error when value is undefined
data-test-subj="connector-resilient-org-id-form-input"
onChange={(evt) => handleOnChangeActionConfig('orgId ', evt.target.value)}
onBlur={() => {
if (!orgId) {
editActionConfig('orgId ', '');
}
}}
/>
</EuiFormRow>
</EuiFlexItem>
</EuiFlexGroup>
<EuiSpacer size="m" />
<EuiFlexGroup>
<EuiFlexItem>
<EuiFormRow
id="connector-resilient-apiKeyId"
fullWidth
error={errors.apiKeyId}
isInvalid={isApiKeyInvalid}
label={i18n.API_KEY_ID_LABEL}
>
<EuiFieldText
fullWidth
isInvalid={isApiKeyInvalid}
readOnly={readOnly}
name="connector-resilient-apiKeyId"
value={apiKeyId || ''} // Needed to prevent uncontrolled input error when value is undefined
data-test-subj="connector-resilient-apiKeyId-form-input"
onChange={(evt) => handleOnChangeSecretConfig('apiKeyId', evt.target.value)}
onBlur={() => {
if (!apiKeyId) {
editActionSecrets('apiKeyId', '');
}
}}
/>
</EuiFormRow>
</EuiFlexItem>
</EuiFlexGroup>
<EuiSpacer size="m" />
<EuiFlexGroup>
<EuiFlexItem>
<EuiFormRow
id="connector-resilient-apiKeySecret"
fullWidth
error={errors.apiKeySecret}
isInvalid={isApiKeySecretInvalid}
label={i18n.API_KEY_SECRET_LABEL}
>
<EuiFieldPassword
fullWidth
readOnly={readOnly}
isInvalid={isApiKeySecretInvalid}
name="connector-resilient-apiKeySecret"
value={apiKeySecret || ''} // Needed to prevent uncontrolled input error when value is undefined
data-test-subj="connector-resilient-apiKeySecret-form-input"
onChange={(evt) => handleOnChangeSecretConfig('apiKeySecret', evt.target.value)}
onBlur={() => {
if (!apiKeySecret) {
editActionSecrets('apiKeySecret', '');
}
}}
/>
</EuiFormRow>
</EuiFlexItem>
</EuiFlexGroup>
{consumer === 'case' && ( // TODO: remove this block later, when Case ServiceNow will move their fields to the level of action execution
<>
<EuiSpacer size="l" />
<EuiFlexGroup>
<EuiFlexItem data-test-subj="case-resilient-mappings">
<FieldMapping
disabled={true}
connectorActionTypeId={connectorConfiguration.id}
mapping={mapping as CasesConfigurationMapping[]}
onChangeMapping={handleOnChangeMappingConfig}
/>
</EuiFlexItem>
</EuiFlexGroup>
</>
)}
</>
);
};

export const createDefaultMapping = (fields: Record<string, any>): CasesConfigurationMapping[] =>
Object.keys(fields).map(
(key) =>
({
source: fields[key].defaultSourceField,
target: key,
actionType: fields[key].defaultActionType,
} as CasesConfigurationMapping)
);

// eslint-disable-next-line import/no-default-export
export { ResilientConnectorFields as default };
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ export const API_URL_INVALID = i18n.translate(
}
);

export const PROJECT_KEY_LABEL = i18n.translate(
export const ORG_ID_LABEL = i18n.translate(
'xpack.triggersActionsUI.components.builtinActionTypes.resilient.orgId',
{
defaultMessage: 'Organization ID',
}
);

export const PROJECT_KEY_REQUIRED = i18n.translate(
export const ORG_ID_REQUIRED = i18n.translate(
'xpack.triggersActionsUI.components.builtinActionTypes.resilient.requiredOrgIdTextField',
{
defaultMessage: 'Organization ID is required',
Expand Down

0 comments on commit a99a0da

Please sign in to comment.