-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Solution #1277
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 #1277
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,156 +1,45 @@ | ||
| /* eslint-disable jsx-a11y/control-has-associated-label */ | ||
| import React from 'react'; | ||
| import { useEffect } from 'react'; | ||
| import { useReducer } from 'react'; | ||
| import { Footer } from './components/footer/Footer'; | ||
| import { TodoList } from './components/TodoList/TodoList'; | ||
| import { Header } from './header/Header'; | ||
| import { SortContext } from './store/SortContext'; | ||
| import { SortReducer } from './store/SortReducer'; | ||
| import { TodoContext } from './store/TodoContext'; | ||
| import { TodoReducer } from './store/TodoReducer'; | ||
|
|
||
| export const App: React.FC = () => { | ||
| const initTodos = () => { | ||
| const saved = localStorage.getItem('todos'); | ||
| return saved ? JSON.parse(saved) : []; | ||
| }; | ||
|
|
||
| const [todos, dispatch] = useReducer( | ||
| TodoReducer, | ||
| [], | ||
| initTodos | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| localStorage.setItem('todos', JSON.stringify(todos)); | ||
| }, [todos]); | ||
|
|
||
| const [sortBy, sortDispatch] = useReducer(SortReducer, 'all'); | ||
|
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 uses a raw string Checklist violation (quote): const FILTERS = {
all: 'all',
completed: 'completed',
active: 'active',
};
```","lineStart":30,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: "Do not rely on the unknown string, make constants for this." The initial state for the filter reducer is hardcoded as |
||
|
|
||
| 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> | ||
| <TodoContext.Provider value={{ todos, dispatch }}> | ||
| <SortContext.Provider value={{ sortBy, sortDispatch }}> | ||
| <Header /> | ||
| <TodoList /> | ||
| {todos.length > 0 && <Footer />} | ||
| </SortContext.Provider> | ||
| </TodoContext.Provider> | ||
| </div> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import classNames from 'classnames'; | ||
| import { useRef } from 'react'; | ||
| import { useState } from 'react'; | ||
| import { useEffect } from 'react'; | ||
| import { useContext } from 'react'; | ||
| import { TodoContext } from '../../store/TodoContext'; | ||
| import { | ||
| completeTodoAction, | ||
| deleteTodoAction, | ||
| renameTodoAction, | ||
| } from '../../store/TodoReducer'; | ||
| import { TodoType } from '../../types/TodoType'; | ||
|
|
||
| type Props = { | ||
| todo: TodoType; | ||
| }; | ||
|
|
||
| export const TodoItem = ({ todo }: Props) => { | ||
| const { dispatch } = useContext(TodoContext); | ||
| const [title, setTitle] = useState(todo.title); | ||
|
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. You access 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.
|
||
| const [isEditing, setIsEditing] = useState(false); | ||
| const inputRef = useRef<HTMLInputElement>(null); | ||
|
|
||
| useEffect(() => { | ||
| setTitle(todo.title); | ||
| }, [todo.title]); | ||
|
|
||
| const cancelEditings = (event: React.KeyboardEvent<HTMLInputElement>) => { | ||
|
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. Handler name 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 function name is a bit unconventional. Per checklist item #6, function names should be clear and follow conventions. A name like |
||
| if (event.key === 'Escape') { | ||
| setTitle(todo.title); | ||
| setIsEditing(false); | ||
| } | ||
| }; | ||
|
|
||
| const editTodo = () => { | ||
| const trimmed = title.trim(); | ||
|
|
||
| if (!trimmed) { | ||
| dispatch(deleteTodoAction(todo.id)); | ||
| } else { | ||
| dispatch(renameTodoAction(todo.id, trimmed)); | ||
| } | ||
|
|
||
| setIsEditing(false); | ||
|
Comment on lines
+35
to
+44
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 |
||
| }; | ||
|
|
||
| const saveTodoOnEnter = (event: React.SyntheticEvent<HTMLFormElement>) => { | ||
| event.preventDefault(); | ||
| editTodo(); | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| if (isEditing && inputRef.current) { | ||
| inputRef.current.focus(); | ||
| inputRef.current.select(); | ||
| } | ||
| }, [isEditing]); | ||
|
|
||
| return ( | ||
| <div | ||
| data-cy="Todo" | ||
| className={classNames('todo', { completed: todo.completed })} | ||
| key={todo.id} | ||
|
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 |
||
| > | ||
| {/* eslint-disable-next-line jsx-a11y/label-has-associated-control */} | ||
| <label className="todo__status-label" htmlFor={`todo-status-${todo.id}`}> | ||
| <input | ||
| id={`todo-status-${todo.id}`} | ||
| data-cy="TodoStatus" | ||
| type="checkbox" | ||
| className="todo__status" | ||
| checked={todo.completed ? true : false} | ||
| onChange={() => dispatch(completeTodoAction(todo.id))} | ||
| /> | ||
| </label> | ||
|
|
||
| {isEditing === false ? ( | ||
| <> | ||
| <span | ||
| data-cy="TodoTitle" | ||
| className="todo__title" | ||
| onDoubleClick={() => setIsEditing(true)} | ||
| > | ||
| {todo.title} | ||
| </span> | ||
| <button | ||
| type="button" | ||
| className="todo__remove" | ||
| data-cy="TodoDelete" | ||
| onClick={() => dispatch(deleteTodoAction(todo.id))} | ||
| > | ||
| × | ||
| </button> | ||
| </> | ||
| ) : ( | ||
| <form onSubmit={saveTodoOnEnter}> | ||
| <input | ||
| ref={inputRef} | ||
| data-cy="TodoTitleField" | ||
| type="text" | ||
| className="todo__title-field" | ||
| placeholder="Empty todo will be deleted" | ||
| value={title} | ||
| onChange={event => setTitle(event.target.value)} | ||
| onKeyUp={event => cancelEditings(event)} | ||
| onBlur={() => editTodo()} | ||
| /> | ||
| </form> | ||
| )} | ||
| </div> | ||
| ); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { useContext } from 'react'; | ||
| import { SortContext } from '../../store/SortContext'; | ||
| import { TodoContext } from '../../store/TodoContext'; | ||
| import { TodoItem } from '../TodoItem.tsx/TodoItem'; | ||
|
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. Invalid import path will cause module resolution/build errors. The import reads |
||
|
|
||
| export const TodoList = () => { | ||
| const { todos } = useContext(TodoContext); | ||
| const { sortBy } = useContext(SortContext); | ||
|
|
||
| const filteredTodos = todos.filter(todo => { | ||
| if (sortBy === 'active') { | ||
| return !todo.completed; | ||
| } | ||
|
|
||
| if (sortBy === 'completed') { | ||
| return todo.completed; | ||
|
Comment on lines
+11
to
+16
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 uses raw string literals (
Comment on lines
+11
to
+16
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 file compares |
||
| } | ||
|
Comment on lines
+11
to
+17
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: 'Do not rely on the unknown string, make constants for this.' Instead of hardcoding the strings |
||
|
|
||
| return true; | ||
| }); | ||
|
|
||
| return ( | ||
| <section className="todoapp__main" data-cy="TodoList"> | ||
| {filteredTodos.map(todo => { | ||
| return <TodoItem todo={todo} key={todo.id} />; | ||
| })} | ||
| </section> | ||
| ); | ||
| }; | ||
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.
This file uses a raw string literal for the initial filter state:
useReducer(SortReducer, 'all'). This violates checklist item #10: "Do not rely on an unknown string; make constants for this (e.g.,FILTERS = { all: 'all', completed: 'completed', active: 'active' })." Use a shared FILTERS constant (e.g.FILTERS.all) here so the code is explicit and consistent with the rest of the app.