-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.jsx
116 lines (101 loc) · 3.19 KB
/
app.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// App React component - represents the whole app
App = React.createClass ({
// This mixin makes the getMeteorData method work
mixins: [ReactMeteorData],
// Defines a state
getInitialState() {
return {
hideCompleted: false
}
},
// Loads items from the Tasks collection and puts them on this.data.tasks
getMeteorData() {
let query = {};
// Filters out completed tasks
if (this.state.hideCompleted) {
// If hide completed is checked, filter tasks
query = {checked: {$ne: true}};
}
/**
* Returns tasks sorted, task's count and incomplete count of tasks
* Also returns the currently logged in user
*/
return {
tasks: Tasks.find(query, {sort: {createdAt: -1}}).fetch(),
tasksCount: Tasks.find().count(),
incompleteCount: Tasks.find({checked:{$ne: true}}).count(),
currentUser: Meteor.user()
};
},
// Renders the tasks
renderTasks() {
// Gets tasks from this.data.tasks
return this.data.tasks.map((task) => {
const currentUserId = this.data.currentUser && this.data.currentUser._id;
const showPrivateButton = task.owner == currentUserId;
const showDeleteButton = task.owner == currentUserId;
return <Task
key={task._id}
task={task}
showPrivateButton={showPrivateButton}
showDeleteButton={showDeleteButton}
/>;
});
},
handleSubmit(event) {
event.preventDefault();
// Finds the text field via the React ref
var text = ReactDOM.findDOMNode(this.refs.textInput).value.trim();
// Calls addTack method
Meteor.call('addTask', text);
// Clears form
ReactDOM.findDOMNode(this.refs.textInput).value = '';
},
/**
* Updates asynchronously the state property of this.state from an event
* handler by calling this.setState and then cause the component to re-render
*/
toggleHideCompleted() {
this.setState ({
hideCompleted: ! this.state.hideCompleted
});
},
// Renders the whole app
render() {
return (
/**
* Returns a div with the class container
* Has a title with a counter of incomplete tasks and all tasks
* A checkbox for hide or show completed tasks
* A account UI for sign in, sign up, change password and sign out
* A input form to insert a new task but only when there is a logged in
* user and finally a list of the tasks
*/
<div className='container'>
<header>
<h1>Todo ({this.data.incompleteCount}/{this.data.tasksCount})</h1>
<label className='hide-completed'>
<input
type='checkbox'
readOnly={true}
checked={this.state.hideCompleted}
onClick={this.toggleHideCompleted} />
Hide Completed Tasks
</label>
<AccountsUIWrapper />
{ this.data.currentUser ?
<form className='new-task' onSubmit={this.handleSubmit} >
<input
type='text'
ref='textInput'
placeholder='Type to add new Tasks' />
</form> : ''
}
</header>
<ul>
{this.renderTasks()}
</ul>
</div>
);
}
});