Skip to content

Commit

Permalink
feat: improve codes style & bug fix (apache#579)
Browse files Browse the repository at this point in the history
* feat: use svg locally

* feat(Route): use id as rowkey

* feat: update global request

* feat: update import order

* feat: remove unused rules

* fix(Route): PluginOrchestration

* feat(request): using code instead of status code

* feat: remove comments

* feat(i18n): consumer

* feat(SSL): hide SSL currently

* feat(Login): using errorHandler

* feat: update version

* feat: update version

* feat: update plugin's version

Co-authored-by: litesun <7sunmiao@gmail.com>
  • Loading branch information
juzhiyuan and LiteSun authored Oct 22, 2020
1 parent d31a7e6 commit e9d5040
Showing 15 changed files with 50 additions and 39 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apisix-dashboard",
"version": "1.6.1",
"version": "2.0.2",
"private": true,
"description": "Dashboard for Apache APISIX",
"scripts": {
@@ -54,7 +54,7 @@
"@ant-design/icons": "^4.0.0",
"@ant-design/pro-layout": "^6.0.0",
"@ant-design/pro-table": "2.6.3",
"@api7-dashboard/plugin": "^1.0.7",
"@api7-dashboard/plugin": "^1.0.8",
"@api7-dashboard/pluginchart": "^1.0.14",
"@api7-dashboard/ui": "^1.0.3",
"@rjsf/antd": "2.2.0",
1 change: 1 addition & 0 deletions public/empty.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 17 additions & 1 deletion src/app.tsx
Original file line number Diff line number Diff line change
@@ -66,7 +66,7 @@ export const request: RequestConfig = {
credentials: 'same-origin',
requestInterceptors: [
(url, options) => {
const newOptions = options;
const newOptions = { ...options };
newOptions.headers = {
...options.headers,
Authorization: localStorage.getItem('token') || '',
@@ -77,4 +77,20 @@ export const request: RequestConfig = {
};
},
],
responseInterceptors: [
async (res) => {
if (!res.ok) {
// NOTE: http code >= 400, using errorHandler
return res;
}

const data = await res.json();
const { code = -1 } = data as Res<any>;
if (code !== 0) {
// eslint-disable-next-line
return Promise.reject({ response: res, data });
}
return data;
},
],
};
24 changes: 12 additions & 12 deletions src/helpers.tsx
Original file line number Diff line number Diff line change
@@ -34,11 +34,12 @@ export const getMenuData = (): MenuDataItem[] => {
path: '/routes/list',
icon: <IconFont type="iconroute" />,
},
{
name: 'ssl',
path: '/ssl/list',
icon: <IconFont type="iconSSLshuzizhengshu" />,
},
// NOTE: disable SSL module in v2.0
// {
// name: 'ssl',
// path: '/ssl/list',
// icon: <IconFont type="iconSSLshuzizhengshu" />,
// },
{
name: 'upstream',
path: '/upstream/list',
@@ -64,24 +65,23 @@ export const isLoginPage = () => window.location.pathname.indexOf('/user/login')
*/
export const errorHandler = (error: { response: Response; data: any }): Promise<Response> => {
const { response } = error;
if (response && response.status) {
if (error && response && response.status) {
if ([401].includes(response.status) && !isLoginPage()) {
history.replace(`/user/logout?redirect=${encodeURIComponent(window.location.pathname)}`);
return Promise.reject(response);
}
if ([401].includes(response.status) && isLoginPage()) return Promise.reject(response);

const errorText =
error.data.msg || error.data.message || error.data.error_msg || codeMessage[response.status];

// TODO: improve code message mapper
const errorText = error.data?.message || codeMessage[response.status];
notification.error({
message: `请求错误,错误码: ${error.data.errorCode || response.status}`,
message: `Request Error Code: ${error.data.code}`,
description: errorText,
});
} else if (!response) {
notification.error({
description: '您的网络发生异常,无法连接服务器',
message: '网络异常',
description: 'Network Error',
message: '',
});
}
return Promise.reject(response);
2 changes: 1 addition & 1 deletion src/locales/zh-CN/menu.ts
Original file line number Diff line number Diff line change
@@ -68,6 +68,6 @@ export default {
'menu.routes': '路由',
'menu.ssl': '证书',
'menu.upstream': '上游',
'menu.consumer': '用户',
'menu.consumer': 'Consumer',
'menu.setting': '设置',
};
2 changes: 1 addition & 1 deletion src/pages/Metrics/Metrics.tsx
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ const Metrics: React.FC = () => {
<Card>
{!grafanaURL && (
<Empty
image="https://gw.alipayobjects.com/zos/antfincdn/ZHrcdLPrvN/empty.svg"
image="empty.svg"
imageStyle={{
height: 60,
}}
5 changes: 3 additions & 2 deletions src/pages/Route/Create.tsx
Original file line number Diff line number Diff line change
@@ -93,7 +93,7 @@ const Page: React.FC<Props> = (props) => {
}
}, []);

const StepList = () => {
const renderStepList = () => {
if (step === 1) {
return (
<Step1
@@ -261,7 +261,8 @@ const Page: React.FC<Props> = (props) => {
<Step title={item} key={item} />
))}
</Steps>
<StepList />
{renderStepList()}
{/* NOTE: PluginOrchestration works unexpected when using <renderStepList/> */}
</Card>
</PageHeaderWrapper>
<ActionBar step={step} lastStep={redirect ? 2 : 4} onChange={onStepChange} withResultView />
2 changes: 1 addition & 1 deletion src/pages/Route/List.tsx
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@ const Page: React.FC = () => {
>
<ProTable<RouteModule.ResponseBody>
actionRef={ref}
rowKey="name"
rowKey="id"
columns={columns}
request={fetchList}
toolBarRender={() => [
1 change: 0 additions & 1 deletion src/pages/Route/service.ts
Original file line number Diff line number Diff line change
@@ -64,7 +64,6 @@ export const checkUniqueName = (name = '', exclude = '') =>
});

export const fetchUpstreamList = () => {
// TODO: Use Cache and search on local
return request<Res<ResListData<UpstreamModule.RequestBody>>>('/upstreams').then(({ data }) => ({
data: data.rows,
total: data.total_size,
1 change: 0 additions & 1 deletion src/pages/Route/transform.ts
Original file line number Diff line number Diff line change
@@ -14,7 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable @typescript-eslint/naming-convention */
import { omit, pick } from 'lodash';

export const transformStepData = ({
3 changes: 1 addition & 2 deletions src/pages/SSL/service.ts
Original file line number Diff line number Diff line change
@@ -14,7 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable @typescript-eslint/naming-convention */
import { request } from 'umi';
import querystring from 'querystring';
import { identity, pickBy, omit } from 'lodash';
@@ -28,7 +27,7 @@ export const fetchList = (
queryObj = pickBy({ ...queryObj, expire_start, expire_end, sni: search }, identity);
const query = querystring.encode(queryObj);
return request<{ count: number; list: SSLModule.ResSSL[] }>(
`/ssls?page=${current}&size=${pageSize}&${query}`,
`/ssl?page=${current}&page_size=${pageSize}&${query}`,
).then((data) => {
return {
total: data.count,
1 change: 0 additions & 1 deletion src/pages/Upstream/service.ts
Original file line number Diff line number Diff line change
@@ -17,7 +17,6 @@
import { request } from 'umi';

export const fetchList = ({ current = 1, pageSize = 10, ...res }) => {
// TODO: Use Cache and search on local
return request<Res<ResListData<UpstreamModule.RequestBody>>>('/upstreams', {
params: {
name: res.name,
8 changes: 2 additions & 6 deletions src/pages/User/Login.tsx
Original file line number Diff line number Diff line change
@@ -18,11 +18,12 @@ import React, { useState } from 'react';
import { Button, notification, Tabs } from 'antd';
import { SelectLang } from '@@/plugin-locale/SelectLang';
import { Link, useIntl, history } from 'umi';
import { SettingOutlined } from '@ant-design/icons';

import LoginMethodPassword from '@/pages/User/components/LoginMethodPassword';
import LoginMethodExample from '@/pages/User/components/LoginMethodExample';
import { UserModule } from '@/pages/User/typing';
import logo from '@/assets/logo.svg';
import { SettingOutlined } from '@ant-design/icons';
import { getUrlQuery } from '@/helpers';
import Footer from '@/components/Footer';
import styles from './Login.less';
@@ -66,11 +67,6 @@ const Page: React.FC = () => {
history.replace(redirect ? decodeURIComponent(redirect) : '/');
},
});
} else {
notification.error({
message: formatMessage({ id: 'component.status.fail' }),
description: response.message,
});
}
});
}
9 changes: 5 additions & 4 deletions src/pages/User/components/LoginMethodPassword.tsx
Original file line number Diff line number Diff line change
@@ -15,12 +15,12 @@
* limitations under the License.
*/
import React from 'react';
import { UserModule } from '@/pages/User/typing';
import { Form, Input } from 'antd';
import { FormInstance } from 'antd/lib/form';
import { UserOutlined, LockTwoTone } from '@ant-design/icons';
import { formatMessage } from '@@/plugin-locale/localeExports';
import { request } from '@@/plugin-request/request';
import { request, formatMessage } from 'umi';

import { UserModule } from '@/pages/User/typing';

const formRef = React.createRef<FormInstance>();

@@ -112,9 +112,10 @@ const LoginMethodPassword: UserModule.LoginMethod = {
data: [],
};
} catch (e) {
// NOTE: API failed, using errorHandler
return {
status: false,
message: formatMessage({ id: 'component.user.loginMethodPassword.incorrectPassword' }),
message: '',
data: [],
};
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -199,10 +199,10 @@
json-schema "^0.2.5"
set-value "^3.0.2"

"@api7-dashboard/plugin@^1.0.7":
version "1.0.7"
resolved "https://registry.yarnpkg.com/@api7-dashboard/plugin/-/plugin-1.0.7.tgz#ab4bd47c0ce3f3aac3c1ed5de7f78725f77d31de"
integrity sha512-VVQW6xfrKou2n1UBTBZVwZbvc1ZSskvvGlDb7x5kBbVoAzAVAT8bZqGnvrn6aZtei4f8YKkmSWFCD7+zBAKx8w==
"@api7-dashboard/plugin@^1.0.8":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@api7-dashboard/plugin/-/plugin-1.0.8.tgz#31ab85755d5df69d22b4f45b0097f0da1b6a22ea"
integrity sha512-RK5R9rqRpEiNYmGvJjtVmkvwf7G2r2fZ4Ufbra/pOOTL8DyuvQHzw1JHOSWuO7qfjK4QDxf/SVKYOdJYy/CTZQ==
dependencies:
"@rjsf/antd" "^2.3.0"
"@rjsf/core" "^2.3.0"

0 comments on commit e9d5040

Please sign in to comment.