Skip to content

Commit

Permalink
add alt to TODO list app.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chow Hui Ling committed Jan 3, 2016
1 parent fb32869 commit e9ae541
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 34 deletions.
52 changes: 20 additions & 32 deletions app/components/App.jsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import React from 'react';
import Notes from './Notes.jsx';
import NoteActions from '../actions/NoteActions';
import NoteStore from '../stores/NoteStore';

export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
notes: [
{
id: uuid.v4(),
task: 'Learn webpack'
},
{
id: uuid.v4(),
task: 'Learn react'
},
{
id: uuid.v4(),
task: 'go sleep'
}
]
};
this.state = NoteStore.getState();

//bind the "this" context to the function so "this" works in the function
this.addNote = this.addNote.bind(this);
}

componentDidMount() {
NoteStore.listen(this.storeChanged);
}

componentWillUnmount() {
NoteStore.unlisten(this.storeChanged);
}

storeChanged = (state) => {
//must initialize the state so that it points to the right context.
this.setState(state);
}

render() {
const notes = this.state.notes;
return (
Expand All @@ -36,28 +36,16 @@ export default class App extends React.Component {
}
//addNote = () => { //uncomment this line and remove the binding event above to use the new syntax.
addNote() {
this.setState({
notes: this.state.notes.concat([{
id: uuid.v4(),
task: 'New task'
}])
});
NoteActions.create({task: 'New task'});

console.log('add note');
}
editNote = (id, task) => {
const notes = this.state.notes.map((note) => {
if (note.id === id) {
note.task = task;
}
NoteActions.update({id, task});

return note;
});
this.setState({notes});
}
deleteNote = (id) => {
NoteActions.delete(id);
console.log("deleting node:",id);
this.setState({
notes: this.state.notes.filter((note) => note.id !== id)
});
}
}
4 changes: 2 additions & 2 deletions app/stores/NoteStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class NoteStore {
constructor() {
//use bindActions to map each action in NodeActions to a method by name.
this.bindActions(NoteActions);
this.notes = [];
this.notes = [{id: uuid.v4(), task:'test'}];
}

create(note) {
Expand All @@ -32,7 +32,7 @@ class NoteStore {
delete(noteId) {
this.setState({
notes: this.notes.filter((note) => note.id !== nodeId)

});
}
}

Expand Down

0 comments on commit e9ae541

Please sign in to comment.