|
|
@@ -0,0 +1,77 @@
|
|
|
+var React = require('react'),
|
|
|
+ ReactDOM = require('react-dom'),
|
|
|
+ Core = require('./core.js').Core,
|
|
|
+ Thing = require('./core.js').Thing,
|
|
|
+ BootstrapModal = require('./BootstrapModal.js');
|
|
|
+
|
|
|
+var ThingModal = React.createClass({
|
|
|
+ propTypes: {
|
|
|
+ onAdded: React.PropTypes.func,
|
|
|
+ onUpdated: React.PropTypes.func
|
|
|
+ },
|
|
|
+ getInitialState: function () {
|
|
|
+ return { busy: false, thing: null, content: '' };
|
|
|
+ },
|
|
|
+ handleContentChange: function (e) {
|
|
|
+ this.setState({ content: e.target.value });
|
|
|
+ },
|
|
|
+ componentDidMount: function () {
|
|
|
+ this.refs.modal.setFocusElement(this.refs.textarea);
|
|
|
+ },
|
|
|
+ handleCancel: function () {
|
|
|
+ this.refs.modal.close();
|
|
|
+ },
|
|
|
+ handleConfirm: function () {
|
|
|
+ var that = this;
|
|
|
+
|
|
|
+ this.setState({ busy: true });
|
|
|
+
|
|
|
+ function done() {
|
|
|
+ that.refs.modal.close();
|
|
|
+
|
|
|
+ setTimeout(function () {
|
|
|
+ that.setState({ busy: false, content: '', thing: null });
|
|
|
+ }, 2000);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (this.state.thing) {
|
|
|
+ this.state.thing.content = this.state.content;
|
|
|
+
|
|
|
+ Core.thingsUpdate(this.state.thing, function (error) {
|
|
|
+ if (error) return console.error(error);
|
|
|
+
|
|
|
+ if (that.props.onUpdated) that.props.onUpdated();
|
|
|
+
|
|
|
+ done();
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ Core.thingsAdd(new Thing('', Date.now(), [], this.state.content, ''), function (error) {
|
|
|
+ if (error) return console.error(error);
|
|
|
+
|
|
|
+ if (that.props.onAdded) that.props.onAdded();
|
|
|
+
|
|
|
+ done();
|
|
|
+ });
|
|
|
+ }
|
|
|
+ },
|
|
|
+ render: function () {
|
|
|
+ var content = this.state.busy ? <center><i className="spinner"></i></center> : <textarea ref="textarea" className="form-control" value={this.state.content} onChange={this.handleContentChange} rows="5"/>;
|
|
|
+ var title = this.state.thing ? 'Edit Thing' : 'Add thing';
|
|
|
+ var action = this.state.thing ? 'Save' : 'Add';
|
|
|
+
|
|
|
+ return (
|
|
|
+ <BootstrapModal ref="modal" confirm={action} cancel="Cancel" busy={this.state.busy} onCancel={this.handleCancel} onConfirm={this.handleConfirm} title={title}>
|
|
|
+ {content}
|
|
|
+ </BootstrapModal>
|
|
|
+ );
|
|
|
+ },
|
|
|
+ openModal: function (thing) {
|
|
|
+ this.setState({ busy: false, content: thing ? thing.content : '', thing: thing || null });
|
|
|
+ this.refs.modal.open();
|
|
|
+ },
|
|
|
+ closeModal: function () {
|
|
|
+ this.refs.modal.close();
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+module.exports = ThingModal;
|