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

feat: add basic-auth UI Form #1718

Merged
merged 14 commits into from
Apr 9, 2021
4 changes: 3 additions & 1 deletion web/cypress/fixtures/selector.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,7 @@
"twentyPerPage": "[title=\"20 / page\"]",
"pageList": ".ant-table-pagination-right",
"pageTwo": ".ant-pagination-item-2",
"pageTwoActived": ".ant-pagination-item-2.ant-pagination-item-active"
"pageTwoActived": ".ant-pagination-item-2.ant-pagination-item-active",
"selectDropdown": ".ant-select-dropdown",
"codeMirrorMode": "[data-cy='code-mirror-mode']"
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,17 @@ context('Delete Plugin List with the Drawer', () => {
cy.contains('Create').click();

cy.contains(this.data.basicAuthPlugin).parents(this.domSelector.pluginCardBordered).within(() => {
cy.get('button').click({ force: true });
cy.get('button').click({
force: true
});
});

cy.get(this.domSelector.codeMirrorMode).invoke('text').then(text => {
if (text === 'UIForm') {
cy.get(this.domSelector.codeMirrorMode).click();
cy.get(this.domSelector.selectDropdown).should('be.visible');
cy.get(this.domSelector.selectDropdown + " [label=JSON]").click();
liuxiran marked this conversation as resolved.
Show resolved Hide resolved
}
});

cy.get(this.domSelector.drawer).should('be.visible').within(() => {
Expand All @@ -41,15 +51,21 @@ context('Delete Plugin List with the Drawer', () => {
});

cy.contains('button', 'Submit').click();
cy.get(this.domSelector.drawer, { timeout }).should('not.exist');
cy.get(this.domSelector.drawer, {
timeout
}).should('not.exist');
});

it('should delete the plugin with the drawer', function () {
cy.visit('/plugin/list');
cy.get(this.domSelector.refresh).click();
cy.contains('button', 'Configure').click();
cy.get(this.domSelector.drawerFooter).contains('button', 'Delete').click({ force: true });
cy.contains('button', 'Confirm').click({ force: true });
cy.get(this.domSelector.drawerFooter).contains('button', 'Delete').click({
force: true
});
cy.contains('button', 'Confirm').click({
force: true
});
cy.get(this.domSelector.empty).should('be.visible');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ context('Create Configure and Delete PluginTemplate', () => {
force: true
});
cy.focused(this.domSelector.drawer).should('exist');
cy.get(this.domSelector.drawer, {
timeout
}).within(() => {
cy.get(this.domSelector.disabledSwitcher).click({
force: true,
});

cy.get(this.domSelector.codeMirrorMode).click();
cy.get(this.domSelector.selectDropdown).should('be.visible');
cy.get(this.domSelector.selectDropdown + " [label=JSON]").click();
cy.get(this.domSelector.disabledSwitcher).click({
force: true,
});

cy.contains('Submit').click();
cy.contains('Next').click();
cy.contains('Submit').click();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ context('Create and Delete Route', () => {
cy.get(this.domSelector.checkedSwitcher).should('exist');
});

cy.get(this.domSelector.codeMirrorMode).click();
cy.get(this.domSelector.selectDropdown).should('be.visible');
cy.get(this.domSelector.selectDropdown + " [label=JSON]").click();

cy.contains('button', 'Submit').click();
cy.get(this.domSelector.drawer, { timeout }).should('not.exist');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ context('Create and Delete Service ', () => {
cy.get(this.domSelector.checkedSwitcher).should('exist');
});

cy.get(this.domSelector.codeMirrorMode).click();
cy.get(this.domSelector.selectDropdown).should('be.visible');
cy.get(this.domSelector.selectDropdown + " [label=JSON]").click();
cy.contains('button', 'Submit').click();
cy.get(this.domSelector.drawer, { timeout }).should('not.exist');

Expand Down
35 changes: 23 additions & 12 deletions web/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ Cypress.Commands.add('login', () => {

Cypress.Commands.add('configurePlugins', (cases) => {
const timeout = 300;
const domSelectors = {
const domSelector = {
name: '[data-cy-plugin-name]',
parents: '.ant-card-bordered',
drawer_wrap: '.ant-drawer-content-wrapper',
drawer: '.ant-drawer-content',
switch: '#disable',
close: '.anticon-close',
selectDropdown: '.ant-select-dropdown',
codeMirrorMode: '[data-cy="code-mirror-mode"]'
};

cy.get(domSelectors.name, { timeout }).then(function (cards) {
cy.get(domSelector.name, { timeout }).then(function (cards) {
[...cards].forEach((card) => {
const name = card.innerText;
const pluginCases = cases[name] || [];
Expand All @@ -54,17 +56,17 @@ Cypress.Commands.add('configurePlugins', (cases) => {
}

cy.contains(name)
.parents(domSelectors.parents)
.parents(domSelector.parents)
.within(() => {
cy.contains('Enable').click({
force: true,
});
});

// NOTE: wait for the Drawer to appear on the DOM
cy.focused(domSelectors.drawer).should('exist');
cy.get(domSelectors.drawer, { timeout }).within(() => {
cy.get(domSelectors.switch).click({
cy.focused(domSelector.drawer).should('exist');
cy.get(domSelector.drawer, { timeout }).within(() => {
cy.get(domSelector.switch).click({
force: true,
});
});
Expand All @@ -73,26 +75,35 @@ Cypress.Commands.add('configurePlugins', (cases) => {
if (codemirror) {
codemirror.setValue(JSON.stringify(data));
}
cy.get(domSelectors.drawer).should('exist');
cy.get(domSelectors.drawer, { timeout }).within(() => {
cy.get(domSelector.drawer).should('exist');

cy.get(domSelector.codeMirrorMode).invoke('text').then(text => {
if (text === 'UIForm') {
cy.get(domSelector.codeMirrorMode).click();
cy.get(domSelector.selectDropdown).should('be.visible');
cy.get(domSelector.selectDropdown + " [label=JSON]").click();
}
});

cy.get(domSelector.drawer, { timeout }).within(() => {
cy.contains('Submit').click({
force: true,
});
cy.get(domSelectors.drawer).should('not.exist');
cy.get(domSelector.drawer).should('not.exist');
});
});

if (shouldValid === true) {
cy.get(domSelectors.drawer).should('not.exist');
cy.get(domSelector.drawer).should('not.exist');
} else if (shouldValid === false) {
cy.get(this.domSelector.notification).should('contain', 'Invalid plugin data');

cy.get(domSelectors.close).should('be.visible').click({
cy.get(domSelector.close).should('be.visible').click({
force: true,
multiple: true,
});

cy.get(domSelectors.drawer, { timeout })
cy.get(domSelector.drawer, { timeout })
.invoke('show')
.within(() => {
cy.contains('Cancel').click({
Expand Down
81 changes: 61 additions & 20 deletions web/src/components/Plugin/PluginDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import addFormats from 'ajv-formats';

import { fetchSchema } from './service';
import { json2yaml, yaml2json } from '../../helpers';
import { PluginForm, PLUGIN_UI_LIST } from './UI';

type Props = {
name: string;
Expand Down Expand Up @@ -94,8 +95,10 @@ const PluginDetail: React.FC<Props> = ({
enum codeMirrorModeList {
JSON = 'JSON',
YAML = 'YAML',
UIForm = 'UIForm'
}
const [form] = Form.useForm();
const [UIForm] = Form.useForm();
const ref = useRef<any>(null);
const data = initialData[name] || {};
const pluginType = pluginList.find((item) => item.name === name)?.type;
Expand All @@ -107,11 +110,19 @@ const PluginDetail: React.FC<Props> = ({
{ label: codeMirrorModeList.YAML, value: codeMirrorModeList.YAML },
];

if (PLUGIN_UI_LIST.includes(name)) {
modeOptions.push({ label: codeMirrorModeList.UIForm, value: codeMirrorModeList.UIForm });
}

useEffect(() => {
form.setFieldsValue({
disable: initialData[name] && !initialData[name].disable,
scope: 'global',
});
if (PLUGIN_UI_LIST.includes(name)) {
setCodeMirrorMode(codeMirrorModeList.UIForm);
UIForm.setFieldsValue(initialData[name]);
};
}, []);

const validateData = (pluginName: string, value: PluginComponent.Data) => {
Expand Down Expand Up @@ -161,23 +172,30 @@ const PluginDetail: React.FC<Props> = ({
const handleModeChange = (value: PluginComponent.CodeMirrorMode) => {
switch (value) {
case codeMirrorModeList.JSON: {
const { data: yamlData, error } = yaml2json(ref.current.editor.getValue(), true);

if (error) {
notification.error({
message: 'Invalid Yaml data',
});
return;
if (codeMirrorMode === codeMirrorModeList.YAML) {
const { data: yamlData, error } = yaml2json(ref.current.editor.getValue(), true);
if (error) {
notification.error({
message: 'Invalid Yaml data',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
message: 'Invalid Yaml data',
message: 'Invalid YAML data',

});
return;
}
ref.current.editor.setValue(
js_beautify(yamlData, {
indent_size: 2,
}),
);
} else {
ref.current.editor.setValue(
js_beautify(JSON.stringify(UIForm.getFieldsValue()), {
indent_size: 2,
}),
);
}
ref.current.editor.setValue(
js_beautify(yamlData, {
indent_size: 2,
}),
);
break;
}
case codeMirrorModeList.YAML: {
const { data: jsonData, error } = json2yaml(ref.current.editor.getValue());
const { data: jsonData, error } = json2yaml(codeMirrorMode === codeMirrorModeList.JSON ? ref.current.editor.getValue() : JSON.stringify(UIForm.getFieldsValue()));

if (error) {
notification.error({
Expand All @@ -188,11 +206,28 @@ const PluginDetail: React.FC<Props> = ({
ref.current.editor.setValue(jsonData);
break;
}

case codeMirrorModeList.UIForm: {
if (codeMirrorMode === codeMirrorModeList.JSON) {
UIForm.setFieldsValue(JSON.parse(ref.current.editor.getValue()));
} else {
const { data: yamlData, error } = yaml2json(ref.current.editor.getValue(), true);
if (error) {
notification.error({
message: 'Invalid Yaml data',
});
return;
}
UIForm.setFieldsValue(JSON.parse(yamlData));
}
break;
}
default:
break;
}
setCodeMirrorMode(value);
};

const formatCodes = () => {
try {
if (ref.current) {
Expand Down Expand Up @@ -249,10 +284,15 @@ const PluginDetail: React.FC<Props> = ({
type="primary"
onClick={() => {
try {
const editorData =
codeMirrorMode === codeMirrorModeList.JSON
? JSON.parse(ref.current?.editor.getValue())
: yaml2json(ref.current?.editor.getValue(), false).data;
let editorData;
if (codeMirrorMode === codeMirrorModeList.JSON) {
editorData = JSON.parse(ref.current?.editor.getValue());
} else if (codeMirrorMode === codeMirrorModeList.YAML) {
editorData = yaml2json(ref.current?.editor.getValue(), false).data;
} else {
editorData = UIForm.getFieldsValue();
}

validateData(name, editorData).then((value) => {
onChange({ formData: form.getFieldsValue(), codemirrorData: value });
});
Expand Down Expand Up @@ -333,7 +373,8 @@ const PluginDetail: React.FC<Props> = ({
</Button>,
]}
/>
<CodeMirror
{Boolean(codeMirrorMode === codeMirrorModeList.UIForm) && <PluginForm name={name} form={UIForm} />}
<div style={{ display: codeMirrorMode === codeMirrorModeList.UIForm ? 'none' : 'unset' }}><CodeMirror
ref={(codemirror) => {
ref.current = codemirror;
if (codemirror) {
Expand All @@ -350,8 +391,8 @@ const PluginDetail: React.FC<Props> = ({
lineNumbers: true,
showCursorWhenSelecting: true,
autofocus: true,
}}
/>
}} />
</div>
</Drawer>
</>
);
Expand Down
59 changes: 59 additions & 0 deletions web/src/components/Plugin/UI/basic-auth.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React from 'react';
import type { FormInstance } from 'antd/es/form';
import { Form, Input } from 'antd';

type Props = {
form: FormInstance;
ref?: any;
};

export const FORM_ITEM_LAYOUT = {
labelCol: {
span: 4,
},
wrapperCol: {
span: 8
},
};

const BasicAuth: React.FC<Props> = ({ form }) => {
juzhiyuan marked this conversation as resolved.
Show resolved Hide resolved
return (
<Form
form={form}
{...FORM_ITEM_LAYOUT}
>
<Form.Item
label="username"
juzhiyuan marked this conversation as resolved.
Show resolved Hide resolved
required
name="username"
>
<Input></Input>
</Form.Item>
<Form.Item
label="password"
name="password"
required
>
<Input></Input>
</Form.Item>
</Form>
);
}

export default BasicAuth;
Loading