Skip to content

Commit

Permalink
fix issue 302 (#303)
Browse files Browse the repository at this point in the history
* add system/resource searcher to search menu with name

* api doc optimize

* api doc optimize

* update api tag form

* update api doc module

* npm run lint:fix

* api doc fix (#302)
  • Loading branch information
VampireAchao authored Aug 2, 2023
1 parent ee33872 commit a2b9e06
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 53 deletions.
4 changes: 4 additions & 0 deletions src/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@
"SHENYU.DOCUMENT.APIDOC.INFO.RESPONSE.PARAMETERS": "Response Parameters",
"SHENYU.DOCUMENT.APIDOC.INFO.COMMON.RESPONSE.PARAMETERS":
"Common Response Parameters",
"SHENYU.DOCUMENT.APIDOC.INFO.COMMON.RESPONSE.CODE": "Response Code",
"SHENYU.DOCUMENT.APIDOC.INFO.COMMON.RESPONSE.DESCRIPTION": "Error Description",
"SHENYU.DOCUMENT.APIDOC.INFO.COMMON.RESPONSE.DESCRIPTION_EXAMPLE": "Illegal Parameter",
"SHENYU.DOCUMENT.APIDOC.INFO.COMMON.RESPONSE.RESULTS": "Response Results",
"SHENYU.DOCUMENT.APIDOC.INFO.BUSINESS.RESPONSE.PARAMETERS":
"Business Response Parameters",
"SHENYU.DOCUMENT.APIDOC.INFO.INTERFACE.DEBUG": "Interface Debug",
Expand Down
4 changes: 4 additions & 0 deletions src/locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,10 @@
"SHENYU.DOCUMENT.APIDOC.INFO.SERVICE.REQUEST.PARAMETERS": "业务请求参数",
"SHENYU.DOCUMENT.APIDOC.INFO.RESPONSE.PARAMETERS": "响应参数",
"SHENYU.DOCUMENT.APIDOC.INFO.COMMON.RESPONSE.PARAMETERS": "公共响应参数",
"SHENYU.DOCUMENT.APIDOC.INFO.COMMON.RESPONSE.CODE": "返回码",
"SHENYU.DOCUMENT.APIDOC.INFO.COMMON.RESPONSE.DESCRIPTION": "错误描述信息",
"SHENYU.DOCUMENT.APIDOC.INFO.COMMON.RESPONSE.DESCRIPTION_EXAMPLE": "非法的参数",
"SHENYU.DOCUMENT.APIDOC.INFO.COMMON.RESPONSE.RESULTS": "响应的业务结果",
"SHENYU.DOCUMENT.APIDOC.INFO.BUSINESS.RESPONSE.PARAMETERS": "业务响应参数",
"SHENYU.DOCUMENT.APIDOC.INFO.INTERFACE.DEBUG": "接口调试",
"SHENYU.DOCUMENT.APIDOC.INFO.INTERFACE.DOCUMENT": "接口文档",
Expand Down
19 changes: 14 additions & 5 deletions src/routes/Document/ApiDoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

import { Col, Row, Card, BackTop, Empty, message } from "antd";
import React, { useEffect, useState } from "react";
import { getApi ,
import {
getApi,
getDocMenus,
getApiDetail,
deleteApi,
Expand Down Expand Up @@ -118,7 +119,12 @@ function ApiDoc() {
message.error(msg);
} else {
message.success(msg);
searchApiRef.current?.updateTree();
if (tagDetail.id) {
searchApiRef.current?.updateTree(null, 'tag');
}
if (apiDetail.id) {
searchApiRef.current?.updateTree(null, 'api');
}
}
};

Expand All @@ -132,9 +138,12 @@ function ApiDoc() {
};

// eslint-disable-next-line no-unused-vars
const handleAfterUpdate = data => {
setApiDetail({});
setTagDetail({});
const handleAfterUpdate = (data, refType) => {
if (refType === 'tag') {
setTagDetail({ ...tagDetail, ...data });
} else if (refType === 'api') {
setApiDetail({ ...apiDetail, ...data });
}
};

useEffect(() => {
Expand Down
38 changes: 19 additions & 19 deletions src/routes/Document/components/AddAndUpdateApiDoc.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
/*
* 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.
*/
* 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.
*/

/* eslint-disable no-unused-expressions */
/* eslint-disable radix */
import { Modal, Form, Input, Select, message,Radio } from "antd";
import { Modal, Form, Input, Select, message, Radio } from "antd";
import React, { Component } from "react";
import PropTypes from "prop-types";
import { Method, RPCTYPE, API_SOURCE_TYPE, STATE_TYPE } from "./globalData";
Expand Down Expand Up @@ -46,7 +46,7 @@ class AddAndUpdateApiDoc extends Component {
try {
let obj = JSON.parse(str);
return !!(typeof obj === 'object' && obj);
} catch(e) {
} catch (e) {
return false;
}
}
Expand Down Expand Up @@ -100,7 +100,7 @@ class AddAndUpdateApiDoc extends Component {
document = "",
visible = false
} = this.props;
const { getFieldDecorator } = form;
const { getFieldDecorator, getFieldValue } = form;
const formItemLayout = {
labelCol: {
sm: { span: 6 }
Expand Down Expand Up @@ -275,7 +275,7 @@ class AddAndUpdateApiDoc extends Component {
<Radio.Group buttonStyle="solid">
{STATE_TYPE.map((e, i) => {
return (
<Radio.Button key={`${e} ${i}`} value={i}>
<Radio.Button key={`${e} ${i}`} value={i} disabled={!getFieldValue("id") && e === 'offline'}>
{e}
</Radio.Button>
);
Expand Down
8 changes: 4 additions & 4 deletions src/routes/Document/components/ApiInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,21 @@ function ApiInfo(props) {
id: 1,
name: "code",
type: "integer",
description: "返回码",
description: getIntlContent("SHENYU.DOCUMENT.APIDOC.INFO.COMMON.RESPONSE.CODE"),
example: "200"
},
{
id: 2,
name: "message",
type: "string",
description: "错误描述信息",
example: "非法的参数"
description: getIntlContent("SHENYU.DOCUMENT.APIDOC.INFO.COMMON.RESPONSE.DESCRIPTION"),
example: getIntlContent("SHENYU.DOCUMENT.APIDOC.INFO.COMMON.RESPONSE.DESCRIPTION_EXAMPLE")
},
{
id: 3,
name: "data",
type: "object",
description: "响应的业务结果",
description: getIntlContent("SHENYU.DOCUMENT.APIDOC.INFO.COMMON.RESPONSE.RESULTS"),
example: '{"id":"1988771289091030"}'
}
];
Expand Down
78 changes: 53 additions & 25 deletions src/routes/Document/components/SearchApi.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*
* 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.
*/
* 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.
*/

/* eslint-disable no-unused-expressions */

Expand All @@ -23,7 +23,7 @@ import { getRootTag, getParentTagId, getApi } from "../../../services/api";
import { Method } from "./globalData";
import AddAndUpdateTag from "./AddAndUpdateTag";
import AddAndUpdateApiDoc from "./AddAndUpdateApiDoc";
import {getIntlContent} from "../../../utils/IntlUtils";
import { getIntlContent } from "../../../utils/IntlUtils";

const { Text } = Typography;

Expand All @@ -32,8 +32,10 @@ const SearchApi = React.forwardRef((props, ref) => {
const [loading, setLoading] = useState(false);
const [treeData, setTreeData] = useState({});
const [expandedKeys, setExpandedKeys] = useState([]);
const [selectedKeys, setSelectedKeys] = useState([]);

const queryRootTag = async () => {
setExpandedKeys([]);
setLoading(true);
const { code, data = [], message: msg } = await getRootTag();
setLoading(false);
Expand All @@ -48,7 +50,7 @@ const SearchApi = React.forwardRef((props, ref) => {
key: index.toString(),
isLeaf: false
})) || [];
if(data?.length) {
if (data?.length) {
const { code: apiCode, message: apiMsg, data: apiDataRecords } = await getApi(data[0].id);
if (apiCode !== 200) {
message.error(apiMsg);
Expand All @@ -59,6 +61,7 @@ const SearchApi = React.forwardRef((props, ref) => {
}
setTreeData(arr);
// 默认选中第一个
setSelectedKeys(["0"]);
onSelect(["0"], { node: { props: arr[0] } })
};

Expand Down Expand Up @@ -122,7 +125,7 @@ const SearchApi = React.forwardRef((props, ref) => {
curNode.children.push({
selectable: false,
title: (
<Row gutter={8}>
<Row gutter={18}>
{showAddTag && (
<Col span={12}>
<Button
Expand Down Expand Up @@ -172,7 +175,7 @@ const SearchApi = React.forwardRef((props, ref) => {

const handleTagOk = data => {
handleTagCancel();
updateTree(data);
updateTree(data, 'tag');
};

const [openApi, setOpenApi] = useState(false);
Expand All @@ -185,10 +188,11 @@ const SearchApi = React.forwardRef((props, ref) => {

const handleApiOk = data => {
handleApiCancel();
updateTree(data);
updateTree(data, 'api');
};

const addOrUpdateApi = data => {
apiForm.resetFields()
apiForm.setFieldsValue({
...data
});
Expand All @@ -202,10 +206,30 @@ const SearchApi = React.forwardRef((props, ref) => {
setOpenTag(true);
};

const updateTree = data => {
setExpandedKeys([]);
queryRootTag();
afterUpdate(data);
const updateTree = (data, refType) => {
if (!data?.id) {
queryRootTag()
return
}
let allNodes = treeData.flatMap(i => i.children ? [...i.children, i] : i)
let curNodeIdx = allNodes.findIndex(t => t.id && t.id === data.id) ?? -1
if (curNodeIdx === -1) {
return
}
if (refType === 'tag') {
allNodes[curNodeIdx].title = data.name
} else if (refType === 'api') {
allNodes[curNodeIdx].title = (
<>
<Text code>{Method[data.httpMethod]}</Text>
<Tooltip title={data.apiPath}>{data.apiPath}</Tooltip>
</>
)
}
// forceUpdate tree
setTreeData()
setTreeData(treeData)
afterUpdate(data, refType);
};

useImperativeHandle(ref, () => ({
Expand All @@ -223,10 +247,14 @@ const SearchApi = React.forwardRef((props, ref) => {
{treeData?.length ? (
<Spin spinning={loading}>
<Tree
onSelect={onSelect}
onSelect={(keys, e) => {
setSelectedKeys(keys)
onSelect(keys, e)
}}
treeData={treeData}
onExpand={onExpand}
expandedKeys={expandedKeys}
selectedKeys={selectedKeys}
defaultSelectedKeys={["0"]}
/>
</Spin>
Expand Down

0 comments on commit a2b9e06

Please sign in to comment.