-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.jsx
132 lines (112 loc) · 3.45 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
117
118
119
120
121
122
123
124
125
126
127
128
// Include the react module in our file so we can reference it's methods.
// because it was listed as a dependency in our package.json file, when we run npm install these libraries are added to our node modules folder.
var React = require('react');
var ReactDOM = require('react-dom');
var AutosizeInput = require('react-input-autosize');
var ReactFireMixin = require('reactfire');
var Catalyst = require('react-catalyst');
// this creates our first react component. All you need to make it go is to write a render function that renders some JSX.
var App = React.createClass({
mixins: [ReactFireMixin],
getInitialState: function() {
return {
items: [],
text: '',
name: 'enter name',
editName: false,
};
},
componentWillMount: function() {
var firebaseRef = firebase.database().ref('todoApp/items');;
this.bindAsArray(firebaseRef.limitToLast(20), 'items');
var localStorageRef = localStorage.getItem('name');
if(localStorageRef) {
// update our component state to reflect what is in localStorage
this.setState({
name : localStorageRef
});
}
},
onChange: function(e) {
this.setState({text: e.target.value});
},
removeItem: function(key) {
var firebaseRef = firebase.database().ref('todoApp/items');;
firebaseRef.child(key).remove();
},
handleSubmit: function(e) {
e.preventDefault();
if (this.state.text && this.state.text.trim().length !== 0) {
this.firebaseRefs['items'].push({
text: this.state.text
});
this.setState({
text: ''
});
}
},
updateInputValue (input, event) {
var newState = {};
newState[input] = event.target.value;
this.setState(newState);
},
updateName:function(e){
if(e.which === 13) {
localStorage.setItem('name',this.state.name);
this.setState({
editName: false
});
}
},
showUpdate: function() {
this.setState({
editName: true
});
},
render: function() {
var displayName = (
<span className="username" onClick={this.showUpdate} onTouchStart={this.showUpdate}>{this.state.name}</span>
);
if(this.state.editName) {
displayName = (
<AutosizeInput
value={this.state.name}
onChange={this.updateInputValue.bind(this, 'name')}
onKeyPress={this.updateName}
style={{padding: 5 }}
inputStyle={{ background: 'transparent', border: 0, borderBottom: '3px solid #FFF', padding:'0 15px', outline: 'none', color: '#FFF'}}
/>
);
}
return (
<div className="app">
<h1 className="welcome">Welcome, {displayName}!</h1>
<div className="content">
<form onSubmit={ this.handleSubmit }>
<h1><i className="fa fa-star" aria-hidden="true"></i> What do you need to do today?</h1>
<input onChange={ this.onChange } value={ this.state.text } />
</form>
<TodoList items={ this.state.items } removeItem={ this.removeItem } />
</div>
</div>
);
}
});
var TodoList = React.createClass({
render: function() {
var _this = this;
var createItem = function(item, index) {
return (
<li key={index} >
{ item.text }
<span onClick={ _this.props.removeItem.bind(null, item['.key']) }
className="delete">
x
</span>
</li>
);
};
return <ul>{ this.props.items.map(createItem) }</ul>;
}
});
ReactDOM.render(<App />, document.getElementById('app'));