Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 导入JSON发生异常无错误提示问题 #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/components/editor/Importer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { RootState, Property } from 'actions/types'
import JSON5 from 'json5'
import { Button } from '@material-ui/core'
import { POS_TYPE } from './InterfaceSummary'
import {MSG_TYPE, showMessage} from '../../actions/common'

const mockResult =
process.env.NODE_ENV === 'development'
? () => ({
Expand Down Expand Up @@ -120,7 +122,7 @@ class Importer extends Component<ImporterProps, ImporterState> {
</div>
<div className="rmodal-footer">
<div className="form-group mb0">
<Button type="submit" style={{ marginRight: 8 }} variant="contained" color="primary">
<Button type="submit" onClick={this.verifyJSONFormat} style={{ marginRight: 8 }} variant="contained" color="primary">
提交
</Button>
<Button onClick={() => rmodal.close()} > 取消 </Button>
Expand All @@ -130,6 +132,16 @@ class Importer extends Component<ImporterProps, ImporterState> {
</section>
)
}
// 校验JSON格式异常, 有异常时提示错误
verifyJSONFormat = (event: any) => {
try {
JSON5.parse(this.state.result)
} catch (e) {
console.error(e.message)
this.props.showMessage('JSON格式错误, 请校正后重试', MSG_TYPE.WARNING)
event.preventDefault()
}
}
componentDidUpdate() {
this.context.rmodal.reposition()
}
Expand Down Expand Up @@ -248,5 +260,6 @@ const mapStateToProps = (state: RootState) => ({
})
const mapDispatchToProps = {
onAddProperty: addProperty,
showMessage: showMessage
}
export default connect(mapStateToProps, mapDispatchToProps)(Importer)