Skip to content
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

Added react example #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 collection/todo-app-react/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions collection/todo-app-react/app.dpd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
113 changes: 113 additions & 0 deletions collection/todo-app-react/app/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const React = require('react');
const ReactDOM = require('react-dom');

class App extends React.Component {
constructor(){
super();

this.state = { todos: []};

dpd.todos.get()
.then(todos => this.setState({ todos }));
}

addTask(e){
e.preventDefault();
const todos = this.state.todos;
const title = this.input.value;

if (title) {
dpd.todos.post({ title }, (result, error) => {
if (error) throw new Error(error);

this.setState({
todos: [
...todos,
{
id: result.id,
title: this.input.value,
done: false
}
]
});

this.input.value = '';
});
}
}

toggleTask(i){
const todos = this.state.todos;
const updatedTodo = Object.assign({}, todos[i], {done: !todos[i].done});

dpd.todos.put(updatedTodo, (result, error) => {
this.setState({
todos: [
...todos.slice(0, i),
result,
...todos.slice(i+1)
]
});
});
}

removeDoneTasks(e){
e.preventDefault();
const todos = this.state.todos.filter(t => t.done);

todos.forEach(todo => {
dpd.todos.del(todo.id, (_, error) => {
if (error) throw new Error(error);
this.setState({ todos: this.state.todos.filter(t => t !== todo) });
});
});
}

render(){
var noTasks;
if (!this.state.todos.length)
noTasks = (<p id="empty">You don't have any todos! Add one now:</p>);

return (
<div className="container">
<h1>Deployd Todos</h1>
{ noTasks }
<ul id="todos" className="unstyled">
{
this.state.todos.map((todo, index) =>
{
return (
<li key={todo.id}>
<label className="checkbox">
<input type="checkbox" onClick={ this.toggleTask.bind(this, index) } checked={todo.done}/>
<span>{ todo.title }</span>
</label>
</li>
);
}
)

}
</ul>
<form className="form-inline">
<input
id="todo-title"
type="text"
ref={ (node) => this.input = node }
/>
<button
id="add-btn"
className="btn btn-success"
onClick={ this.addTask.bind(this) }
>Add</button>
</form>
<p>
<a href id="remove-completed-btn" onClick={ this.removeDoneTasks.bind(this) }>Remove completed</a>
</p>
</div>
);
}
}


ReactDOM.render(<App/>, document.getElementById('app'));
31 changes: 31 additions & 0 deletions collection/todo-app-react/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "todo-app-react",
"version": "1.0.0",
"description": "deployd + react todo example",
"main": "index.js",
"scripts": {
"webpack-watch": "webpack --watch",
"watch": "npm-run-all --parallel start webpack-watch",
"start": "dpd start"
},
"author": "Gabriel Perales",
"license": "ISC",
"devDependencies": {
"babel": "^6.5.2",
"babel-core": "^6.7.2",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"browser-sync": "^2.11.1",
"browser-sync-webpack-plugin": "^1.0.1",
"css-loader": "^0.23.1",
"extract-text-webpack-plugin": "^1.0.1",
"npm-run-all": "^1.5.3",
"react": "^0.14.7",
"react-dom": "^0.14.7",
"webpack": "^1.12.14"
},
"dependencies": {
"deployd": "^0.8.8"
}
}
9 changes: 9 additions & 0 deletions collection/todo-app-react/public/css/bootstrap.min.css

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions collection/todo-app-react/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Deployd Todos</title>
<link rel="stylesheet" href="css/bootstrap.min.css" />
</head>
<body>
<div id="app"></div>
<script type="text/javascript" src="dpd.js"></script>
<script type="text/javascript" src="js/bundle.js"></script>
</body>
</html>
Loading