Explorar o código

Hookup the things edit

Johannes Zellner %!s(int64=11) %!d(string=hai) anos
pai
achega
920f70758a
Modificáronse 8 ficheiros con 138 adicións e 73 borrados
  1. 14 6
      src/js/Application.js
  2. 0 16
      src/js/BootstrapButton.js
  3. 7 10
      src/js/BootstrapModal.js
  4. 0 27
      src/js/NewThing.js
  5. 16 11
      src/js/Thing.js
  6. 12 2
      src/js/ThingList.js
  7. 77 0
      src/js/ThingModal.js
  8. 12 1
      src/js/core.js

+ 14 - 6
src/js/Application.js

@@ -2,19 +2,27 @@ var React = require('react'),
     ReactDOM = require('react-dom'),
     classNames = require('classnames'),
     Core = require('./core.js').Core,
-    NewThing = require('./NewThing.js'),
+    ThingModal = require('./ThingModal.js'),
     ThingList = require('./ThingList.js'),
-    BootstrapButton = require('./BootstrapButton.js'),
     SearchBox = require('./SearchBox.js');
 
 var Application = React.createClass({
-  openNewThingModal: function () {
+  handleThingAdd: function () {
     this.refs.modalAdd.openModal();
   },
+  handleThingEdit: function (thing) {
+    this.refs.modalAdd.openModal(thing);
+  },
+  handleThingDelete: function (thing) {
+    console.log('del', thing)
+  },
   getInitialState: function () {
     return { data: [], busy: false };
   },
   componentDidMount: function () {
+    this.refresh();
+  },
+  refresh: function () {
     var that = this;
 
     that.setState({ data: [], busy: true });
@@ -25,16 +33,16 @@ var Application = React.createClass({
     });
   },
   render: function () {
-    var things = this.state.busy ? <center><i className="spinner"></i></center> : <ThingList data={this.state.data}/>;
+    var things = this.state.busy ? <center><i className="spinner"></i></center> : <ThingList data={this.state.data} onEdit={this.handleThingEdit} onDelete={this.handleThingDelete}/>;
 
     return (
       <div>
-        <NewThing ref="modalAdd"/>
+        <ThingModal ref="modalAdd" onAdded={this.refresh} onUpdated={this.refresh}/>
         <div className="container">
           <div className="row">
             <div className="col-md-4"></div>
             <div className="col-md-4">
-            <BootstrapButton className="btn-success btn-block" onClick={this.openNewThingModal}>Add</BootstrapButton>
+              <button role="button" className="btn btn-success btn-block" onClick={this.handleThingAdd}>Add</button>
             </div>
             <div className="col-md-4"></div>
           </div>

+ 0 - 16
src/js/BootstrapButton.js

@@ -1,16 +0,0 @@
-var React = require('react'),
-    ReactDOM = require('react-dom');
-
-var BootstrapButton = React.createClass({
-  render: function () {
-    var classNames = 'btn ' + (this.props.className ? this.props.className : '');
-
-    return (
-      <a {...this.props} href="javascript:;" role="button" className={classNames}>
-        {this.props.children}
-      </a>
-    );
-  }
-});
-
-module.exports = BootstrapButton;

+ 7 - 10
src/js/BootstrapModal.js

@@ -1,6 +1,5 @@
 var React = require('react'),
-    ReactDOM = require('react-dom'),
-    BootstrapButton = require('./BootstrapButton.js');
+    ReactDOM = require('react-dom');
 
 var BootstrapModal = React.createClass({
   componentDidMount: function () {
@@ -27,19 +26,13 @@ var BootstrapModal = React.createClass({
 
     if (this.props.confirm) {
       confirmButton = (
-        <BootstrapButton
-          onClick={this.handleConfirm}
-          className="btn-primary">
-          {this.props.confirm}
-        </BootstrapButton>
+        <button role="button" className="btn btn-success" onClick={this.handleConfirm} disabled={this.props.busy}>{this.props.confirm}</button>
       );
     }
 
     if (this.props.cancel) {
       cancelButton = (
-        <BootstrapButton onClick={this.handleCancel}>
-          {this.props.cancel}
-        </BootstrapButton>
+        <button role="button" className="btn" onClick={this.handleCancel} disabled={this.props.busy}>{this.props.cancel}</button>
       );
     }
 
@@ -69,11 +62,15 @@ var BootstrapModal = React.createClass({
     );
   },
   handleCancel: function() {
+    if (this.props.busy) return;
+
     if (this.props.onCancel) {
       this.props.onCancel();
     }
   },
   handleConfirm: function() {
+    if (this.props.busy) return;
+
     if (this.props.onConfirm) {
       this.props.onConfirm();
     }

+ 0 - 27
src/js/NewThing.js

@@ -1,27 +0,0 @@
-var React = require('react'),
-    ReactDOM = require('react-dom'),
-    BootstrapModal = require('./BootstrapModal.js');
-
-var NewThing = React.createClass({
-  componentDidMount: function () {
-    this.refs.modal.setFocusElement(this.refs.textarea);
-  },
-  handleCancel: function () {
-    this.refs.modal.close();
-  },
-  render: function () {
-    return (
-      <BootstrapModal ref="modal" confirm="OK" cancel="Cancel" onCancel={this.handleCancel} onConfirm={this.closeModal} title="New Thing">
-        <textarea ref="textarea" className="form-control" id="inputContent" rows="5"/>
-      </BootstrapModal>
-    );
-  },
-  openModal: function() {
-    this.refs.modal.open();
-  },
-  closeModal: function() {
-    this.refs.modal.close();
-  }
-});
-
-module.exports = NewThing;

+ 16 - 11
src/js/Thing.js

@@ -1,31 +1,36 @@
 var React = require('react'),
     ReactDOM = require('react-dom'),
     marked = require('marked'),
-    classNames = require('classnames');
+    core = require('./core.js');
 
 var NewThing = React.createClass({
   propTypes: {
-    createdAt: React.PropTypes.number,
-    tags: React.PropTypes.arrayOf(React.PropTypes.string),
-    content: React.PropTypes.string,
-    richContent: React.PropTypes.string
+    thing: React.PropTypes.instanceOf(core.Thing),
+    onEdit: React.PropTypes.func,
+    onDelete: React.PropTypes.func
   },
   rawMarkup: function() {
-    var rawMarkup = marked(this.props.richContent, { sanitize: true });
+    var rawMarkup = marked(this.props.thing.richContent, { sanitize: true });
     return { __html: rawMarkup };
   },
   render: function () {
-    var dateString = (new Date(this.props.createdAt)).toISOString();
+    var dateString = (new Date(this.props.thing.createdAt)).toISOString();
     return (
-      <div className={classNames('well', 'well-sm', 'thing')}>
-        <div className={classNames('col-md-12', 'thing-title-date')}>
+      <div className="well well-sm thing">
+        <div className="col-md-12 thing-title-date">
           {dateString}
-          <button className={classNames('btn', 'btn-xs', 'btn-default')}>delete</button>
-          <button className={classNames('btn', 'btn-xs', 'btn-default')}>edit</button>
+          <button className="btn btn-xs btn-default" onClick={this.handleEdit}>edit</button>
+          <button className="btn btn-xs btn-default" onClick={this.handleDelete}>delete</button>
         </div>
         <div className="content" dangerouslySetInnerHTML={this.rawMarkup()} />
       </div>
     );
+  },
+  handleEdit: function () {
+    if (this.props.onEdit) this.props.onEdit(this.props.thing);
+  },
+  handleDelete: function () {
+    if (this.props.onDelete) this.props.onDelete(this.props.thing);
   }
 });
 

+ 12 - 2
src/js/ThingList.js

@@ -5,12 +5,16 @@ var React = require('react'),
 
 var NewThing = React.createClass({
   propTypes: {
-    data: React.PropTypes.arrayOf(React.PropTypes.instanceOf(core.Thing))
+    data: React.PropTypes.arrayOf(React.PropTypes.instanceOf(core.Thing)),
+    onEdit: React.PropTypes.func,
+    onDelete: React.PropTypes.func
   },
   render: function() {
+    var that = this;
+
     var thingNodes = this.props.data.map(function (thing) {
       return (
-        <Thing key={thing.id} createdAt={thing.createdAt} tags={thing.tags} content={thing.content} richContent={thing.richContent} />
+        <Thing key={thing.id} thing={thing} onEdit={that.handleEdit} onDelete={that.handleDelete} />
       );
     });
 
@@ -19,6 +23,12 @@ var NewThing = React.createClass({
         {thingNodes}
       </div>
     );
+  },
+  handleEdit: function (thing) {
+    if (this.props.onEdit) this.props.onEdit(thing);
+  },
+  handleDelete: function (thing) {
+    if (this.props.onDelete) this.props.onDelete(thing);
   }
 });
 

+ 77 - 0
src/js/ThingModal.js

@@ -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;

+ 12 - 1
src/js/core.js

@@ -34,7 +34,18 @@ Core.prototype.thingsGet = function (filter, callback) {
 Core.prototype.thingsAdd = function (thing, callback) {
     var url = this._server + '/api/things';
 
-    superagent.post(url).send(thing).end(function (error, result) {
+    superagent.post(url).send({ content: thing.content }).end(function (error, result) {
+        if (error) return callback(error);
+        if (result.status !== 201) return callback(new Error('Failed: ' + result.status + '. ' + result.text));
+
+        callback(null);
+    });
+};
+
+Core.prototype.thingsUpdate = function (thing, callback) {
+    var url = this._server + '/api/things/' + thing.id;
+
+    superagent.put(url).send({ content: thing.content }).end(function (error, result) {
         if (error) return callback(error);
         if (result.status !== 201) return callback(new Error('Failed: ' + result.status + '. ' + result.text));