-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Solution #1317
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
base: master
Are you sure you want to change the base?
Solution #1317
Changes from all 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,157 +1,22 @@ | ||
| /* eslint-disable jsx-a11y/control-has-associated-label */ | ||
| import React from 'react'; | ||
| import { Header } from './components/Header'; | ||
| import { TodoList } from './components/TodoList'; | ||
| import { Footer } from './components/Footer'; | ||
| import { TodoProvider } from './context/TodoContext'; | ||
|
|
||
| export const App: React.FC = () => { | ||
| return ( | ||
| <div className="todoapp"> | ||
| <h1 className="todoapp__title">todos</h1> | ||
|
|
||
| <div className="todoapp__content"> | ||
| <header className="todoapp__header"> | ||
| {/* this button should have `active` class only if all todos are completed */} | ||
| <button | ||
| type="button" | ||
| className="todoapp__toggle-all active" | ||
| data-cy="ToggleAllButton" | ||
| /> | ||
|
|
||
| {/* Add a todo on form submit */} | ||
| <form> | ||
| <input | ||
| data-cy="NewTodoField" | ||
| type="text" | ||
| className="todoapp__new-todo" | ||
| placeholder="What needs to be done?" | ||
| /> | ||
| </form> | ||
| </header> | ||
|
|
||
| <section className="todoapp__main" data-cy="TodoList"> | ||
| {/* This is a completed todo */} | ||
| <div data-cy="Todo" className="todo completed"> | ||
| <label className="todo__status-label"> | ||
| <input | ||
| data-cy="TodoStatus" | ||
| type="checkbox" | ||
| className="todo__status" | ||
| checked | ||
| /> | ||
| </label> | ||
|
|
||
| <span data-cy="TodoTitle" className="todo__title"> | ||
| Completed Todo | ||
| </span> | ||
|
|
||
| {/* Remove button appears only on hover */} | ||
| <button type="button" className="todo__remove" data-cy="TodoDelete"> | ||
| × | ||
| </button> | ||
| </div> | ||
|
|
||
| {/* This todo is an active todo */} | ||
| <div data-cy="Todo" className="todo"> | ||
| <label className="todo__status-label"> | ||
| <input | ||
| data-cy="TodoStatus" | ||
| type="checkbox" | ||
| className="todo__status" | ||
| /> | ||
| </label> | ||
|
|
||
| <span data-cy="TodoTitle" className="todo__title"> | ||
| Not Completed Todo | ||
| </span> | ||
|
|
||
| <button type="button" className="todo__remove" data-cy="TodoDelete"> | ||
| × | ||
| </button> | ||
| </div> | ||
|
|
||
| {/* This todo is being edited */} | ||
| <div data-cy="Todo" className="todo"> | ||
| <label className="todo__status-label"> | ||
| <input | ||
| data-cy="TodoStatus" | ||
| type="checkbox" | ||
| className="todo__status" | ||
| /> | ||
| </label> | ||
|
|
||
| {/* This form is shown instead of the title and remove button */} | ||
| <form> | ||
| <input | ||
| data-cy="TodoTitleField" | ||
| type="text" | ||
| className="todo__title-field" | ||
| placeholder="Empty todo will be deleted" | ||
| value="Todo is being edited now" | ||
| /> | ||
| </form> | ||
| </div> | ||
|
|
||
| {/* This todo is in loadind state */} | ||
| <div data-cy="Todo" className="todo"> | ||
| <label className="todo__status-label"> | ||
| <input | ||
| data-cy="TodoStatus" | ||
| type="checkbox" | ||
| className="todo__status" | ||
| /> | ||
| </label> | ||
|
|
||
| <span data-cy="TodoTitle" className="todo__title"> | ||
| Todo is being saved now | ||
| </span> | ||
|
|
||
| <button type="button" className="todo__remove" data-cy="TodoDelete"> | ||
| × | ||
| </button> | ||
| </div> | ||
| </section> | ||
|
|
||
| {/* Hide the footer if there are no todos */} | ||
| <footer className="todoapp__footer" data-cy="Footer"> | ||
| <span className="todo-count" data-cy="TodosCounter"> | ||
| 3 items left | ||
| </span> | ||
|
|
||
| {/* Active link should have the 'selected' class */} | ||
| <nav className="filter" data-cy="Filter"> | ||
| <a | ||
| href="#/" | ||
| className="filter__link selected" | ||
| data-cy="FilterLinkAll" | ||
| > | ||
| All | ||
| </a> | ||
|
|
||
| <a | ||
| href="#/active" | ||
| className="filter__link" | ||
| data-cy="FilterLinkActive" | ||
| > | ||
| Active | ||
| </a> | ||
|
|
||
| <a | ||
| href="#/completed" | ||
| className="filter__link" | ||
| data-cy="FilterLinkCompleted" | ||
| > | ||
| Completed | ||
| </a> | ||
| </nav> | ||
|
|
||
| {/* this button should be disabled if there are no completed todos */} | ||
| <button | ||
| type="button" | ||
| className="todoapp__clear-completed" | ||
| data-cy="ClearCompletedButton" | ||
| > | ||
| Clear completed | ||
| </button> | ||
| </footer> | ||
| <TodoProvider> | ||
| <div className="todoapp"> | ||
| <h1 className="todoapp__title">todos</h1> | ||
|
|
||
| <div className="todoapp__content"> | ||
|
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. Checklist item #7 requires using the |
||
| <Header /> | ||
| <TodoList /> | ||
|
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 handler is called inside the onClick callbacks, creating unnecessary nested arrow functions. Consider passing the filter type directly to setFilter or creating a dedicated handler. |
||
| <Footer /> | ||
| </div> | ||
|
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. Checklist item #10 requires creating constants for filter strings instead of using hardcoded string literals. Define a FILTERS constant object with 'all', 'active', 'completed' values. |
||
| </div> | ||
| </div> | ||
| </TodoProvider> | ||
| ); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import React from 'react'; | ||
| import cn from 'classnames'; | ||
| import { FILTERS } from '../../utils/filters'; | ||
|
|
||
| import { useTodo } from '../../context/TodoContext'; | ||
|
|
||
| export const Filter: React.FC = () => { | ||
| const { filter, setFilter } = useTodo(); | ||
|
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 violates checklist item #10 which requires using constants for filter strings. The values 'all', 'active', and 'completed' are used as raw strings throughout the file. Define a FILTERS constant object instead. |
||
|
|
||
| return ( | ||
| <nav className="filter" data-cy="Filter"> | ||
| <a | ||
| href="#/" | ||
| className={cn('filter__link', { selected: filter === FILTERS.all })} | ||
| data-cy="FilterLinkAll" | ||
| onClick={event => { | ||
| event.preventDefault(); | ||
| setFilter(FILTERS.all); | ||
| }} | ||
| > | ||
| All | ||
| </a> | ||
|
|
||
| <a | ||
| href="#/active" | ||
| className={cn('filter__link', { selected: filter === FILTERS.active })} | ||
| data-cy="FilterLinkActive" | ||
| onClick={event => { | ||
| event.preventDefault(); | ||
|
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. The |
||
| setFilter(FILTERS.active); | ||
| }} | ||
| > | ||
| Active | ||
| </a> | ||
|
|
||
| <a | ||
| href="#/completed" | ||
| className={cn('filter__link', { | ||
| selected: filter === FILTERS.completed, | ||
| })} | ||
| data-cy="FilterLinkCompleted" | ||
| onClick={event => { | ||
| event.preventDefault(); | ||
| setFilter(FILTERS.completed); | ||
| }} | ||
| > | ||
| Completed | ||
| </a> | ||
| </nav> | ||
| ); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './Filter'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import React from 'react'; | ||
| import { Filter } from '../Filter'; | ||
| import { useTodo } from '../../context/TodoContext'; | ||
|
|
||
| export const Footer: React.FC = () => { | ||
| const { todos, clearCompleted } = useTodo(); | ||
|
|
||
| if (todos.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| const onClick = () => { | ||
| clearCompleted(); | ||
| }; | ||
|
|
||
| return ( | ||
| <footer className="todoapp__footer" data-cy="Footer"> | ||
| <span className="todo-count" data-cy="TodosCounter"> | ||
| {todos.filter(todo => !todo.completed).length} items left | ||
| </span> | ||
|
|
||
| <Filter /> | ||
|
|
||
| <button | ||
| type="button" | ||
| className="todoapp__clear-completed" | ||
| data-cy="ClearCompletedButton" | ||
| onClick={onClick} | ||
| disabled={!todos.some(todo => todo.completed)} | ||
| > | ||
| Clear Completed | ||
| </button> | ||
| </footer> | ||
| ); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './Footer'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import cn from 'classnames'; | ||
| import { useTodo } from '../../context/TodoContext'; | ||
| import { NewTodo } from '../NewTodo'; | ||
|
|
||
| export const Header: React.FC = () => { | ||
|
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. Checklist item #7 violation: Use the 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. Other components use the |
||
| const { todos, toggleAll } = useTodo(); | ||
|
|
||
|
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. Checklist item #6 violation: Function names should start with a verb and describe the result. Consider renaming to |
||
| const handleToggleAll = () => { | ||
| toggleAll(); | ||
| }; | ||
|
|
||
| return ( | ||
| <header className="todoapp__header"> | ||
| {/* this button should have `active` class only if all todos are completed */} | ||
| {todos.length > 0 && ( | ||
| <button | ||
| type="button" | ||
| className={cn('todoapp__toggle-all', { | ||
| active: todos.every(todo => todo.completed), | ||
| })} | ||
| onClick={handleToggleAll} | ||
| data-cy="ToggleAllButton" | ||
| /> | ||
| )} | ||
|
|
||
| {/* Add a todo on form submit */} | ||
| <NewTodo /> | ||
| </header> | ||
| ); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './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.
The
onClickvariable shadows the filter type'sonClickmethod from useTodo. Consider renaming to avoid confusion and follow the naming convention that handler functions should start with 'handle'.