Skip to content

Commit d59c3d0

Browse files
baseline for episode 13
1 parent a781a13 commit d59c3d0

File tree

8 files changed

+222
-0
lines changed

8 files changed

+222
-0
lines changed

ep13-clear-completed/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#### Setting up the application
2+
3+
```
4+
npm install
5+
npm start
6+
```
7+
8+
Visit http://localhost:8080 in browser.
9+
10+
#### Notes
11+
12+
* [Source code](...)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import React from 'react';
2+
import DisplayList from './DisplayList';
3+
4+
export default class App extends React.Component {
5+
6+
constructor () {
7+
super();
8+
this.state = { title: '', todos: [
9+
{ title: 'eggs', done: false },
10+
{ title: 'banana', done: false },
11+
{ title: 'bread', done: false }
12+
] };
13+
}
14+
15+
handleDone (titleToBeMarkedAsDone) {
16+
console.log(titleToBeMarkedAsDone + " wants to be marked as done");
17+
var _todos = this.state.todos;
18+
var todo = _todos.filter((todo) => {
19+
return todo.title === titleToBeMarkedAsDone;
20+
})[0];
21+
22+
todo.done = !todo.done;
23+
24+
this.setState({ todos: _todos });
25+
}
26+
27+
handleDelete (titleToBeDeleted) {
28+
var newTodos = this.state.todos.filter( (todo) => {
29+
return todo.title != titleToBeDeleted
30+
} )
31+
32+
this.setState({ todos: newTodos});
33+
}
34+
35+
handleSubmit (event) {
36+
event.preventDefault();
37+
38+
var title = this.state.title;
39+
var newTodos = this.state.todos.concat({ title: title, done: false });
40+
41+
this.setState({ title: '', todos: newTodos });
42+
}
43+
44+
handleChange (event) {
45+
var title = event.target.value;
46+
this.setState({ title: title });
47+
}
48+
49+
render () {
50+
return <div>
51+
<p> TODO </p>
52+
<form onSubmit={this.handleSubmit.bind(this)}>
53+
<input onChange={this.handleChange.bind(this)} value={this.state.title} />
54+
<button> Submit </button>
55+
</form>
56+
57+
<p>
58+
Number of total tasks: { this.state.todos.length }
59+
</p>
60+
<p>
61+
Number of total tasks done: { this.state.todos.filter((todo) => { return todo.done }).length }
62+
</p>
63+
64+
<DisplayList
65+
handleDone={this.handleDone.bind(this)}
66+
handleDelete={this.handleDelete.bind(this)}
67+
todos={this.state.todos} />
68+
</div>;
69+
}
70+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react';
2+
3+
export default class DisplayItem extends React.Component {
4+
5+
render () {
6+
var todo = this.props.todo;
7+
var title = todo.title;
8+
9+
return <li>
10+
<input
11+
checked={todo.done}
12+
onChange={this.props.handleDone.bind(null, title)}
13+
type="checkbox"
14+
style={{ fontSize: 'x-large' }} />
15+
16+
{ title }
17+
<a href='#' onClick={ this.props.handleDelete.bind(null, title) }>
18+
[x]
19+
</a>
20+
</li>;
21+
}
22+
23+
}
24+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react';
2+
import DisplayItem from './DisplayItem';
3+
4+
export default class DisplayList extends React.Component {
5+
6+
render () {
7+
return <ul>
8+
{ this.props.todos.map((todo, i) => {
9+
return <DisplayItem
10+
key={todo.title}
11+
todo={todo}
12+
handleDone={this.props.handleDone}
13+
handleDelete={this.props.handleDelete.bind(null, todo.title)} />;
14+
}) }
15+
</ul>
16+
}
17+
18+
}

ep13-clear-completed/app/main.jsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import './stylesheets/main.css';
2+
3+
import React from 'react';
4+
import App from './components/App';
5+
6+
main();
7+
8+
function main() {
9+
var app = document.createElement('div');
10+
document.body.appendChild(app);
11+
12+
React.render(<App />, app);
13+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body{
2+
font-size: 200%;
3+
}

ep13-clear-completed/package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "learning-reactjs-in-steps",
3+
"version": "0.0.1",
4+
"description": "Learn ReactJS in steps",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"build": "TAGET=build webpack",
9+
"start": "TARGET=dev webpack-dev-server --devtool eval --progress --colors --hot"
10+
},
11+
"author": "Neeraj Singh",
12+
"license": "ISC",
13+
"devDependencies": {
14+
"babel-core": "^5.6.15",
15+
"babel-loader": "^5.3.1",
16+
"css-loader": "^0.15.1",
17+
"html-webpack-plugin": "^1.5.2",
18+
"node-libs-browser": "^0.5.2",
19+
"react-hot-loader": "^1.2.7",
20+
"style-loader": "^0.12.3",
21+
"webpack": "^1.10.1",
22+
"webpack-dev-server": "^1.10.1",
23+
"webpack-merge": "^0.1.2"
24+
},
25+
"dependencies": {
26+
"react": "^0.13.3"
27+
}
28+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var path = require('path');
2+
var HtmlWebpackPlugin = require('html-webpack-plugin');
3+
var merge = require('webpack-merge');
4+
5+
var TARGET = process.env.TARGET;
6+
var ROOT_PATH = path.resolve(__dirname);
7+
8+
var common = {
9+
entry: [path.resolve(ROOT_PATH, 'app/main')],
10+
resolve: {
11+
extensions: ['', '.js', '.jsx'],
12+
},
13+
output: {
14+
path: path.resolve(ROOT_PATH, 'build'),
15+
filename: 'bundle.js',
16+
},
17+
plugins: [
18+
new HtmlWebpackPlugin({ title: 'Todo app', }),
19+
],
20+
module: {
21+
loaders: [
22+
{
23+
test: /\.css$/,
24+
loaders: ['style', 'css'],
25+
},
26+
{
27+
test: /\.jsx?$/,
28+
loader: 'babel?stage=1',
29+
include: path.resolve(ROOT_PATH, 'app'),
30+
}
31+
],
32+
},
33+
};
34+
35+
if (TARGET === 'build') {
36+
module.exports = common;
37+
}
38+
39+
if (TARGET === 'dev') {
40+
module.exports = merge(common, {
41+
entry: [
42+
'webpack-dev-server/client?http://0.0.0.0:8080',
43+
'webpack/hot/dev-server'
44+
],
45+
module: {
46+
loaders: [ {
47+
test: /\.jsx?$/,
48+
loaders: ['react-hot', 'babel?stage=1'],
49+
include: path.resolve(ROOT_PATH, 'app'),
50+
},
51+
],
52+
},
53+
});
54+
}

0 commit comments

Comments
 (0)