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

solution #7

Open
wants to merge 9 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ Resources:
- `id` - unique identifier
- `completed` - current status (`false` by default)
1. Show the number of not completed todos in `TodoApp`
1. Implement `TodoList` component to display a list of todos ({ id, title, completed })
1. Implement `Todos` component to display a list of todos ({ id, title, completed })
```jsx harmony
<TodoList items={todos} />
<Todos items={todos} />
```
1. Implement `TodoItem` component with ability to toggle the `complete` status.
1. Add ability to toggle the completed status of all the todos.
Expand Down
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
{
"homepage": "http://mate-academy.github.io/react_todo-app",
"homepage": "http://earthhuman41.github.io/react_todo-app",
"name": "react-todo-app",
"version": "0.1.0",
"private": true,
"keywords": [],
"author": "Mate Academy",
"license": "GPL-3.0",
"dependencies": {
"classnames": "^2.2.6",
"node-uuid": "^1.4.8",
"prop-types": "^15.7.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "3.0.1"
"react-redux": "^7.1.0",
"react-router-dom": "^5.0.1",
"react-scripts": "3.0.1",
"redux": "^4.0.4",
"reselect": "^4.0.0"
},
"devDependencies": {
"@mate-academy/eslint-config": "*",
Expand Down
75 changes: 0 additions & 75 deletions src/App.js

This file was deleted.

33 changes: 33 additions & 0 deletions src/components/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import { connect } from 'react-redux';

import TodoList from './Todos';
import Header from './Header';
import Footer from './Footer';
import { appPropTypes } from '../propTypes/propTypes';

function App({ todosLength, match: { params } }) {
return (
<section className="todoapp">

<Header />
{
todosLength > 0 && (
<>
<TodoList filter={params.filter} />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ToDoList in heder? footer in heder???

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heder их не оборачивает

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Пофиксил баг в инпуте

<Footer />
</>
)
}

</section>
);
}

const mapState = ({ todos }) => ({
todosLength: todos.length,
});

export default connect(mapState)(App);

App.propTypes = appPropTypes;
74 changes: 74 additions & 0 deletions src/components/Footer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React from 'react';
import classNames from 'classnames';
import { NavLink } from 'react-router-dom';
import { connect } from 'react-redux';

import { footerPropTypes } from '../propTypes/propTypes';

import { getCompletedLength } from '../redux/store';
import { deleteAllCompleted } from '../redux/todos';

const Footer = ({ todosLength, completedLength, ...props }) => {
const clearBtnClasses = classNames({
'clear-completed': true,
'clear-completed--disable': completedLength === 0,
});

return (
<footer className="footer">
<span className="todo-count">
{ `${todosLength - completedLength} items left` }
</span>
<ul className="filters">
<li>
<NavLink
exact="/"
to="/"
activeClassName="selected"
>
All
</NavLink>
</li>

<li>
<NavLink
to="/active"
activeClassName="selected"
>
Active
</NavLink>
</li>

<li>
<NavLink
to="/completed"
activeClassName="selected"
>
Completed
</NavLink>
</li>
</ul>

<button
onClick={props.deleteAllCompleted}
type="button"
className={clearBtnClasses}
>
Clear completed
</button>
</footer>
);
};

const mapState = state => ({
todosLength: state.todos.length,
completedLength: getCompletedLength(state),
});

const mapActions = {
deleteAllCompleted,
};

export default connect(mapState, mapActions)(Footer);

Footer.propTypes = footerPropTypes;
54 changes: 54 additions & 0 deletions src/components/Header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React from 'react';
import { connect } from 'react-redux';

import { addTodo } from '../redux/todos';
import { headerPropTypes } from '../propTypes/propTypes';

class Header extends React.Component {
state = {
inputValue: '',
};

handleInput = (e) => {
const { value } = e.target;

this.setState({
inputValue: value,
});
};

handleSubmit = (e) => {
const inputValue = this.state.inputValue.trim();

if (e.key === 'Enter' && inputValue) {
this.props.addTodo(inputValue);
this.setState({
inputValue: '',
});
}
};

render() {
return (
<header className="header">
<h1>todos</h1>

<input
onKeyDown={this.handleSubmit}
onChange={this.handleInput}
className="new-todo"
placeholder="What needs to be done?"
value={this.state.inputValue}
/>
</header>
);
}
}

const mapActions = {
addTodo,
};

export default connect(null, mapActions)(Header);

Header.propTypes = headerPropTypes;
112 changes: 112 additions & 0 deletions src/components/TodoItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import React from 'react';
import classNames from 'classnames';
import { connect } from 'react-redux';

import {
deleteTodo, togleCompleteTodo, changeTodoTitle,
} from '../redux/todos';
import { todoItemPropTypes } from '../propTypes/propTypes';

class TodoItem extends React.Component {
state = {
isEditableItem: false,
inputValue: '',
};

componentDidUpdate() {
this.TitleField.focus();
}

handleDbClick = () => {
this.setState(prevState => ({
inputValue: this.props.todo.title,
isEditableItem: !prevState.isEditableItem,
}));
};

handleInput = (event) => {
const { value } = event.target;

this.setState({
inputValue: value,
});
};

handlePressEnter = (event) => {
if (event.key === 'Enter') {
this.handleSubmit();
}
};

handleBlur = () => {
if (this.state.isEditableItem !== false) {
this.handleSubmit();
}
};

handleSubmit = () => {
const { todo: { id }, ...props } = this.props;
const inputValue = this.state.inputValue.trim();

if (inputValue) {
props.changeTodoTitle(id, inputValue);

this.setState({
isEditableItem: false,
});
} else {
props.deleteTodo(id);
}
};

render() {
const { todo: { completed, id, title }, ...props } = this.props;
const { isEditableItem } = this.state;

const classes = classNames({
editing: isEditableItem,
completed,
});

return (
<li className={classes}>
<div className="view">
<input
type="checkbox"
onClick={() => { props.togleCompleteTodo(id); }}
className="toggle"
checked={completed}
/>
<label
onDoubleClick={this.handleDbClick}
>
{title}
</label>
<button
onClick={() => props.deleteTodo(id)}
type="button"
className="destroy"
/>
</div>
<input
className="edit"
onChange={this.handleInput}
value={this.state.inputValue}
onKeyPress={this.handlePressEnter}
onBlur={this.handleBlur}
ref={(input) => { this.TitleField = input; }}
/>
</li>
);
}
}

const mapActions = {
deleteTodo,
togleCompleteTodo,
changeTodoTitle,
};

export default connect(null, mapActions)(TodoItem);

TodoItem.propTypes = todoItemPropTypes;
Loading