Skip to content

Ep 19 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
npm-debug.log
**/node_modules/*
build/*
.idea
13 changes: 13 additions & 0 deletions ep19-call-api-jquery/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#### Setting up the application

```
npm install
npm start
```

Visit http://localhost:8080 in browser.

#### Notes

* `npm install random-key --save`
* [Source code](...)
97 changes: 97 additions & 0 deletions ep19-call-api-jquery/app/components/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import React from 'react';
import DisplayList from './DisplayList';

var rand = require('random-key');
var api = require("../utils/api");

export default class App extends React.Component {

constructor () {
super();
this.state = { title: '', todos: [] };
var processData = function(data) {
this.setState({todos: data.todos});
};
api.getTasks(processData.bind(this));
}

handleDone (idToBeMarkedAsDone) {
var _todos = this.state.todos;
var todo = _todos.filter((todo) => {
return todo.id === idToBeMarkedAsDone;
})[0];

todo.done = !todo.done;

var processData = function(data) {
this.setState({todos: data.todos});
};

var markTaskDoneCallback = function(data){
data.success ? api.getTasks(processData.bind(this)) : console.log("Failed to mark task as done/undone");
};

api.markTaskDone(markTaskDoneCallback.bind(this), todo);
}

handleDelete (idToBeDeleted) {
var processData = function(data) {
this.setState({todos: data.todos});
};

var deleteTaskCallback = function(data){
data.success ? api.getTasks(processData.bind(this)) : console.log("Failed to delete task");
};

api.deleteTask(deleteTaskCallback.bind(this), idToBeDeleted);
}

handleSubmit (event) {
event.preventDefault();

var newTodo = { title: this.state.title, done: false };

var processData = function(data) {
this.setState({title: '', todos: data.todos});
};

var addTaskCallback = function(data){
data.success ? api.getTasks(processData.bind(this)) : console.log("Failed to add task");
};

api.addTask(addTaskCallback.bind(this), newTodo);
}

handleChange (event) {
var title = event.target.value;
this.setState({ title: title });
}

handleClearCompleted (event) {
var newTodos = this.state.todos.filter((todo) => { return !todo.done});
this.setState({ todos: newTodos });
}

render () {
return <div>
<h1> TODO </h1>
<form onSubmit={this.handleSubmit.bind(this)}>
<input type="text"
onChange={this.handleChange.bind(this)}
value={this.state.title} />
</form>

<DisplayList
handleDone={this.handleDone.bind(this)}
handleDelete={this.handleDelete.bind(this)}
todos={this.state.todos} />

<footer>
All: ({ this.state.todos.length }) |
Completed: ({ this.state.todos.filter((todo) => { return todo.done }).length }) |
Pending: ({ this.state.todos.filter((todo) => { return !todo.done }).length }) |
<a href='#' onClick={this.handleClearCompleted.bind(this)}>Clear Completed</a>
</footer>
</div>;
}
}
74 changes: 74 additions & 0 deletions ep19-call-api-jquery/app/components/DisplayItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React from 'react';

export default class DisplayItem extends React.Component {

constructor () {
super();
this.state = { editing: false }
}

componentDidMount () {
this.setState({ changedText: this.props.todo.title });
}

handleEditing (event) {
this.setState({ editing: true, changedText: this.props.todo.title });
}

handleEditingDone (event) {
if (event.keyCode === 13 ) { // submit
this.setState({ editing: false });
}
}

handleEditingChange (event) {
var _changedText = event.target.value;
this.setState({ changedText: _changedText });
}

render () {
var todo = this.props.todo;

var viewStyle = {};
var editStyle = {};

if (this.state.editing) {
viewStyle.display = 'none';
} else {
editStyle.display = 'none';
}

return <li className={ todo.done ? 'done' : '' }>
<div style={viewStyle} onDoubleClick={this.handleEditing.bind(this)}>
<input
checked={todo.done}
onChange={this.props.handleDone.bind(null, todo.id)}
type="checkbox"
style={{ fontSize: 'x-large' }} />

<label>
{ this.state.changedText }
</label>

<a href='#'
className="destroy"
onClick={ this.props.handleDelete.bind(null, todo.id) }>
[x]
</a>
</div>

<input type="text"
onKeyDown={this.handleEditingDone.bind(this)}
onChange={this.handleEditingChange.bind(this)}
style={editStyle}
value={this.state.changedText} />
</li>
}

}

DisplayItem.propTypes = {
todo: React.PropTypes.object.isRequired,
handleDone: React.PropTypes.func.isRequired,
handleDelete: React.PropTypes.func.isRequired
}
25 changes: 25 additions & 0 deletions ep19-call-api-jquery/app/components/DisplayList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';
import DisplayItem from './DisplayItem';

export default class DisplayList extends React.Component {

render () {
return <ul id="todo-list">
{ this.props.todos.map((todo, i) => {
return <section id="main" key={todo.id}>
<DisplayItem
todo={todo}
handleDone={this.props.handleDone}
handleDelete={this.props.handleDelete} />
</section>
}) }
</ul>
}

}

DisplayList.propTypes = {
todos: React.PropTypes.array.isRequired,
handleDone: React.PropTypes.func.isRequired,
handleDelete: React.PropTypes.func.isRequired
}
14 changes: 14 additions & 0 deletions ep19-call-api-jquery/app/main.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import './stylesheets/main.css';

import React from 'react';
import App from './components/App';

main();

function main() {
var div = document.createElement('div');
div.setAttribute("id", "todoapp");
document.body.appendChild(div);

React.render(<App />, div);
}
Loading