Skip to content

Todo #57

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Todo #57

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
/node_modules

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
2,229 changes: 2,229 additions & 0 deletions 02-props-state-component-architecture/react-components/todo/todo/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "todo",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-router-dom": "^4.2.2",
"react-scripts": "1.0.17"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
}
],
"start_url": "./index.html",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.App {
text-align: center;
}

/*.App-header {
background-color: #222;
height: 0px;
padding: 20px;
color: white;
}*/

.App-title {
font-size: 1.5em;
}

.App-intro {
font-size: large;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React, { Component } from "react";
import logo from "./logo.svg";
import "./App.css";
import Todo from "./Todo";
import { Route, Link } from "react-router-dom";

class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<h1 className="Todo's">What's your Todo's?</h1>
<p>
<Link to="/todos/new">Add a todo</Link>
</p>
<p>
<Link to="/todos">Show all todos</Link>
</p>
</header>
<Todo />
</div>
);
}
}

export default App;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import React, { Component } from "react";
import TodoItem from "./TodoItem";
import TodoForm from "./TodoForm";
import { Route } from "react-router-dom";
export default class TodoList extends Component {
constructor(props) {
super(props);
this.state = {
todos: [{ id: 0, title: "Test", details: "stufff", done: false }]
};

this.handleAdd = this.handleAdd.bind(this);
this.handleRemove = this.handleRemove.bind(this);
}
handleAdd(newTodo) {
this.setState({ todos: [newTodo, ...this.state.todos] });
}
handleRemove(idx) {
const newTodos = [...this.state.todos];
newTodos.splice(idx, 1);
this.setState({ todos: newTodos });
}

toggleTodo(id) {
const newTodos = this.state.todos.map(todo => {
if (todo.id === id) {
return { ...todo, done: !todo.done };
}
return todo;
});
this.setState({ todos: newTodos });
}

render() {
const todos = this.state.todos.map((
todo //{
) => (
<TodoItem
key={todo.id}
id={todo.id}
title={todo.title}
details={todo.details}
done={todo.done}
remove={this.handleRemove}
add={this.handleAdd}
toggleTodo={this.toggleTodo.bind(this, todo.id)}
/>
));

return (
<div>
<Route exact path="/todos" render={() => <div>{todos} </div>} />
<Route
exact
path="/todos/new"
render={routeProps => (
<TodoForm
handleSubmit={this.handleAdd}
{...routeProps}
/>
)}
/>
<Route
exact
path="/todos/:id"
render={props =>
todos.find(
t => t.props.id === +props.match.params.id
) || null}
/>
<Route
exact
path="/todos/:id/edit"
render={props => {
let todo =
todos.find(
t => t.props.id === +props.match.params.id
) || null;
return (
<TodoForm
{...props}
handleSubmit={this.handleAdd}
title={todo.props.title}
details={todo.props.details}
edit
/>
);
}}
/>
</div>
);
}
} //})}
//</ul>
//</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React, { Component } from "react";

export default class TodoForm extends Component {
constructor(props) {
super(props);
this.state = {
title: props.title || "",
details: props.details || ""
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

handleChange(e) {
this.setState({ [e.target.name]: e.target.value });
}
handleSubmit(e) {
e.preventDefault();
this.props.handleSubmit(this.state);
this.setState({
title: "",
details: ""
});
}

render() {
let formTitle = this.props.edit ? "Edit a todo" : "Add a todo";
let { title, details } = this.state;
return (
<div>
<h3>{formTitle}</h3>
<form onSubmit={this.handleSubmit}>
<label htmlFor="title">title</label>
<input
type="text"
placeholder="What's your todo?"
onChange={this.handleChange}
name="title"
value={title}
/>
<label htmlFor="details">details</label>
<input
type="text"
placeholder="details please"
onChange={this.handleChange}
name="details"
value={details}
/>
<input type="submit" value="todos list!" />
</form>
</div>
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from "react";
import styled from "styled-components";
import { Link } from "react-router-dom";

const ListItem = styled.li`
text-decoration: ${props => (props.done ? "line-through" : "none")};
`;

const XStyle = styled.span`
:hover {
color: red;
cursor: pointer;
}
`;

//stateless functional component

const TodoItem = props => (
<div>
<ListItem done={props.done}>
<Link to={`/todos/${props.id}`}> {props.title} </Link>
{" - " + props.details + " "}
<Link to={`/todos/${props.id}/edit`}> Edit </Link>
<button onClick={props.toggleTodo}>Complete</button>
<XStyle onClick={props.remove}>X</XStyle>
</ListItem>
</div>
);

export default TodoItem;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
body {
margin: 0;
padding: 0;
font-family: sans-serif;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { BrowserRouter } from "react-router-dom";
import registerServiceWorker from "./registerServiceWorker";
import "./index.css";

ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById("root")
);
registerServiceWorker();
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading