Skip to content

Commit

Permalink
refactor: upgrade antd to v4 (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
snapre authored Nov 1, 2021
1 parent 09fb9b5 commit 4035fa5
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 226 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reliable-web",
"version": "1.0.0",
"version": "1.1.0",
"description": "Testing management suite with continuous delivery support.",
"private": true,
"dependencies": {
Expand Down
5 changes: 3 additions & 2 deletions view/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reliable-view",
"version": "1.0.8",
"version": "1.1.0",
"description": "view layer for Reliable",
"files": [
"public/*.js",
Expand All @@ -26,6 +26,7 @@
"lint"
],
"devDependencies": {
"@ant-design/icons": "^4.7.0",
"@babel/core": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.0.0",
"@babel/plugin-proposal-export-default-from": "^7.0.0",
Expand All @@ -35,7 +36,7 @@
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.0.0",
"@babel/runtime": "^7.0.0",
"antd": "^3.21.1",
"antd": "^4.16.13",
"autoprefixer": "^9.1.0",
"awesome-clipboard": "^2.0.2",
"babel-loader": "^8.0.0",
Expand Down
11 changes: 6 additions & 5 deletions view/src/components/BuildsTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import dayjs from 'dayjs';
import { Link } from 'react-router-dom';
import Clipboard from 'awesome-clipboard';
import {
Icon,
Table,
Popover,
message,
} from 'antd';
import {
SettingOutlined,
RightCircleOutlined,
} from '@ant-design/icons';
import { FormattedMessage } from 'react-intl';

import {
Expand Down Expand Up @@ -56,7 +57,7 @@ export default class BuildsTable extends React.Component {
href={record.configureUrl}
target="_blank"
>
<Icon type="setting" />
<SettingOutlined />
</a>
<a
style={{
Expand Down Expand Up @@ -110,7 +111,7 @@ export default class BuildsTable extends React.Component {
search: `?jobName=${record.jobName}&buildNumber=${record.buildNumber}`,
}}
>
<Icon type="right-circle" theme="outlined" />
<RightCircleOutlined />
</Link>
: ''
);
Expand Down
224 changes: 104 additions & 120 deletions view/src/components/DingdingSetting.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
'use strict';

import React from 'react';
import React, { useEffect, useState } from 'react';
import safeGet from 'lodash.get';
import uniqBy from 'lodash.uniqby';
import { FormattedMessage } from 'react-intl';
import {
Form,
Icon,
Spin,
Input,
Button,
Select,
message,
} from 'antd';
import {
DeleteOutlined,
PlusOutlined,
} from '@ant-design/icons';

import request from '../util/request';

const FormItem = Form.Item;
const Option = Select.Option;

const webhookFormItemLayout = {
Expand All @@ -32,148 +34,130 @@ const buttonFormItemLayout = {
},
};

class DingdingSetting extends React.Component {
state = {
webhooks: [],
loading: false,
}

componentDidMount () {
this.fetchWebhooks();
}
const DingdingSetting = () => {
const [form] = Form.useForm();
const [loading, setLoading] = useState(false);

fetchWebhooks = async () => {
this.setState({ loading: true });
const fetchWebhooks = async () => {
setLoading(true);
const res = await request('getWebhooks', 'GET');
this.setState({ loading: false });
setLoading(false);
if (!res.success) return;

const webhooks = safeGet(res, 'data.webhooks');
if (!Array.isArray(webhooks) || !webhooks.length) {
this.setState({
form.setFieldsValue({
webhooks: [{
url: '',
}],
}]
});
return;
}

this.setState({
webhooks,
});
}
form.setFieldsValue({ webhooks })
};

useEffect(() => {
fetchWebhooks();
}, []);

postWebhooks = async (webhooks) => {
const postWebhooks = async (webhooks) => {
const res = await request('postWebhooks', 'POST', {
webhooks,
});
if (res.success) {
await this.fetchWebhooks();
await fetchWebhooks();
message.success('Update webhooks successfully!');
} else {
message.error('Update webhooks failed.');
console.error('postWebhooks', res);
}
}

removeOneNotification = index => {
const webhooks = [
...this.state.webhooks,
];
webhooks.splice(index, 1);
this.setState({ webhooks });
}

addMoreNotification = () => {
this.setState({
webhooks: [
...this.state.webhooks,
{
url: '',
},
],
});
}

updateWebhooks = e => {
e.preventDefault();
this.props.form.validateFields((err, values) => {
if (err) return;
const uniqWebhooks = uniqBy(values.webhooks, value => `${value.tag}${value.url}`);
this.postWebhooks(uniqWebhooks);
});
}

renderWebhookList = () => {
const { getFieldDecorator } = this.props.form;
return this.state.webhooks.map((webhook, index) => {
const notifyTypeSelector = getFieldDecorator(`webhooks[${index}].tag`, {
initialValue: webhook.tag || 'build',
})(
<Select style={{ width: 88 }}>
<Option value="build"><FormattedMessage id="setting.notification.build" /></Option>
</Select>
);
return (<FormItem
key={index}
{...webhookFormItemLayout}
};

const updateWebhooks = values => {
const uniqWebhooks = uniqBy(values.webhooks, value => `${value.tag}${value.url}`);
postWebhooks(uniqWebhooks);
};

return (
<Spin spinning={loading}>
<Form
form={form}
onFinish={updateWebhooks}
autoComplete="off"
>
{getFieldDecorator(`webhooks[${index}].url`, {
validateTrigger: ['onBlur'],
initialValue: webhook.url,
rules: [{
required: true,
type: 'url',
whitespace: true,
message: 'Please input DingTalk webhook url.',
}],
})(
<Input
addonBefore={notifyTypeSelector}
placeholder="webhook"
addonAfter={
(
webhook.url || // committed webhook
(index === this.state.webhooks.length - 1) // new webhook input
) ? (
<Icon
style={{
cursor: 'pointer',
}}
type="delete"
onClick={() => this.removeOneNotification(index)}
/>
) : null
}
/>
)}
</FormItem>);
});
}

render () {
return (
<Form>
<Spin spinning={this.state.loading}>
{this.renderWebhookList()}
</Spin>
<FormItem {...webhookFormItemLayout}>
<Button data-accessibilityid="add-notification" type="dashed" onClick={this.addMoreNotification}
style={{ width: '100%' }}>
<Icon type="plus" /> <FormattedMessage id='setting.addDingMessage' />
</Button>
</FormItem>
<FormItem {...buttonFormItemLayout}>
<Form.List name="webhooks">
{(fields, { add, remove }) => (
<>
{fields.map(field => (
<div key={field.key}>
<Form.Item
{...field}
{...webhookFormItemLayout}
name={[field.name, 'url']}
fieldKey={[field.fieldKey, 'url']}
validateTrigger={['onBlur']}
rules={[{
required: true,
type: 'url',
whitespace: true,
message: 'Please input DingTalk webhook url.',
}]}
>
<Input
addonBefore={
<Form.Item
name={[field.name, 'tag']}
initialValue="build"
noStyle
>
<Select style={{ width: 88 }}>
<Option value="build"><FormattedMessage id="setting.notification.build" /></Option>
</Select>
</Form.Item>
}
placeholder="webhook"
addonAfter={
<DeleteOutlined
style={{
cursor: 'pointer',
}}
onClick={() => remove(field.name)}
/>
}
/>
</Form.Item>
</div>
))}

<Form.Item
{...webhookFormItemLayout}
>
<Button
type="dashed"
data-accessibilityid="add-notification"
onClick={() => add()}
block
icon={<PlusOutlined />}
>
<FormattedMessage id='setting.addDingMessage' />
</Button>
</Form.Item>
</>
)}
</Form.List>
<Form.Item {...buttonFormItemLayout}>
<Button
onClick={this.updateWebhooks}
htmlType="submit"
type="primary"
style={{ width: '100%' }}
><FormattedMessage id='setting.submit' /></Button>
</FormItem>
>
<FormattedMessage id='setting.submit' />
</Button>
</Form.Item>
</Form>
);
}
}

export default Form.create()(DingdingSetting);
</Spin>
);
};

export default DingdingSetting;
Loading

0 comments on commit 4035fa5

Please sign in to comment.