forked from krambertech/react-essential-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.js
142 lines (125 loc) · 3.73 KB
/
notes.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
var Note = React.createClass({
render: function() {
var style = { backgroundColor: this.props.color };
return (
<div className="note" style={style}>
<span className="delete-note" onClick={this.props.onDelete}> × </span>
{this.props.children}
</div>
);
}
});
var NoteEditor = React.createClass({
getInitialState: function() {
return {
text: ''
};
},
handleTextChange: function(event) {
this.setState({ text: event.target.value });
},
handleNoteAdd: function() {
var newNote = {
text: this.state.text,
color: 'yellow',
id: Date.now()
};
this.props.onNoteAdd(newNote);
this.setState({ text: '' });
},
render: function() {
return (
<div className="note-editor">
<textarea
placeholder="Enter your note here..."
rows={5}
className="textarea"
value={this.state.text}
onChange={this.handleTextChange}
/>
<button className="add-button" onClick={this.handleNoteAdd}>Add</button>
</div>
);
}
});
var NotesGrid = React.createClass({
componentDidMount: function() {
var grid = this.refs.grid;
this.msnry = new Masonry( grid, {
itemSelector: '.note',
columnWidth: 200,
gutter: 10,
isFitWidth: true
});
},
componentDidUpdate: function(prevProps) {
if (this.props.notes.length !== prevProps.notes.length) {
this.msnry.reloadItems();
this.msnry.layout();
}
},
render: function() {
var onNoteDelete = this.props.onNoteDelete;
return (
<div className="notes-grid" ref="grid">
{
this.props.notes.map(function(note){
return (
<Note
key={note.id}
onDelete={onNoteDelete.bind(null, note)}
color={note.color}>
{note.text}
</Note>
);
})
}
</div>
);
}
});
var NotesApp = React.createClass({
getInitialState: function() {
return {
notes: []
};
},
componentDidMount: function() {
var localNotes = JSON.parse(localStorage.getItem('notes'));
if (localNotes) {
this.setState({ notes: localNotes });
}
},
componentDidUpdate: function() {
this._updateLocalStorage();
},
handleNoteDelete: function(note) {
var noteId = note.id;
var newNotes = this.state.notes.filter(function(note) {
return note.id !== noteId;
});
this.setState({ notes: newNotes });
},
handleNoteAdd: function(newNote) {
var newNotes = this.state.notes.slice();
newNotes.unshift(newNote);
this.setState({ notes: newNotes });
},
render: function() {
return (
<div className="notes-app">
<h2 className="app-header">NotesApp</h2>
<NoteEditor onNoteAdd={this.handleNoteAdd} />
<NotesGrid notes={this.state.notes} onNoteDelete={this.handleNoteDelete} />
</div>
);
},
_updateLocalStorage: function() {
var notes = JSON.stringify(this.state.notes);
localStorage.setItem('notes', notes);
}
});
ReactDOM.render(
<NotesApp />,
document.getElementById('mount-point')
);