You need to sign in or sign up before continuing.

Complete a typo sending to the server and user informing about that

parent 040cea16
...@@ -16,8 +16,8 @@ export const i18n = new LocalizedStrings({ ...@@ -16,8 +16,8 @@ export const i18n = new LocalizedStrings({
errorTooOften: "Вы отправляете исправления слишком часто", errorTooOften: "Вы отправляете исправления слишком часто",
close: "Закрыть", close: "Закрыть",
saveChanges: "Сохранить изменения", saveChanges: "Сохранить изменения",
messageSuccess: "Благодарим за отправку. Опечатка будет исправлена", messageSuccess: "Благодарим за отправку опечатки. Мы уже работаем над её исправлением!",
messageFailture: "Не удалось отправить исправление", messageFailture: "Не удалось отправить исправление. Мы уже работаем над решением проблемы!",
}, },
en: { en: {
......
import React, { Component } from 'react'; import React, { Component } from 'react';
import { Modal, Button, FormGroup, FormControl, ControlLabel, HelpBlock, Alert } from 'react-bootstrap' import { Modal, Button, FormGroup, FormControl, ControlLabel, HelpBlock, Alert } from 'react-bootstrap'
import $ from 'jquery';
import './Modal.css'; import './Modal.css';
import {i18n} from '../Localization'; import { i18n } from '../Localization';
import {config} from '../config'; import { config } from '../config';
const alertify = require("alertify.js"); const alertify = require("alertify.js");
...@@ -21,7 +22,13 @@ class TypoModal extends Component { ...@@ -21,7 +22,13 @@ class TypoModal extends Component {
error: "" error: ""
} }
// TODO: English support
i18n.setLanguage(config.language);
this.closeCallback = props.closeCallback; this.closeCallback = props.closeCallback;
// Bindings
this.submitTypo = this.submitTypo.bind(this);
} }
handleShow = () => { handleShow = () => {
...@@ -29,7 +36,7 @@ class TypoModal extends Component { ...@@ -29,7 +36,7 @@ class TypoModal extends Component {
} }
handleClose = () => { handleClose = () => {
this.setState({ this.setState({
show: false, show: false,
text: this.props.text, text: this.props.text,
comment: "" comment: ""
...@@ -50,18 +57,18 @@ class TypoModal extends Component { ...@@ -50,18 +57,18 @@ class TypoModal extends Component {
// Validate input data // Validate input data
checkData = () => { checkData = () => {
if (this.state.text === this.state.correct) { if (this.state.text === this.state.correct) {
this.setState({ error: i18n.errorDoesNotDistinct }); this.setState({ error: i18n.errorDoesNotDistinct });
return false; return false;
} }
if (this.state.correct.length > config.maxCorrectLength || if (this.state.correct.length > config.maxCorrectLength ||
this.state.correct.length < config.minCorrectLength) { this.state.correct.length < config.minCorrectLength) {
this.setState({ this.setState({
error: i18n.formatString(i18n.errorCorrectLength, error: i18n.formatString(i18n.errorCorrectLength,
config.minCorrectLength, config.minCorrectLength,
config.maxCorrectLength) config.maxCorrectLength)
}); });
return false; return false;
} }
...@@ -69,18 +76,71 @@ class TypoModal extends Component { ...@@ -69,18 +76,71 @@ class TypoModal extends Component {
return true; return true;
} }
// Send typo to the typos server // Send data to the server
submitTypo = (corrected, comment) => { async sendRequest() {
const serverUrl = config.serverUrl;
const url = /*window.location.href*/
"https://test.etersoft.ru/test_react_client";
const data = {
language: config.language,
// Url of the page with a typo
url: url,
// Typo text
text: this.state.text,
// Corrected variant (legacy name "comment")
comment: this.state.correct,
// TODO: context
context: "",
// This is a comment for a correction
note: this.state.comment,
}
try {
let result = await $.ajax({
method: "POST",
data: data,
url: serverUrl,
});
result = JSON.parse(result);
if (result.success === "true") {
return true;
}
console.error("sendRequest error:", result.message);
return false;
} catch (error) {
return false;
}
}
// Validate data and send request to the server
async submitTypo(corrected, comment) {
if (!this.checkData()) { if (!this.checkData()) {
alertify.error(i18n.messageFailture); alertify.error(i18n.messageFailture);
return; return;
} }
// Close window - user can continue working
this.handleClose(); this.handleClose();
alertify.success(i18n.messageSuccess);
return; // Send async request
this.sendRequest().then(success => {
if (success) {
alertify.success(i18n.messageSuccess);
return;
}
alertify.error(i18n.messageFailture);
});
} }
render() { render() {
...@@ -102,29 +162,29 @@ class TypoModal extends Component { ...@@ -102,29 +162,29 @@ class TypoModal extends Component {
<FormGroup className="es-typo-correct-fg"> <FormGroup className="es-typo-correct-fg">
<ControlLabel>{i18n.modalCorrectLabel}</ControlLabel> <ControlLabel>{i18n.modalCorrectLabel}</ControlLabel>
<FormControl <FormControl
type="text" type="text"
defaultValue={this.state.correct} defaultValue={this.state.correct}
onChange={this.onChangeCorrect} onChange={this.onChangeCorrect}
required /> required />
<HelpBlock>{i18n.modalCorrectHelp}</HelpBlock> <HelpBlock>{i18n.modalCorrectHelp}</HelpBlock>
<ControlLabel>{i18n.modalCommentLabel}</ControlLabel> <ControlLabel>{i18n.modalCommentLabel}</ControlLabel>
<FormControl <FormControl
type="text" type="text"
placeholder={i18n.modalCommentPlaceholder} placeholder={i18n.modalCommentPlaceholder}
onChange={this.onChangeComment}/> onChange={this.onChangeComment} />
</FormGroup> </FormGroup>
</form> </form>
{ this.state.error ? <Alert bsStyle="danger">{this.state.error}</Alert> : null } {this.state.error ? <Alert bsStyle="danger">{this.state.error}</Alert> : null}
</Modal.Body> </Modal.Body>
<Modal.Footer> <Modal.Footer>
<Button onClick={this.handleClose}>{i18n.close}</Button> <Button onClick={this.handleClose}>{i18n.close}</Button>
<Button onClick={this.submitTypo} bsStyle="primary">{i18n.saveChanges}</Button> <Button onClick={this.submitTypo} bsStyle="primary">{i18n.saveChanges}</Button>
</Modal.Footer> </Modal.Footer>
</Modal> </Modal>
); );
} }
......
...@@ -4,6 +4,10 @@ export const config = { ...@@ -4,6 +4,10 @@ export const config = {
maxCorrectLength: 100, maxCorrectLength: 100,
minCorrectLength: 1, minCorrectLength: 1,
serverUrl: "//eterfund.ru/api/typos/server.php", //serverUrl: "//eterfund.ru/api/typos/server.php",
serverUrl: "//ambulance.pubsandbox.eterhost.ru/typos.server/server.php",
requestTimeout: 10000, requestTimeout: 10000,
language: "ru",
}; };
\ 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