forked from davidchang/yo-in-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coreComponents.js
165 lines (149 loc) · 4.55 KB
/
coreComponents.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/** @jsx React.DOM */
var baseUrl = 'https://yo-in-react.firebaseio.com';
// USER DISPLAY COMPONENT (to display username)
var UserDisplay = React.createClass({
render : function() {
return (
<section>
<div className="panel panel-default">
<div className="panel-heading">Username</div>
<div className="panel-body">
<strong>{this.props.user.name}</strong>
</div>
</div>
</section>
);
}
});
// YO DISPLAY COMPONENT (to display yo count and received yos)
var YoDisplay = React.createClass({
mixins : [ReactFireMixin],
componentWillMount : function() {
this.bindAsArray(new Firebase(baseUrl + '/users/' + this.props.name + '/notifications'), 'notifications');
},
getInitialState : function() {
return {
notifications : []
};
},
render : function() {
var notifications = '';
if (this.state.notifications.length) {
notifications = (
<div className="panel panel-default">
<div className="panel-heading">Yos</div>
<div className="panel-body">
<ul className="list-unstyled">{this.state.notifications.slice().reverse().map(function(notification) {
var timestamp = new Date(notification.timestamp).toString();
return <li>{notification.from} on {timestamp}</li>;
})}</ul>
</div>
</div>
);
}
return (
<section>
<div className="panel panel-default">
<div className="panel-heading">Yo Count</div>
<div className="panel-body">
{this.props.user.yoCount || 0}
</div>
</div>
{notifications}
</section>
);
}
});
// PEOPLE TO YO COMPONENT (to display list of people you can yo)
var PeopleToYo = React.createClass({
mixins : [ReactFireMixin],
componentWillMount : function() {
this.bindAsArray(new Firebase(baseUrl + '/users/' + this.props.name + '/yoList'), 'yoList');
},
getInitialState : function() {
return {
newPerson : '',
yoList : [],
showError : false
};
},
_sendYo : function(personToYo) {
var $this = this;
var yoRecipient = new Firebase(baseUrl + '/users/' + personToYo);
yoRecipient.once('value', function(data) {
if (!data.val()) {
return;
}
yoRecipient.child('yoCount').set((data.val().yoCount + 1) || 1);
yoRecipient.child('notifications').push({
from : $this.props.name,
timestamp : new Date().getTime()
});
});
},
_handleChange : function(event) {
this.setState({
showError : false,
newPerson : event.target.value
});
},
_addPerson : function(e) {
e.preventDefault();
if (!this.state.newPerson.trim()) {
return;
}
var $this = this;
var newPersonRef = new Firebase(baseUrl + '/users/' + this.state.newPerson.trim());
newPersonRef.once('value', function(data) {
if (!data.val()) {
$this.setState({
showError : true
});
return;
}
$this.firebaseRefs['yoList'].push({
name: $this.state.newPerson.trim()
});
$this.setState({
showError : false,
newPerson : ''
});
});
},
render : function() {
var errorHtml = '';
if (this.state.showError) {
errorHtml = <div className="alert alert-danger" role="alert">That person does not exist!</div>;
}
var usersToYoHtml = '';
if (this.state.yoList.length) {
usersToYoHtml = (
<div className="panel panel-default">
<div className="panel-heading">Users To Yo</div>
<div className="panel-body">
<ul className="list-unstyled">{this.state.yoList.map(function(person) {
return <li>{person.name} <button className="btn btn-primary" onClick={this._sendYo.bind(this, person.name)}>Yo</button></li>;
}, this)}</ul>
</div>
</div>
);
}
return (
<section>
{usersToYoHtml}
<div className="panel panel-default">
<div className="panel-heading">Add New User</div>
<div className="panel-body">
<form role="form">
{errorHtml}
<div className="form-group">
<input type="text" className="form-control text-center" placeholder="Twitter Handle" value={this.state.newPerson} onChange={this._handleChange} />
</div>
<button type="submit" onClick={this._addPerson} className="btn btn-default">Add to Yo List</button>
</form>
</div>
</div>
</section>
);
}
});