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

Added promise debounce library (with updated typescript) and async validation in Catalog form #5284

Merged
merged 4 commits into from
Mar 13, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions app/javascript/components/catalog-form/catalog-form.schema.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
import { componentTypes, validatorTypes } from '@data-driven-forms/react-form-renderer';
import { componentTypes } from '@data-driven-forms/react-form-renderer';
import debouncePromise from '../../helpers/promise-debounce';
import { API } from '../../http_api';

export const asyncValidator = value => API.get(`/api/service_catalogs?expand=resources&filter[]=name=${value}`)
.then((json) => {
if (json.resources.length > 0) {
return __('Name has already been taken');
}
if (value === '' || value === undefined) {
return __("Name can't be blank");
}
return undefined;
});

const asyncValidatorDebounced = debouncePromise(asyncValidator);

function createSchema(options) {
const fields = [{
Expand All @@ -7,10 +22,9 @@ function createSchema(options) {
fields: [{
component: componentTypes.TEXT_FIELD,
name: 'name',
validate: [{
type: validatorTypes.REQUIRED,
message: __("Name can't be blank"),
}],
validate: [
asyncValidatorDebounced,
],
label: __('Name'),
maxLength: 40,
autoFocus: true,
Expand Down
7 changes: 7 additions & 0 deletions app/javascript/helpers/promise-debounce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import AwesomeDebouncePromise from 'awesome-debounce-promise';

export default (asyncFunction, debounceTime = 250, options = undefined) => AwesomeDebouncePromise(
asyncFunction,
debounceTime,
options,
);
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ exports[`Catalog form component should render add variant form 1`] = `
"maxLength": 40,
"name": "name",
"validate": Array [
Object {
"message": "Name can't be blank",
"type": "required-validator",
},
[Function],
],
"validateOnMount": true,
},
Expand Down Expand Up @@ -120,10 +117,7 @@ exports[`Catalog form component should render edit variant form 1`] = `
"maxLength": 40,
"name": "name",
"validate": Array [
Object {
"message": "Name can't be blank",
"type": "required-validator",
},
[Function],
],
"validateOnMount": true,
},
Expand Down
35 changes: 35 additions & 0 deletions app/javascript/spec/catalog-form/validator.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import fetchMock from 'fetch-mock';
import { asyncValidator } from '../../components/catalog-form/catalog-form.schema';

describe('catalog form - promise validator', () => {
afterEach(() => {
fetchMock.reset();
fetchMock.restore();
});

it('returns error message when name is taken', () => {
fetchMock.getOnce('/api/service_catalogs?expand=resources&filter[]=name=a1', {
resources: [
{ name: 'a1' },
],
});

return asyncValidator('a1').then(data => expect(data).toEqual(__('Name has already been taken')));
});

it('returns error message when name is undefined', () => {
fetchMock.getOnce('/api/service_catalogs?expand=resources&filter[]=name=undefined', {
resources: [],
});

return asyncValidator(undefined).then(data => expect(data).toEqual(__("Name can't be blank")));
});

it('returns nothing when passes', () => {
fetchMock.getOnce('/api/service_catalogs?expand=resources&filter[]=name=a1', {
resources: [],
});

return asyncValidator('a1').then(data => expect(data).toEqual(undefined));
});
});
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
},
"homepage": "https://github.com/ManageIQ/manageiq#readme",
"dependencies": {
"@data-driven-forms/pf3-component-mapper": "^0.4.3",
"@data-driven-forms/react-form-renderer": "^1.7.0",
"@data-driven-forms/pf3-component-mapper": "^0.4.5",
"@data-driven-forms/react-form-renderer": "^1.9.3",
"@manageiq/react-ui-components": "~0.10.8",
"@manageiq/ui-components": "~1.2.2",
"@pf3/select": "~1.12.6",
Expand All @@ -39,6 +39,7 @@
"angular-ui-sortable": "~0.16.1",
"angular.validators": "~4.4.3",
"array-includes": "~3.0.3",
"awesome-debounce-promise": "^2.1.0",
"bootstrap-filestyle": "~1.2.1",
"bootstrap-switch": "~3.3.4",
"classnames": "~2.2.6",
Expand Down Expand Up @@ -145,7 +146,7 @@
"stylelint": "^8.2.0",
"stylelint-config-standard": "^17.0.0",
"ts-jest": "~22.4.3",
"typescript": "~2.8.1",
"typescript": "~3.3.3333",
"webpack": "~4.12.0",
"webpack-cli": "~3.0.7",
"webpack-dev-server": "~3.1.4",
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"app/javascript/typings"
],
"allowJs": true,
"jsx": "react"
"jsx": "react",
"allowSyntheticDefaultImports": true,
},
"exclude": [
"**/*.spec.ts",
Expand Down