improve error handling in input

This commit is contained in:
igoradamenko
2018-05-11 21:39:29 +03:00
parent 088082508e
commit d64138897d
3 changed files with 32 additions and 8 deletions
@@ -0,0 +1,9 @@
.input__error {
margin: 0;
padding: 10px 12px;
font-size: 14px;
line-height: 18px;
border-top: 8px solid #eee;
background: #ffd7d7;
color: #9a0000;
}
+1
View File
@@ -9,5 +9,6 @@ require('./__button/_type/_preview/input__button_type_preview.scss');
require('./__button/_type/_send/input__button_type_send.scss');
require('./__buttons/input__buttons.scss');
require('./__error/input__error.scss');
require('./__field/input__field.scss');
require('./__preview/input__preview.scss');
+22 -8
View File
@@ -8,11 +8,12 @@ export default class Input extends Component {
this.state = {
preview: null,
isErrorShown: false,
};
this.autoResize = this.autoResize.bind(this);
this.send = this.send.bind(this);
this.getPreview = this.getPreview.bind(this);
this.onInput = this.onInput.bind(this);
this.onKeyDown = this.onKeyDown.bind(this);
}
@@ -36,11 +37,18 @@ export default class Input extends Component {
}
}
onInput() {
this.autoResize();
this.setState({
preview: null,
isErrorShown: false,
});
}
autoResize() {
this.fieldNode.style.height = '';
this.fieldNode.style.height = `${this.fieldNode.scrollHeight}px`;
this.setState({ preview: null });
}
send(e) {
@@ -51,7 +59,7 @@ export default class Input extends Component {
if (!text || !text.trim()) return;
this.setState({ isFieldDisabled: true });
this.setState({ isFieldDisabled: true, isErrorShown: false });
api.send({ text, ...(pid ? { pid } : {}) })
.then(({ id }) => {
@@ -65,7 +73,7 @@ export default class Input extends Component {
this.setState({ preview: null });
})
.catch(() => {
// TODO: do smth?
this.setState({ isErrorShown: true });
})
.finally(() => this.setState({ isFieldDisabled: false }));
}
@@ -75,20 +83,22 @@ export default class Input extends Component {
if (!text || !text.trim()) return;
this.setState({ isErrorShown: false });
api.getPreview({ text })
.then(preview => this.setState({ preview }))
.catch(() => {
// TODO: do smth?
this.setState({ isErrorShown: true });
});
}
render(props, { isFieldDisabled, preview }) {
render(props, { isFieldDisabled, isErrorShown, preview }) {
return (
<form className={b('input', props)} onSubmit={this.send}>
<textarea
className="input__field"
placeholder="Your comment here"
onInput={this.autoResize}
onInput={this.onInput}
onKeyDown={this.onKeyDown}
ref={r => (this.fieldNode = r)}
required
@@ -97,6 +107,10 @@ export default class Input extends Component {
{props.children}
</textarea>
{
isErrorShown && <p className="input__error">Something went wrong. Please try again a bit later.</p>
}
<div className="input__buttons">
<button
className={b('input__button', {}, { type: 'preview' })}