App.jsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // App component : represents the whole app.
  2. App = React.createClass({
  3. // This mixin makes the getMeteorData work.
  4. mixins: [ReactMeteorData],
  5. getInitialState() {
  6. return {
  7. hideCompleted: false
  8. };
  9. },
  10. // Loads items from the Tasks collection and puts them on this.data.tasks.
  11. getMeteorData() {
  12. let query = {};
  13. const uncheckedQuery = { checked: { $ne: true} };
  14. if (this.state.hideCompleted) {
  15. // If hideCompleted is checked, filter tasks.
  16. query = uncheckedQuery;
  17. }
  18. let result = {
  19. tasks: Tasks.find(query, { sort: { createdAt: -1 }}).fetch(),
  20. // Since we already have the data in the client-side Minimongo collection,
  21. // adding this extra count doesn't involve asking the server for anything.
  22. incompleteCount: Tasks.find(uncheckedQuery).count(),
  23. currentUser: Meteor.user()
  24. };
  25. // Meteor._debug("result", result);
  26. return result;
  27. },
  28. renderTasks() {
  29. // Get tasks from this.data.tasks.
  30. return this.data.tasks.map((task) => {
  31. const currentUserId = this.data.currentUser && this.data.currentUser._id;
  32. const showPrivateButton = task.owner === currentUserId;
  33. // Meteor._debug(task._id);
  34. return <Task
  35. key={task._id}
  36. task={task}
  37. showPrivateButton={showPrivateButton}
  38. />;
  39. });
  40. },
  41. handleSubmit(event) {
  42. event.preventDefault();
  43. // Meteor._debug('refs', this.refs);
  44. // Find the text field via the React ref.
  45. var text = ReactDOM.findDOMNode(this.refs.textInput).value.trim();
  46. Meteor.call("addTask", text);
  47. // Clear form to allow a new input.
  48. ReactDOM.findDOMNode(this.refs.textInput).value = '';
  49. },
  50. toggleHideCompleted() {
  51. this.setState({
  52. hideCompleted: !this.state.hideCompleted
  53. });
  54. },
  55. render() {
  56. return (
  57. <div className="container">
  58. <header>
  59. <h1>Todo list ({this.data.incompleteCount})</h1>
  60. <label className="hide-completed">
  61. <input type="checkbox" readOnly={true}
  62. checked={this.state.hideCompleted}
  63. onClick={this.toggleHideCompleted} />
  64. Hide completed
  65. </label>
  66. <AccountsUIWrapper />
  67. {/* These are JSX comments. */}
  68. { this.data.currentUser ?
  69. // Beware: for React, onsubmit is not the same as onSubmit, the former doesn't work.
  70. <form className="new-task" onSubmit={this.handleSubmit} >
  71. <input type="text" ref="textInput" placeholder="Type to add new tasks" />
  72. </form> : ''
  73. }
  74. </header>
  75. <ul>
  76. {this.renderTasks()}
  77. </ul>
  78. </div>
  79. );
  80. }
  81. });