javascript - 仅在经过身份验证时关闭模态
问题描述
为了创建交易,用户必须首先在确认对话框模式中进行身份验证(密码)。一旦用户通过身份验证,模式就会关闭并显示交易。如果用户未正确验证,则模式仍会关闭,并显示带有错误的 toast 消息。我想更改此逻辑,以便用户在尝试未通过身份验证时必须重新输入密码。
在 createTransaction 中设置状态是以异步方式完成的。newTransactionModal 中的 this.closeModal 是导致模式关闭和状态重置的原因。
NewTransactionModal.js
this.state.showConfirmDialog ? (
<ConfirmDialog
type="New Transaction"
onPasswordChange={this.changePassword}
onConfirm={this.handleCreateClick}
onCancel={this.handleClose}
errMsg = {this.props.passwordErrMsg}
/>
) : null
NewTransactionModal.js
handleCreateClick = () => {
if (this.formIsValid()) {
let path = '/transaction',
transaction = {
type: this.state.transactionType.id,
amount: this.state.transactionAmount,
internalComment: this.state.comment,
userPassword: this.state.password
},
extraDataForError = {
typeName: this.state.transactionType.name,
advertiserName: this.state.advertiser.name,
fundingType: this.state.advertiser.fundingType,
financialDocumentId: this.state.documentId,
financialDocumentType: this.state.document && this.state.document.documentMetadata.documentType
};
if (this.state.transactionType.creditType) {
path += '/transfer';
transaction.debitAdvertiserId = this.state.advertiserId;
transaction.creditAdvertiserId = this.state.transferAdvertiserId;
transaction.debitFinancialDocumentId = this.state.documentId;
transaction.creditFinancialDocumentId = this.state.documentId;
} else {
transaction.advertiserId = this.state.advertiserId;
transaction.financialDocumentId = this.state.documentId;
}
this.props.createTransaction(path, transaction, extraDataForError);
this.closeModal();
}
};
ListTransaction.js
createTransaction = (path, data, extra) => {
const failureMsg = 'Failed to create transaction';
if (!path || !data) {
this.setState({
toastMessage: failureMsg,
toastType: 'error'
});
return;
}
const getErrTransactions = res => {
return [{ transaction: {...data, ...extra}, validations: res.validations, result: res.result }];
};
this.setState({ toastType: 'pending' }, async () => {
try {
const res = await ApiService.post(path, data, this.abortController.signal);
if (res && res.result !== 'FAILURE') {
this.setState({
toastMessage: 'Transaction created',
toastType: 'success',
selected: []
}, () => this.loadData(1));
} else if (res) {
this.handleError(getErrTransactions(res), failureMsg);
} else {
this.setState({toastType: null})
}
} catch (err) {
if (err) {
if (err.name === 'AbortError') {
return false;
} else if (err.json) {
this.setState({
passwordErrMsg: "Please enter a valid password"
})
const jsonErr = await err.json();
this.handleError(jsonErr.result ? getErrTransactions(jsonErr) : jsonErr, failureMsg);
} else {
this.handleError(err, failureMsg);
}
} else {
this.setState({ toastType: null });
}
}
});
};
ListTransactions.js
canCreate ? (
<NewTransactionModal
show={showNewTransactionModal}
types={allowedTransactionTypes}
createTransaction={this.createTransaction}
passwordErrMsg = {this.state.passwordErrMsg}
handlePasswordAttempt = {this.handlePasswordAttempt}
handleRecent={this.handleRecent}
handleClose={this.hideModal}
/>
) : null
我希望用户在无效尝试后仍必须输入密码,并且模式不会消失。此外,在用户输入正确密码后,模式将关闭。
解决方案
推荐阅读
- npm - Webpack GZip 压缩创建许多包/.gz 文件而不是 1 个完整包?
- python - 不使用关键字 from 的包中的显式相对导入
- android - 是否可以像在旧的 G+/Picasa API 上一样仅使用电子邮件获取个人公开个人资料照片?
- ruby - AWS Lambda 无法找到 Ruby 依赖项
- r - 在 for 循环中调用函数时的默认值
- django - 如何自定义登录错误信息?
- python - ANN 中减少损失函数的正常行为是否取决于隐藏层中神经元的数量?
- python-3.x - 访问字典时默认*key*?
- html - HTML 元素不包含所有页面内容
- python - Python多行匹配字符串结尾