From 1815380f3f5890c92e04640c536828d434b06db2 Mon Sep 17 00:00:00 2001 From: igoradamenko Date: Fri, 18 May 2018 19:24:27 +0300 Subject: [PATCH] add editing support to input component --- web/app/components/input/input.jsx | 60 +++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/web/app/components/input/input.jsx b/web/app/components/input/input.jsx index 1d4444a4..99d4b5d4 100644 --- a/web/app/components/input/input.jsx +++ b/web/app/components/input/input.jsx @@ -29,11 +29,17 @@ export default class Input extends Component { } componentDidMount() { + const { mods = {} } = this.props; + if (this.props.autoFocus) { this.fieldNode.focus(); } - this.fieldNode.value = ''; + if (mods.mode !== 'edit') { + this.fieldNode.value = ''; + } else { + this.autoResize(); + } store.onUpdate('config', config => { this.setState({ maxLength: config && config.max_comment_size || DEFAULT_MAX_COMMENT_SIZE }); @@ -69,7 +75,7 @@ export default class Input extends Component { send(e) { const text = this.fieldNode.value; - const pid = this.props.pid; + const { mods = {}, pid, id } = this.props; if (e) e.preventDefault(); @@ -77,21 +83,38 @@ export default class Input extends Component { this.setState({ isFieldDisabled: true, isErrorShown: false }); - api.send({ text, ...(pid ? { pid } : {}) }) - .then(({ id }) => { - // TODO: maybe we should run onsubmit before send; like in optimistic ui - if (this.props.onSubmit) { - this.props.onSubmit({ text, id, pid }); - } + if (mods.mode === 'edit') { + api.edit({ text }) + .then(() => { + if (this.props.onSubmit) { + this.props.onSubmit({ id }); + } - this.fieldNode.value = ''; - this.fieldNode.style.height = ''; - this.setState({ preview: null }); - }) - .catch(() => { - this.setState({ isErrorShown: true }); - }) - .finally(() => this.setState({ isFieldDisabled: false })); + this.fieldNode.value = ''; + this.fieldNode.style.height = ''; + this.setState({ preview: null }); + }) + .catch(() => { + this.setState({ isErrorShown: true }); + }) + .finally(() => this.setState({ isFieldDisabled: false })); + } else { + api.send({ text, ...(pid ? { pid } : {}) }) + .then(({ id }) => { + // TODO: maybe we should run onsubmit before send; like in optimistic ui + if (this.props.onSubmit) { + this.props.onSubmit({ text, id, pid }); + } + + this.fieldNode.value = ''; + this.fieldNode.style.height = ''; + this.setState({ preview: null }); + }) + .catch(() => { + this.setState({ isErrorShown: true }); + }) + .finally(() => this.setState({ isFieldDisabled: false })); + } } getPreview() { @@ -110,7 +133,9 @@ export default class Input extends Component { render(props, { isFieldDisabled, isErrorShown, preview, maxLength, commentLength }) { const charactersLeft = maxLength - commentLength; - const { mods = {} } = props; + const { mods = {}, value = null } = props; + + console.log(value) return (
@@ -118,6 +143,7 @@ export default class Input extends Component {