Add alertify.js library and rewrite alerts using it. Add data validation and error handling

parent 6ebb777c
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
"version": "0.1.0", "version": "0.1.0",
"private": true, "private": true,
"dependencies": { "dependencies": {
"alertify.js": "^1.0.12",
"bootstrap": "^3.3.7", "bootstrap": "^3.3.7",
"jquery": "^3.3.1", "jquery": "^3.3.1",
"react": "^16.4.1", "react": "^16.4.1",
......
...@@ -6,6 +6,8 @@ import {i18n} from '../Localization'; ...@@ -6,6 +6,8 @@ import {i18n} from '../Localization';
import {config} from '../config'; import {config} from '../config';
const alertify = require("alertify.js");
class App extends Component { class App extends Component {
constructor(props, context) { constructor(props, context) {
...@@ -32,10 +34,15 @@ class App extends Component { ...@@ -32,10 +34,15 @@ class App extends Component {
$(document).keydown(event => { $(document).keydown(event => {
if (event.ctrlKey && event.keyCode === 13) { if (event.ctrlKey && event.keyCode === 13) {
const selection = this.getSelectedText(); const selection = this.getSelectedText();
if (selection === "") {
return;
}
if (selection.length < config.minTypoLength || if (selection.length < config.minTypoLength ||
selection.length > config.maxTypoLength) selection.length > config.maxTypoLength)
{ {
alert(i18n.formatString(i18n.errorSelectionLength, alertify.error(i18n.formatString(i18n.errorSelectionLength,
config.minTypoLength, config.maxTypoLength)); config.minTypoLength, config.maxTypoLength));
return; return;
......
...@@ -11,8 +11,12 @@ export const i18n = new LocalizedStrings({ ...@@ -11,8 +11,12 @@ export const i18n = new LocalizedStrings({
modalCommentPlaceholder: `Например, "опечатка"`, modalCommentPlaceholder: `Например, "опечатка"`,
modalCommentLabel: "Пояснения к исправлению", modalCommentLabel: "Пояснения к исправлению",
errorSelectionLength: "Опечатка должна быть длиной от {0} до {1} символов", errorSelectionLength: "Опечатка должна быть длиной от {0} до {1} символов",
errorCorrectLength: "Исправление должно быть длиной от {0} до {1} символов",
errorDoesNotDistinct: "Исправленный вариант идентичен исходному",
close: "Закрыть", close: "Закрыть",
saveChanges: "Сохранить изменения" saveChanges: "Сохранить изменения",
messageSuccess: "Благодарим за отправку. Опечатка будет исправлена",
messageFailture: "Не удалось отправить исправление",
}, },
en: { en: {
......
import React, { Component } from 'react'; import React, { Component } from 'react';
import { Modal, Button, FormGroup, FormControl, ControlLabel, HelpBlock } from 'react-bootstrap' import { Modal, Button, FormGroup, FormControl, ControlLabel, HelpBlock, Alert } from 'react-bootstrap'
import './Modal.css'; import './Modal.css';
import {i18n} from '../Localization';
import {config} from '../config';
import {i18n} from '../Localization' const alertify = require("alertify.js");
class TypoModal extends Component { class TypoModal extends Component {
...@@ -13,7 +17,8 @@ class TypoModal extends Component { ...@@ -13,7 +17,8 @@ class TypoModal extends Component {
show: this.props.show, show: this.props.show,
text: this.props.text, text: this.props.text,
correct: this.props.text, correct: this.props.text,
comment: "" comment: "",
error: "",
} }
this.closeCallback = props.closeCallback; this.closeCallback = props.closeCallback;
...@@ -43,10 +48,36 @@ class TypoModal extends Component { ...@@ -43,10 +48,36 @@ class TypoModal extends Component {
this.setState({ correct: event.target.value }); this.setState({ correct: event.target.value });
} }
// Validate input data
checkData = () => {
if (this.state.text === this.state.correct) {
this.setState({ error: i18n.errorDoesNotDistinct });
return false;
}
if (this.state.correct.length > config.maxCorrectLength ||
this.state.correct.length < config.minCorrectLength) {
this.setState({
error: i18n.formatString(i18n.errorCorrectLength,
config.minCorrectLength,
config.maxCorrectLength)
});
return false;
}
return true;
}
// Send typo to the typos server // Send typo to the typos server
submitTypo = (corrected, comment) => { submitTypo = (corrected, comment) => {
alert(`Submit typo ${this.state.text}->${this.state.correct} (${this.state.comment})`); if (this.checkData()) {
this.handleClose(); this.handleClose();
alertify.success(i18n.messageSuccess);
return;
}
alertify.error(i18n.messageFailture)
} }
render() { render() {
...@@ -82,6 +113,8 @@ class TypoModal extends Component { ...@@ -82,6 +113,8 @@ class TypoModal extends Component {
onChange={this.onChangeComment}/> onChange={this.onChangeComment}/>
</FormGroup> </FormGroup>
</form> </form>
{ this.state.error ? <Alert bsStyle="danger">{this.state.error}</Alert> : null }
</Modal.Body> </Modal.Body>
<Modal.Footer> <Modal.Footer>
......
export const config = { export const config = {
maxTypoLength: 50, maxTypoLength: 50,
minTypoLength: 2, minTypoLength: 4,
maxCorrectLength: 100, maxCorrectLength: 100,
minCorrectLength: 1, minCorrectLength: 1,
}; };
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment