-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
dev #783
base: master
Are you sure you want to change the base?
dev #783
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,68 @@ | ||
/* eslint-disable jsx-a11y/control-has-associated-label */ | ||
import React from 'react'; | ||
import React, { useState } from 'react'; | ||
import { TodosContext } from './components/TodosContext'; | ||
import { Todo } from './types/Todo'; | ||
import { TodoContent } from './components/TodoContent'; | ||
import { useLocalStorage } from './hooks/useLocalStorage'; | ||
import { TodoFooter } from './components/TodoFooter'; | ||
import { TodosFilter } from './types/TodosFilter'; | ||
|
||
export const App: React.FC = () => { | ||
const [query, setQuery] = useState(''); | ||
const [todos, setTodos] = useLocalStorage<Todo[]>('todos', []); | ||
const [todosFilter, setTodosFilter] = useState<TodosFilter>(TodosFilter.all); | ||
const [todoEditId, setTodoEditId] = useState<number>(0); | ||
const [todoEdit, setTodoEdit] = useState<string>(''); | ||
|
||
const handleSubmit = (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
if (query.trim()) { | ||
const todo = { | ||
id: Date.now(), | ||
title: query, | ||
completed: false, | ||
}; | ||
|
||
setTodos([...todos, todo]); | ||
setQuery(''); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="todoapp"> | ||
<header className="header"> | ||
<h1>todos</h1> | ||
|
||
<form> | ||
<form onSubmit={handleSubmit}> | ||
<input | ||
type="text" | ||
data-cy="createTodo" | ||
className="new-todo" | ||
placeholder="What needs to be done?" | ||
value={query} | ||
onChange={(event) => setQuery(event.target.value)} | ||
/> | ||
</form> | ||
</header> | ||
|
||
<section className="main"> | ||
<input | ||
type="checkbox" | ||
id="toggle-all" | ||
className="toggle-all" | ||
data-cy="toggleAll" | ||
/> | ||
<label htmlFor="toggle-all">Mark all as complete</label> | ||
|
||
<ul className="todo-list" data-cy="todoList"> | ||
<li> | ||
<div className="view"> | ||
<input type="checkbox" className="toggle" id="toggle-view" /> | ||
<label htmlFor="toggle-view">asdfghj</label> | ||
<button type="button" className="destroy" data-cy="deleteTodo" /> | ||
</div> | ||
<input type="text" className="edit" /> | ||
</li> | ||
|
||
<li className="completed"> | ||
<div className="view"> | ||
<input type="checkbox" className="toggle" id="toggle-completed" /> | ||
<label htmlFor="toggle-completed">qwertyuio</label> | ||
<button type="button" className="destroy" data-cy="deleteTodo" /> | ||
</div> | ||
<input type="text" className="edit" /> | ||
</li> | ||
|
||
<li className="editing"> | ||
<div className="view"> | ||
<input type="checkbox" className="toggle" id="toggle-editing" /> | ||
<label htmlFor="toggle-editing">zxcvbnm</label> | ||
<button type="button" className="destroy" data-cy="deleteTodo" /> | ||
</div> | ||
<input type="text" className="edit" /> | ||
</li> | ||
|
||
<li> | ||
<div className="view"> | ||
<input type="checkbox" className="toggle" id="toggle-view2" /> | ||
<label htmlFor="toggle-view2">1234567890</label> | ||
<button type="button" className="destroy" data-cy="deleteTodo" /> | ||
</div> | ||
<input type="text" className="edit" /> | ||
</li> | ||
</ul> | ||
</section> | ||
|
||
<footer className="footer"> | ||
<span className="todo-count" data-cy="todosCounter"> | ||
3 items left | ||
</span> | ||
|
||
<ul className="filters"> | ||
<li> | ||
<a href="#/" className="selected">All</a> | ||
</li> | ||
|
||
<li> | ||
<a href="#/active">Active</a> | ||
</li> | ||
|
||
<li> | ||
<a href="#/completed">Completed</a> | ||
</li> | ||
</ul> | ||
|
||
<button type="button" className="clear-completed"> | ||
Clear completed | ||
</button> | ||
</footer> | ||
<TodosContext.Provider | ||
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. Move provider to the context file, it will be a good practice |
||
value={{ | ||
todos, | ||
todosFilter, | ||
todoEditId, | ||
todoEdit, | ||
setTodos, | ||
setTodosFilter, | ||
setTodoEditId, | ||
setTodoEdit, | ||
}} | ||
> | ||
{todos.length > 0 && ( | ||
<> | ||
<TodoContent /> | ||
<TodoFooter /> | ||
</> | ||
)} | ||
</TodosContext.Provider> | ||
</div> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React, { useContext } from 'react'; | ||
import cn from 'classnames'; | ||
import { TodosContext } from './TodosContext'; | ||
import { TodoList } from './TodoList'; | ||
|
||
export const TodoContent: React.FC = () => { | ||
const context = useContext(TodosContext); | ||
|
||
if (!context) { | ||
return null; | ||
} | ||
|
||
const { todos, setTodos } = context; | ||
|
||
const isCompleted = todos.every(todo => todo.completed); | ||
|
||
const toggleAll = () => { | ||
setTodos(todos.map(todo => { | ||
return { ...todo, completed: !isCompleted }; | ||
})); | ||
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. same here pass callback |
||
}; | ||
|
||
return ( | ||
<section className="main"> | ||
<input | ||
data-cy="toggleAll" | ||
type="checkbox" | ||
id="toggle-all" | ||
className={cn( | ||
'toggle-all', | ||
{ active: isCompleted }, | ||
)} | ||
onClick={toggleAll} | ||
checked={isCompleted} | ||
/> | ||
|
||
<label htmlFor="toggle-all"> | ||
Mark all as complete | ||
</label> | ||
|
||
<TodoList /> | ||
</section> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React, { useContext } from 'react'; | ||
import cn from 'classnames'; | ||
import { TodosContext } from './TodosContext'; | ||
import { TodosFilter } from '../types/TodosFilter'; | ||
|
||
export const TodoFilter: React.FC = () => { | ||
const context = useContext(TodosContext); | ||
|
||
if (!context) { | ||
return null; | ||
} | ||
|
||
const { todosFilter, setTodosFilter } = context; | ||
|
||
const handleLink = (val: string) => { | ||
if (TodosFilter.all) { | ||
return '#/'; | ||
} | ||
|
||
return `#/${val.toLowerCase()}`; | ||
}; | ||
|
||
return ( | ||
<ul className="filters" data-cy="todosFilter"> | ||
{Object.values(TodosFilter).map(val => ( | ||
<li key={val}> | ||
<a | ||
href={handleLink(val)} | ||
onClick={() => setTodosFilter(val)} | ||
className={cn({ selected: todosFilter === val })} | ||
> | ||
{val} | ||
</a> | ||
</li> | ||
))} | ||
</ul> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,42 @@ | ||||||||||||||
import React, { useContext } from 'react'; | ||||||||||||||
import { TodosContext } from './TodosContext'; | ||||||||||||||
import { TodoFilter } from './TodoFilter'; | ||||||||||||||
|
||||||||||||||
export const TodoFooter: React.FC = () => { | ||||||||||||||
const context = useContext(TodosContext); | ||||||||||||||
|
||||||||||||||
if (!context) { | ||||||||||||||
return null; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
const { todos, setTodos } = context; | ||||||||||||||
|
||||||||||||||
const clearComplete = () => { | ||||||||||||||
setTodos(todos.filter(todo => !todo.completed)); | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
const isCompleted = todos.some(todo => todo.completed); | ||||||||||||||
|
||||||||||||||
const todosLeft = todos.filter(todo => todo.completed === false).length; | ||||||||||||||
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.
Suggested change
|
||||||||||||||
|
||||||||||||||
return ( | ||||||||||||||
<footer className="footer"> | ||||||||||||||
<span className="todo-count" data-cy="todosCounter"> | ||||||||||||||
{`${todosLeft} items left`} | ||||||||||||||
</span> | ||||||||||||||
|
||||||||||||||
<TodoFilter /> | ||||||||||||||
|
||||||||||||||
{isCompleted | ||||||||||||||
&& ( | ||||||||||||||
<button | ||||||||||||||
type="button" | ||||||||||||||
className="clear-completed" | ||||||||||||||
onClick={clearComplete} | ||||||||||||||
> | ||||||||||||||
Clear completed | ||||||||||||||
</button> | ||||||||||||||
)} | ||||||||||||||
</footer> | ||||||||||||||
); | ||||||||||||||
}; |
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.
It is safer to pass updater callback when new state values depends on previous one