-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
base: master
Are you sure you want to change the base?
solution #7
Changes from 2 commits
127ccbf
624bd27
7906986
3819f7c
aa5c5b0
132cb3b
ef1fd05
24863b8
26df996
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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) { | ||
localStorage.setItem('state', JSON.stringify(nextState)); | ||
} | ||
|
||
changeFilter = async(e) => { | ||
console.log(e.target); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = ''; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. если ты уже начал парсить |
||
|
||
this.setState({ filteredField: filterValue }); | ||
}; | ||
|
||
filterTodos = (todosList) => { | ||
const { filteredField } = this.state; | ||
let status; | ||
|
||
switch (filteredField) { | ||
case 'completed': | ||
status = true; | ||
break; | ||
case 'active': | ||
status = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. чем создавать переменную проще тут сделать |
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. простота поддержки иногда важнее производительности |
||
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: '' }; | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
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; |
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
у этого метода нет никаких парметров!!!