2016-07-29 19:35:48 +03:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
2017-12-26 04:03:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2016-07-29 19:35:48 +03:00
|
|
|
import sdk from '../../../index';
|
2017-07-12 15:58:14 +03:00
|
|
|
import Promise from 'bluebird';
|
2016-07-29 19:35:48 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A component which wraps an EditableText, with a spinner while updates take
|
|
|
|
* place.
|
|
|
|
*
|
|
|
|
* Parent components should supply an 'onSubmit' callback which returns a
|
|
|
|
* promise; a spinner is shown until the promise resolves.
|
|
|
|
*
|
|
|
|
* The parent can also supply a 'getIntialValue' callback, which works in a
|
|
|
|
* similarly asynchronous way. If this is not provided, the initial value is
|
|
|
|
* taken from the 'initialValue' property.
|
|
|
|
*/
|
|
|
|
export default class EditableTextContainer extends React.Component {
|
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this._unmounted = false;
|
|
|
|
this.state = {
|
|
|
|
busy: false,
|
|
|
|
errorString: null,
|
|
|
|
value: props.initialValue,
|
|
|
|
};
|
2016-08-01 12:49:51 +03:00
|
|
|
this._onValueChanged = this._onValueChanged.bind(this);
|
2016-07-29 19:35:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
|
|
|
if (this.props.getInitialValue === undefined) {
|
|
|
|
// use whatever was given in the initialValue property.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({busy: true});
|
|
|
|
|
2019-11-12 14:51:23 +03:00
|
|
|
this.props.getInitialValue().then(
|
2016-07-29 19:35:48 +03:00
|
|
|
(result) => {
|
|
|
|
if (this._unmounted) { return; }
|
|
|
|
this.setState({
|
|
|
|
busy: false,
|
|
|
|
value: result,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
if (this._unmounted) { return; }
|
|
|
|
this.setState({
|
|
|
|
errorString: error.toString(),
|
|
|
|
busy: false,
|
|
|
|
});
|
2017-10-11 19:56:17 +03:00
|
|
|
},
|
2016-07-29 19:35:48 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this._unmounted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_onValueChanged(value, shouldSubmit) {
|
|
|
|
if (!shouldSubmit) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
busy: true,
|
|
|
|
errorString: null,
|
|
|
|
});
|
|
|
|
|
2019-11-12 14:51:23 +03:00
|
|
|
this.props.onSubmit(value).then(
|
2016-07-29 19:35:48 +03:00
|
|
|
() => {
|
|
|
|
if (this._unmounted) { return; }
|
|
|
|
this.setState({
|
|
|
|
busy: false,
|
|
|
|
value: value,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
if (this._unmounted) { return; }
|
|
|
|
this.setState({
|
|
|
|
errorString: error.toString(),
|
|
|
|
busy: false,
|
|
|
|
});
|
2017-10-11 19:56:17 +03:00
|
|
|
},
|
2016-07-29 19:35:48 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (this.state.busy) {
|
2017-10-11 19:56:17 +03:00
|
|
|
const Loader = sdk.getComponent("elements.Spinner");
|
2016-07-29 19:35:48 +03:00
|
|
|
return (
|
|
|
|
<Loader />
|
|
|
|
);
|
|
|
|
} else if (this.state.errorString) {
|
|
|
|
return (
|
2017-10-11 19:56:17 +03:00
|
|
|
<div className="error">{ this.state.errorString }</div>
|
2016-07-29 19:35:48 +03:00
|
|
|
);
|
|
|
|
} else {
|
2017-10-11 19:56:17 +03:00
|
|
|
const EditableText = sdk.getComponent('elements.EditableText');
|
2016-07-29 19:35:48 +03:00
|
|
|
return (
|
|
|
|
<EditableText initialValue={this.state.value}
|
|
|
|
placeholder={this.props.placeholder}
|
|
|
|
onValueChanged={this._onValueChanged}
|
2017-03-02 20:29:06 +03:00
|
|
|
blurToSubmit={this.props.blurToSubmit}
|
2016-07-29 19:35:48 +03:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EditableTextContainer.propTypes = {
|
|
|
|
/* callback to retrieve the initial value. */
|
2017-12-26 04:03:18 +03:00
|
|
|
getInitialValue: PropTypes.func,
|
2016-07-29 19:35:48 +03:00
|
|
|
|
|
|
|
/* initial value; used if getInitialValue is not given */
|
2017-12-26 04:03:18 +03:00
|
|
|
initialValue: PropTypes.string,
|
2016-07-29 19:35:48 +03:00
|
|
|
|
|
|
|
/* placeholder text to use when the value is empty (and not being
|
|
|
|
* edited) */
|
2017-12-26 04:03:18 +03:00
|
|
|
placeholder: PropTypes.string,
|
2016-07-29 19:35:48 +03:00
|
|
|
|
|
|
|
/* callback to update the value. Called with a single argument: the new
|
|
|
|
* value. */
|
2017-12-26 04:03:18 +03:00
|
|
|
onSubmit: PropTypes.func,
|
2017-03-02 20:29:06 +03:00
|
|
|
|
|
|
|
/* should the input submit when focus is lost? */
|
2017-12-26 04:03:18 +03:00
|
|
|
blurToSubmit: PropTypes.bool,
|
2016-07-29 19:35:48 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
EditableTextContainer.defaultProps = {
|
|
|
|
initialValue: "",
|
|
|
|
placeholder: "",
|
2017-03-02 20:29:06 +03:00
|
|
|
blurToSubmit: false,
|
2017-07-12 16:02:00 +03:00
|
|
|
onSubmit: function(v) {return Promise.resolve(); },
|
2016-07-29 19:35:48 +03:00
|
|
|
};
|