Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a DDF component for provider credentials validation #6657

Merged
merged 1 commit into from Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,28 @@
import React from 'react';
import AsyncCredentials from './async-credentials';

const AsyncProviderCredentials = ({ ...props }) => {
const asyncValidate = fields => new Promise((resolve, reject) => {
API.post('/api/providers', {
action: 'verify_credentials',
resource: fields,
}).then(({ results: [result] }) => {
const { task_id, success } = result; // eslint-disable-line camelcase
// The request here can either create a background task or fail
return success ? API.wait_for_task(task_id) : Promise.reject(result);
// The wait_for_task request can succeed with valid or invalid credentials
// with the message that the task is completed successfully. Based on the
// task_results we resolve() or reject() with an unknown error.
// Any known errors are passed to the catch(), which will reject() with a
// message describing what went wrong.
}).then(result => (result.task_results ? resolve() : reject(__('Validation failed: unknown error'))))
.catch(({ message }) => reject([__('Validation failed:'), message].join(' ')));
});

return <AsyncCredentials asyncValidate={asyncValidate} {...props} />;
};

AsyncProviderCredentials.propTypes = AsyncCredentials.propTypes;
AsyncProviderCredentials.defaultProps = AsyncCredentials.defaultProps;

export default AsyncProviderCredentials;
2 changes: 2 additions & 0 deletions app/javascript/forms/mappers/formsFieldsMapper.jsx
Expand Up @@ -3,6 +3,7 @@ import { componentTypes } from '@data-driven-forms/react-form-renderer';
import { formFieldsMapper, components } from '@data-driven-forms/pf3-component-mapper';

import AsyncCredentials from '../../components/async-credentials/async-credentials';
import AsyncProviderCredentials from '../../components/async-credentials/async-provider-credentials';
import DualGroup from '../../components/dual-group';
import DualListSelect from '../../components/dual-list-select';
import EditSecretField from '../../components/async-credentials/edit-secret-field';
Expand All @@ -20,6 +21,7 @@ const fieldsMapper = {
hr: () => <hr />,
'secret-switch-field': SecretSwitchField,
'validate-credentials': AsyncCredentials,
'validate-provider-credentials': AsyncProviderCredentials,
[componentTypes.SELECT]: props => <components.SelectField classNamePrefix="miq-ddf-select" {...props} />,
};

Expand Down