diff --git a/front/src/pages/manage/OverView/AddModal.jsx b/front/src/pages/manage/OverView/AddModal.jsx index b9b27bf..2d797e2 100644 --- a/front/src/pages/manage/OverView/AddModal.jsx +++ b/front/src/pages/manage/OverView/AddModal.jsx @@ -19,13 +19,26 @@ function mapDispatchToProps(dispatch) { }; } +const reg = { + name: { + reg: /^.{1,40}$/, + text: "名称字符数为1-40" + }, + url: { + reg: /^.{1,500}$/, + text: "链接字符数为1-500" + } +}; + class AddModal extends React.Component { constructor(props) { super(props); this.state = { - addType: 0, - addName: "", - addValue: "", + addType: "bookmark", + name: "", + url: "", + nameHelp: "", + urlHelp: "", file: null }; } @@ -34,11 +47,28 @@ class AddModal extends React.Component { const { currentEditNode } = nextProps; if (currentEditNode != null) { this.type = "edit"; - this.setState({ addType: currentEditNode.type, addName: currentEditNode.name, addValue: currentEditNode.url }); + this.setState({ addType: currentEditNode.type === 0 ? "bookmark" : "folder", name: currentEditNode.name, url: currentEditNode.url }); } else { this.type = "add"; - this.setState({ addType: 0, addName: "", addValue: "", file: null }); + this.setState({ addType: "bookmark", name: "", url: "", file: null }); } + this.setState({ nameHelp: "", urlHelp: "" }); + } + + checkValue(key, value) { + const rule = reg[key]; + if (rule.reg.test(value)) { + this.setState({ [key + "Help"]: "" }); + } else { + this.setState({ [key + "Help"]: rule.text }); + } + } + + changeValue(e) { + const name = e.target.name; + const value = e.target.value.trim(); + this.checkValue(name, value); + this.setState({ [name]: value }); } submit = () => { @@ -55,18 +85,18 @@ class AddModal extends React.Component { * @param {*} node */ editOne(node) { - const { addName, addValue } = this.state; + const { name, url } = this.state; const { bookmarkId, type } = this.props.currentEditNode; const body = { bookmarkId, - name: addName, - url: addValue, + name: name, + url: url, type }; httpUtil.post("/bookmark/updateOne", body).then(() => { message.success("编辑成功"); - node.name = addName; - node.url = addValue; + node.name = name; + node.url = url; this.props.closeModal(); }); } @@ -77,10 +107,15 @@ class AddModal extends React.Component { addOne() { const { currentAddFolder, updateTreeData, closeModal, treeData } = this.props; const path = currentAddFolder == null ? "" : currentAddFolder.path + "." + currentAddFolder.bookmarkId; - if (this.state.addType === 2) { + const { name, url, file, addType } = this.state; + if (addType === "file") { + if (file == null) { + message.error("请先选择文件"); + return; + } const form = new FormData(); form.append("path", path); - form.append("file", this.state.file.originFileObj); + form.append("file", file.originFileObj); httpUtil .put("/bookmark/uploadBookmarkFile", form, { headers: { "Content-Type": "multipart/form-data" } @@ -90,10 +125,10 @@ class AddModal extends React.Component { }); } else { let body = { - type: this.state.addType, + type: this.state.addType === "bookmark" ? 0 : 1, path, - name: this.state.addName, - url: this.state.addValue + name, + url }; httpUtil.put("/bookmark", body).then(res => { // addToTree(res); @@ -112,9 +147,34 @@ class AddModal extends React.Component { } } + renderFileUpload() { + const uploadProps = { + accept: ".html,.htm", + onChange: e => { + this.setState({ file: e.fileList[0] }); + }, + beforeUpload: file => { + return false; + }, + fileList: [] + }; + const { file } = this.state; + return ( +