App.jsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. return this.data.tasks.map((task) => {
  30. // Meteor._debug(task._id);
  31. return <Task key={task._id} task={task} />;
  32. });
  33. },
  34. handleSubmit(event) {
  35. event.preventDefault();
  36. // Meteor._debug('refs', this.refs);
  37. // Find the text field via the React ref.
  38. var text = ReactDOM.findDOMNode(this.refs.textInput).value.trim();
  39. Meteor.call("addTask", text);
  40. // Clear form to allow a new input.
  41. ReactDOM.findDOMNode(this.refs.textInput).value = '';
  42. },
  43. toggleHideCompleted() {
  44. this.setState({
  45. hideCompleted: !this.state.hideCompleted
  46. });
  47. },
  48. render() {
  49. return (
  50. <div className="container">
  51. <header>
  52. <h1>Todo list ({this.data.incompleteCount})</h1>
  53. <label className="hide-completed">
  54. <input type="checkbox" readOnly={true}
  55. checked={this.state.hideCompleted}
  56. onClick={this.toggleHideCompleted} />
  57. Hide completed
  58. </label>
  59. <AccountsUIWrapper />
  60. {/* These are JSX comments. */}
  61. { this.data.currentUser ?
  62. // Beware: for React, onsubmit is not the same as onSubmit, the former doesn't work.
  63. <form className="new-task" onSubmit={this.handleSubmit} >
  64. <input type="text" ref="textInput" placeholder="Type to add new tasks" />
  65. </form> : ''
  66. }
  67. </header>
  68. <ul>
  69. {this.renderTasks()}
  70. </ul>
  71. </div>
  72. );
  73. }
  74. });