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 2 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"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,
Expand Down
75 changes: 0 additions & 75 deletions src/App.js

This file was deleted.

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

import TodoList from '../TodoList/TodoList';
import Header from '../Header/Header';
import Footer from '../Footer/Footer';

class App extends React.Component {
state = {
todosList: [],
filteredField: '',
inputValue: '',
completedLength: 0,
};

componentWillMount() {
console.log(window.location.href);
const state = JSON.parse(localStorage.getItem('state'));
console.log(JSON.parse(localStorage.getItem('state')));
this.setState({ ...state });
}

componentWillUpdate(nextProps, nextState, nextContext) {
Copy link
Contributor

Choose a reason for hiding this comment

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

у этого метода нет никаких парметров!!!

localStorage.setItem('state', JSON.stringify(nextState));
}

changeFilter = async(e) => {
console.log(e.target);
Copy link
Contributor

Choose a reason for hiding this comment

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

поубирай пожалуйста лишний код

let filterValue = e.target.href.match(/#\/([a-z]+)/);

if (filterValue) {
filterValue = filterValue[1];
} else {
filterValue = '';
}
Copy link
Contributor

Choose a reason for hiding this comment

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

если ты уже начал парсить href то пора использовать React Router


this.setState({ filteredField: filterValue });
};

filterTodos = (todosList) => {
const { filteredField } = this.state;
let status;

switch (filteredField) {
case 'completed':
status = true;
break;
case 'active':
status = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

чем создавать переменную проще тут сделать return того что нужно
Может добавится больше фильтров или логика изменится
Да и нагляднее будет - что именно возвращаем в каждом случае когда

break;
default: return todosList;
}

return todosList.filter(todo => todo.completed === status);
};

completeTodo = (id) => {
this.setState((prevState) => {
const todosList = [...prevState.todosList];
const index = todosList.findIndex(todo => todo.id === id);
const togle = !todosList[index].completed;

todosList[index].completed = togle;
Copy link
Contributor

@mgrinko mgrinko Jul 12, 2019

Choose a reason for hiding this comment

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

const todos = prevState.todos.map(todo => {
  if (todo.id !== id) {
    return todo;
  }

  return {
    ...todo,
    completed: !todo.completed,
  };
});

const completedTodos = todos.filter(todo => todo.completed)

return { todos, completedLength: completedTodos.length };

Copy link
Contributor

Choose a reason for hiding this comment

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

let completedLength;

if (togle) {
completedLength = prevState.completedLength + 1;
} else {
completedLength = prevState.completedLength - 1;
}

return { todosList, completedLength };
});
};

togleAllComplete = () => {
let { completedLength, todosList } = this.state;
const togle = completedLength !== todosList.length;

this.setState((prevState) => {
todosList = prevState.todosList.map((todo) => {
const { id, title } = todo;
return {
id,
title,
completed: togle,
};
});

completedLength = togle ? prevState.todosList.length : 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

простота поддержки иногда важнее производительности
https://zoom.us/recording/share/0toADjnabJSYr5BrwhxuYQ8YXxuXzjke_u7ga_mD7cawIumekTziMw

return { todosList, completedLength };
});
};

deleteAllCompleted = () => {
this.setState((prevState) => {
let todosList = [...prevState.todosList];

todosList = todosList.filter(todo => !todo.completed);
return { todosList, completedLength: 0 };
});
};

deleteTodo = (id) => {
this.setState((prevState) => {
const todosList = [...prevState.todosList];

const elemIndex = todosList.findIndex(todo => todo.id === id);
const completedLength = prevState.completedLength - 1;
todosList.splice(elemIndex, 1);

return { todosList, completedLength };
});
};

changeInput = (e) => {
this.setState({ inputValue: e.target.value });
};

createTodo = (e, id) => {
const title = this.state.inputValue.trim();
if (e.key === 'Enter' && title) {
const todoItem = {
id,
title,
completed: false,
};

this.setState((prevState) => {
const list = [...prevState.todosList];
list.push(todoItem);
return { todosList: list, inputValue: '' };
});
Copy link
Contributor

Choose a reason for hiding this comment

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

}
};

addId = (callback) => {
let id = 1;
return (e) => {
id += 1;
callback(e, id);
};
};

render() {
const {
todosList,
inputValue,
completedLength,
filteredField,
} = this.state;

const filteredTodosList = this.filterTodos(todosList);
const todosLeft = todosList.length - completedLength;
const addTodo = this.addId(this.createTodo);

return (
<section className="todoapp">

<Header
inputValue={inputValue}
changeInput={this.changeInput}
addTodo={addTodo}
/>
{
todosList.length > 0 && (
<>
<TodoList
completedLength={completedLength}
completeTodo={this.completeTodo}
togleAllComplete={this.togleAllComplete}
deleteTodo={this.deleteTodo}
todosList={filteredTodosList}
/>
<Footer
filteredField={filteredField}
todosLeft={todosLeft}
completedLength={completedLength}
changeFilter={this.changeFilter}
deleteAllCompleted={this.deleteAllCompleted}
/>
</>
)
}

</section>
);
}
}

export default App;
78 changes: 78 additions & 0 deletions src/Footer/Footer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React from 'react';
import PropTypes from 'prop-types';

class Footer extends React.Component {
selectedPage(page) {
const { filteredField } = this.props;
return page === filteredField ? 'selected' : '';
}

render() {
const {
deleteAllCompleted,
changeFilter,
todosLeft,
completedLength,
} = this.props;

const clearBtnClassName = completedLength ? '' : 'clear-completed--disable';

return (
<footer className="footer">
<span className="todo-count">
{todosLeft} items left
</span>

<ul className="filters">
<li>
<a
onClick={e => changeFilter(e)}
href="#/"
className={this.selectedPage('')}
>
All
</a>
</li>

<li>
<a
onClick={e => changeFilter(e)}
href="#/active"
className={this.selectedPage('active')}
>
Active
</a>
</li>

<li>
<a
onClick={changeFilter}
href="#/completed"
className={this.selectedPage('completed')}
>
Completed
</a>
</li>
</ul>

<button
onClick={deleteAllCompleted}
type="button"
className={`clear-completed ${clearBtnClassName}`}
>
Clear completed
</button>
</footer>
);
}
}

Footer.propTypes = {
deleteAllCompleted: PropTypes.func.isRequired,
changeFilter: PropTypes.func.isRequired,
todosLeft: PropTypes.number.isRequired,
completedLength: PropTypes.number.isRequired,
filteredField: PropTypes.string.isRequired,
};

export default Footer;
26 changes: 26 additions & 0 deletions src/Header/Header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import PropTypes from 'prop-types';

function Header({ addTodo, inputValue, changeInput }) {
return (
<header className="header">
<h1>todos</h1>

<input
onKeyDown={addTodo}
onChange={changeInput}
className="new-todo"
placeholder="What needs to be done?"
value={inputValue}
/>
</header>
);
}

Header.propTypes = {
addTodo: PropTypes.func.isRequired,
inputValue: PropTypes.string.isRequired,
changeInput: PropTypes.func.isRequired,
};

export default Header;
Loading