💄 UI: 加入loading状态展示

This commit is contained in:
fanxb 2019-08-02 14:44:32 +08:00
parent beb2794537
commit fba3d426af
4 changed files with 92 additions and 50 deletions

View File

@ -39,7 +39,8 @@ class AddModal extends React.Component {
url: "", url: "",
nameHelp: "", nameHelp: "",
urlHelp: "", urlHelp: "",
file: null file: null,
isLoading: false
}; };
} }
@ -59,8 +60,10 @@ class AddModal extends React.Component {
const rule = reg[key]; const rule = reg[key];
if (rule.reg.test(value)) { if (rule.reg.test(value)) {
this.setState({ [key + "Help"]: "" }); this.setState({ [key + "Help"]: "" });
return true;
} else { } else {
this.setState({ [key + "Help"]: rule.text }); this.setState({ [key + "Help"]: rule.text });
return false;
} }
} }
@ -93,12 +96,21 @@ class AddModal extends React.Component {
url: url, url: url,
type type
}; };
httpUtil.post("/bookmark/updateOne", body).then(() => { let isOk = this.checkValue("name", name) && (type === 0 ? this.checkValue("url", url) : true);
message.success("编辑成功"); if (!isOk) {
node.name = name; return;
node.url = url; }
this.props.closeModal(); this.setState({ isLoading: true });
}); httpUtil
.post("/bookmark/updateOne", body)
.then(() => {
message.success("编辑成功");
node.name = name;
node.url = url;
this.setState({ isLoading: false });
this.props.closeModal();
})
.catch(() => this.setState({ isLoading: false }));
} }
/** /**
@ -113,6 +125,7 @@ class AddModal extends React.Component {
message.error("请先选择文件"); message.error("请先选择文件");
return; return;
} }
this.setState({ isLoading: true });
const form = new FormData(); const form = new FormData();
form.append("path", path); form.append("path", path);
form.append("file", file.originFileObj); form.append("file", file.originFileObj);
@ -121,8 +134,10 @@ class AddModal extends React.Component {
headers: { "Content-Type": "multipart/form-data" } headers: { "Content-Type": "multipart/form-data" }
}) })
.then(res => { .then(res => {
this.setState({ isLoading: false });
window.location.reload(); window.location.reload();
}); })
.catch(res => this.setState({ isLoading: false }));
} else { } else {
let body = { let body = {
type: this.state.addType === "bookmark" ? 0 : 1, type: this.state.addType === "bookmark" ? 0 : 1,
@ -130,20 +145,29 @@ class AddModal extends React.Component {
name, name,
url url
}; };
httpUtil.put("/bookmark", body).then(res => { let isOk = this.checkValue("name", name) && (body.type === 0 ? this.checkValue("url", url) : true);
// addToTree(res); if (!isOk) {
message.success("加入成功"); return;
if (currentAddFolder === null) { }
treeData.push(res); this.state({ isLoading: true });
} else { httpUtil
//children .put("/bookmark", body)
if (currentAddFolder.children) { .then(res => {
currentAddFolder.children.push(res); // addToTree(res);
message.success("加入成功");
if (currentAddFolder === null) {
treeData.push(res);
} else {
//children
if (currentAddFolder.children) {
currentAddFolder.children.push(res);
}
} }
} updateTreeData(treeData);
updateTreeData(treeData); closeModal();
closeModal(); this.state({ isLoading: false });
}); })
.catch(() => this.setState({ isLoading: false }));
} }
} }
@ -174,7 +198,7 @@ class AddModal extends React.Component {
render() { render() {
const { isShowModal, currentEditNode, closeModal } = this.props; const { isShowModal, currentEditNode, closeModal } = this.props;
const { addType, name, url, nameHelp, urlHelp } = this.state; const { addType, name, url, nameHelp, urlHelp, isLoading } = this.state;
const type = currentEditNode == null ? "add" : "edit"; const type = currentEditNode == null ? "add" : "edit";
const formItemLayout = { const formItemLayout = {
labelCol: { labelCol: {
@ -210,7 +234,7 @@ class AddModal extends React.Component {
) : null} ) : null}
{addType === "file" ? this.renderFileUpload() : null} {addType === "file" ? this.renderFileUpload() : null}
<div style={{ textAlign: "center", paddingTop: "1em" }}> <div style={{ textAlign: "center", paddingTop: "1em" }}>
<Button type="primary" onClick={this.submit}> <Button type="primary" onClick={this.submit} loading={isLoading}>
提交 提交
</Button> </Button>
</div> </div>

View File

@ -193,6 +193,7 @@ export function onDrop(info) {
message.error("无法移动到书签内部"); message.error("无法移动到书签内部");
return; return;
} }
this.setState({ isLoading: true });
const current = info.dragNode.props.dataRef; const current = info.dragNode.props.dataRef;
const body = { const body = {
bookmarkId: current.bookmarkId, bookmarkId: current.bookmarkId,
@ -243,8 +244,10 @@ export function onDrop(info) {
.then(res => { .then(res => {
message.success("移动完成"); message.success("移动完成");
updateTreeData([...treeData]); updateTreeData([...treeData]);
this.setState({ isLoading: false });
}) })
.catch(() => { .catch(() => {
this.setState({ isLoading: false });
message.error("后台移动失败将于2s后刷新页面以免前后台数据不一致"); message.error("后台移动失败将于2s后刷新页面以免前后台数据不一致");
setTimeout(window.location.reload, 2000); setTimeout(window.location.reload, 2000);
}); });

View File

@ -26,7 +26,8 @@ class Login extends Component {
str: "", str: "",
password: "", password: "",
rememberMe: false, rememberMe: false,
errorText: "" errorText: "",
isLoading: false
}; };
this.query = queryString.parse(window.location.search); this.query = queryString.parse(window.location.search);
} }
@ -70,22 +71,27 @@ class Login extends Component {
if (!(this.checkStr(str) && this.checkPassword(password))) { if (!(this.checkStr(str) && this.checkPassword(password))) {
return; return;
} }
axios.post("/user/login", this.state).then(res => { this.setState({ isLoading: true });
const token = res.token; axios
delete res.token; .post("/user/login", this.state)
localStorage.setItem("token", token); .then(res => {
window.token = token; this.setState({ isLoading: false });
this.props.updateToken(token); const token = res.token;
if (this.query.redirect) { delete res.token;
this.props.history.replace(decodeURIComponent(this.query.redirect)); localStorage.setItem("token", token);
} else { window.token = token;
this.props.history.replace("/"); this.props.updateToken(token);
} if (this.query.redirect) {
}); this.props.history.replace(decodeURIComponent(this.query.redirect));
} else {
this.props.history.replace("/");
}
})
.catch(() => this.setState({ isLoading: false }));
}; };
render() { render() {
const { str, password, rememberMe, errorText } = this.state; const { str, password, rememberMe, errorText, isLoading } = this.state;
return ( return (
<LoginLayout type={LOGIN_TYPE}> <LoginLayout type={LOGIN_TYPE}>
<div className={styles.main}> <div className={styles.main}>
@ -114,7 +120,7 @@ class Login extends Component {
</Checkbox> </Checkbox>
<Link to="/public/resetPassword">忘记密码</Link> <Link to="/public/resetPassword">忘记密码</Link>
</div> </div>
<Button type="primary" onClick={this.submit} block> <Button type="primary" onClick={this.submit} block loading={isLoading}>
登录 登录
</Button> </Button>
</div> </div>

View File

@ -38,7 +38,8 @@ export default class Register extends React.Component {
authCode: "", authCode: "",
authCodeText: "获取验证码", authCodeText: "获取验证码",
isCountDown: false, isCountDown: false,
errorText: "" errorText: "",
isLoading: false
}; };
} }
@ -121,23 +122,31 @@ export default class Register extends React.Component {
if (!isOk) { if (!isOk) {
return; return;
} }
this.setState({ isLoading: true });
if (current === REGISTER_TYPE) { if (current === REGISTER_TYPE) {
axios.put("/user", form).then(() => { axios
message.success("注册成功"); .put("/user", form)
setTimeout(() => this.props.history.replace("/public/login"), 500); .then(() => {
}); message.success("注册成功");
this.setState({ isLoading: false });
setTimeout(() => this.props.history.replace("/public/login"), 500);
})
.catch(() => this.setState({ isLoading: false }));
} else { } else {
delete form.username; delete form.username;
axios.post("/user/resetPassword", form).then(() => { axios
message.success("操作成功"); .post("/user/resetPassword", form)
console.log(this.location); .then(() => {
setTimeout(() => this.props.history.replace("/public/login"), 500); message.success("操作成功");
}); this.setState({ isLoading: false });
setTimeout(() => this.props.history.replace("/public/login"), 500);
})
.catch(() => this.setState({ isLoading: false }));
} }
}; };
render() { render() {
const { current, username, email, password, repassword, authCode, authCodeText, isCountDown, errorText } = this.state; const { current, username, email, password, repassword, authCode, authCodeText, isCountDown, errorText, isLoading } = this.state;
return ( return (
<LoginLayout type={current}> <LoginLayout type={current}>
<div className={styles.main}> <div className={styles.main}>
@ -194,7 +203,7 @@ export default class Register extends React.Component {
} }
placeholder="验证码" placeholder="验证码"
/> />
<Button type="primary" className={styles.submit} onClick={this.submit} block> <Button type="primary" className={styles.submit} onClick={this.submit} block loading={isLoading}>
{current === REGISTER_TYPE ? "注册" : "重置密码"} {current === REGISTER_TYPE ? "注册" : "重置密码"}
</Button> </Button>
</div> </div>