Skip to content

Commit

Permalink
react app v.2
Browse files Browse the repository at this point in the history
  • Loading branch information
ZupaFly committed Sep 24, 2024
1 parent c47ade5 commit 1d796f7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Storage/storageFiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const reducer = (state: State, action: Action): State => {
return {
...state,
todos: [...state.todos.filter(todo => todo.id !== action.id)],
focusNewTodo: !state.focusNewTodo,
focusNewTodo: true,
};

case 'checked':
Expand Down
22 changes: 9 additions & 13 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,21 @@ import cn from 'classnames';
export const Header = () => {
const dispatch = useContext(DispatchContext);
const { todos, newTodo, focusNewTodo } = useContext(StateContext);
const inputRef = useRef<HTMLInputElement>(null);

const allTodosComplete = todos.reduce((prev, todo) => {
return prev && todo.completed;
}, true);
const allTodosComplete = todos.reduce(
(prev, todo) => prev && todo.completed,
true,
);

const addNewTodo = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();

if (newTodo.trim()) {
dispatch({ type: 'add' });
dispatch({
type: 'changeTodo',
text: '',
});
dispatch({ type: 'changeTodo', text: '' });
}
};

const inputRef = useRef<HTMLInputElement>(null);

useEffect(() => {
if (focusNewTodo) {
inputRef.current?.focus();
Expand All @@ -37,9 +33,7 @@ export const Header = () => {
{todos.length > 0 && (
<button
type="button"
className={cn('todoapp__toggle-all', {
active: allTodosComplete,
})}
className={cn('todoapp__toggle-all', { active: allTodosComplete })}
data-cy="ToggleAllButton"
onClick={() =>
dispatch({ type: 'setAllCompleate', use: allTodosComplete })
Expand All @@ -55,6 +49,8 @@ export const Header = () => {
className="todoapp__new-todo"
placeholder="What needs to be done?"
value={newTodo}
onClick={() => dispatch({ type: 'setFocudNewTodo' })}
onBlur={() => dispatch({ type: 'setFocudNewTodo' })}
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
dispatch({
type: 'changeTodo',
Expand Down
2 changes: 1 addition & 1 deletion src/components/Todos.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext } from 'react';
import React, { useContext } from 'react';
import { DispatchContext, StateContext } from '../Storage/storageFiles';
import { TodoItem } from './Todo';

Expand Down

0 comments on commit 1d796f7

Please sign in to comment.