Skip to content

Commit

Permalink
fixing code
Browse files Browse the repository at this point in the history
  • Loading branch information
mbulchak committed Nov 15, 2024
1 parent 42def51 commit 5fa9eb7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 44 deletions.
28 changes: 16 additions & 12 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import { useEffect, useState, FC } from 'react';
import { Header } from './components/Header';
import { TodoList } from './components/TodoList';
import { Footer } from './components/Footer';
Expand All @@ -11,7 +11,7 @@ import { Errors } from './types/Errors';
import { Filter } from './types/Filters';
import { todosService } from './api';

export const App: React.FC = () => {
export const App: FC = () => {
const [todos, setTodos] = useState<Todo[]>([]);
const [filter, setFilter] = useState(Filter.All);

Expand All @@ -29,6 +29,15 @@ export const App: React.FC = () => {
return !todo.completed ? accum + 1 : accum;
}, 0);

const handleGetTodos = () => {
todosService
.getTodos()
.then(setTodos)
.catch(() => {
setErrorMessage(Errors.LOADING);
});
};

const handleDeleteTodo = (id: number) => {
setLoadingTodoId(currentIds => [...currentIds, id]);

Expand Down Expand Up @@ -80,12 +89,7 @@ export const App: React.FC = () => {
};

useEffect(() => {
todosService
.getTodos()
.then(setTodos)
.catch(() => {
setErrorMessage(Errors.LOADING);
});
handleGetTodos();
}, []);

return (
Expand All @@ -101,7 +105,7 @@ export const App: React.FC = () => {
todos={todos}
setTodos={setTodos}
setErrorMessage={setErrorMessage}
handleUpdateTodo={handleUpdateTodo}
onUpdateTodo={handleUpdateTodo}
/>

{todos.length > 0 && (
Expand All @@ -110,8 +114,8 @@ export const App: React.FC = () => {
todos={filteredTodos}
tempTodo={tempTodo}
loadingTodoId={loadingTodoId}
handleDeleteTodo={handleDeleteTodo}
handleUpdateTodo={handleUpdateTodo}
onDeleteTodo={handleDeleteTodo}
onUpdateTodo={handleUpdateTodo}
editedTodo={editedTodo}
setEditedTodo={setEditedTodo}
/>
Expand All @@ -121,7 +125,7 @@ export const App: React.FC = () => {
filter={filter}
setFilter={setFilter}
todos={todos}
handleDeleteTodo={handleDeleteTodo}
onDeleteTodo={handleDeleteTodo}
/>
</>
)}
Expand Down
4 changes: 2 additions & 2 deletions src/components/ErrorNotification/ErrorNotification.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import cn from 'classnames';
import { Dispatch, SetStateAction, useEffect } from 'react';
import { Dispatch, SetStateAction, useEffect, FC } from 'react';
import { Errors } from '../../types/Errors';

type Props = {
errorMessage: string;
setErrorMessage: Dispatch<SetStateAction<Errors>>;
};

export const ErrorNotification: React.FC<Props> = ({
export const ErrorNotification: FC<Props> = ({
errorMessage,
setErrorMessage,
}) => {
Expand Down
12 changes: 6 additions & 6 deletions src/components/Footer/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import { FC } from 'react';
import { Filter } from '../../types/Filters';
import cn from 'classnames';
import { Todo } from '../../types/Todo';
import cn from 'classnames';

type Props = {
countActiveTodos: number;
filter: Filter;
setFilter: (filter: Filter) => void;
todos: Todo[];
handleDeleteTodo: (id: number) => void;
onDeleteTodo: (id: number) => void;
};

export const Footer: React.FC<Props> = ({
export const Footer: FC<Props> = ({
countActiveTodos,
filter,
setFilter,
todos,
handleDeleteTodo,
onDeleteTodo,
}) => {
const filterOptionName = Object.values(Filter);

const completedTodos = todos.filter(todo => todo.completed);

const deleteAllCompletedTodos = () => {
completedTodos.map(todo => handleDeleteTodo(todo.id));
completedTodos.map(todo => onDeleteTodo(todo.id));
};

return (
Expand Down Expand Up @@ -54,7 +55,6 @@ export const Footer: React.FC<Props> = ({
})}
</nav>

{/* this button should be disabled if there are no completed todos */}
<button
type="button"
className="todoapp__clear-completed"
Expand Down
11 changes: 6 additions & 5 deletions src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, {
useEffect,
useRef,
useState,
FC,
} from 'react';
import cn from 'classnames';
import { Todo } from '../../types/Todo';
Expand All @@ -20,19 +21,19 @@ type Props = {

setErrorMessage: (Error: Errors) => void;

handleUpdateTodo: (todoToUpdate: Todo) => void;
onUpdateTodo: (todoToUpdate: Todo) => void;

loadingTodoId: number[];
setLoadingTodoId: React.Dispatch<React.SetStateAction<number[]>>;
};

export const Header: React.FC<Props> = ({
export const Header: FC<Props> = ({
tempTodo,
setTempTodo,
setErrorMessage,
todos,
setTodos,
handleUpdateTodo,
onUpdateTodo,
loadingTodoId,
setLoadingTodoId,
}) => {
Expand Down Expand Up @@ -101,11 +102,11 @@ export const Header: React.FC<Props> = ({

if (allCompletedTodos) {
defTodos.forEach(defTodo =>
handleUpdateTodo({ ...defTodo, completed: false }),
onUpdateTodo({ ...defTodo, completed: false }),
);
} else {
activeTodos.forEach(defTodo =>
handleUpdateTodo({ ...defTodo, completed: true }),
onUpdateTodo({ ...defTodo, completed: true }),
);
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/components/TodoItem/TodoItem.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
/* eslint-disable jsx-a11y/label-has-associated-control */
/* eslint-disable jsx-a11y/control-has-associated-label */

import React, { useEffect, useRef, useState } from 'react';
import { useEffect, useRef, useState, FC } from 'react';
import { Todo } from '../../types/Todo';
import cn from 'classnames';

type Props = {
todo: Todo;
loadingTodoId: number[];

handleDeleteTodo: (id: number) => void;
handleUpdateTodo: (todoToUpdate: Todo) => void;
onDeleteTodo: (id: number) => void;
onUpdateTodo: (todoToUpdate: Todo) => void;

editedTodo: Todo | null;
setEditedTodo: React.Dispatch<React.SetStateAction<Todo | null>>;
};

export const TodoItem: React.FC<Props> = ({
export const TodoItem: FC<Props> = ({
todo,
loadingTodoId,
handleDeleteTodo,
handleUpdateTodo,
onDeleteTodo,
onUpdateTodo,
editedTodo,
setEditedTodo,
}) => {
Expand All @@ -36,7 +36,7 @@ export const TodoItem: React.FC<Props> = ({
const preparedTitle = newTitle.trim();

if (!preparedTitle) {
handleDeleteTodo(todo.id);
onDeleteTodo(todo.id);

return;
}
Expand All @@ -47,7 +47,7 @@ export const TodoItem: React.FC<Props> = ({
return;
}

handleUpdateTodo({ ...todo, title: preparedTitle });
onUpdateTodo({ ...todo, title: preparedTitle });
};

const handleTitleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
Expand Down Expand Up @@ -82,7 +82,7 @@ export const TodoItem: React.FC<Props> = ({
checked={todo.completed}
disabled={!todo.id}
onChange={event =>
handleUpdateTodo({ ...todo, completed: event.target.checked })
onUpdateTodo({ ...todo, completed: event.target.checked })
}
/>
</label>
Expand Down Expand Up @@ -114,7 +114,7 @@ export const TodoItem: React.FC<Props> = ({
type="button"
className="todo__remove"
data-cy="TodoDelete"
onClick={() => handleDeleteTodo(todo.id)}
onClick={() => onDeleteTodo(todo.id)}
>
×
</button>
Expand Down
19 changes: 10 additions & 9 deletions src/components/TodoList/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FC } from 'react';
import { Todo } from '../../types/Todo';
import { TodoItem } from '../TodoItem';

Expand All @@ -6,19 +7,19 @@ type Props = {
tempTodo: Todo | null;
loadingTodoId: number[];

handleDeleteTodo: (id: number) => void;
handleUpdateTodo: (todoToUpdate: Todo) => void;
onDeleteTodo: (id: number) => void;
onUpdateTodo: (todoToUpdate: Todo) => void;

editedTodo: Todo | null;
setEditedTodo: React.Dispatch<React.SetStateAction<Todo | null>>;
};

export const TodoList: React.FC<Props> = ({
export const TodoList: FC<Props> = ({
todos,
tempTodo,
loadingTodoId,
handleDeleteTodo,
handleUpdateTodo,
onDeleteTodo,
onUpdateTodo,
editedTodo,
setEditedTodo,
}) => {
Expand All @@ -30,8 +31,8 @@ export const TodoList: React.FC<Props> = ({
key={todo.id}
todo={todo}
loadingTodoId={loadingTodoId}
handleDeleteTodo={handleDeleteTodo}
handleUpdateTodo={handleUpdateTodo}
onDeleteTodo={onDeleteTodo}
onUpdateTodo={onUpdateTodo}
editedTodo={editedTodo}
setEditedTodo={setEditedTodo}
/>
Expand All @@ -42,8 +43,8 @@ export const TodoList: React.FC<Props> = ({
<TodoItem
todo={tempTodo}
loadingTodoId={loadingTodoId}
handleDeleteTodo={handleDeleteTodo}
handleUpdateTodo={handleUpdateTodo}
onDeleteTodo={onDeleteTodo}
onUpdateTodo={onUpdateTodo}
editedTodo={editedTodo}
setEditedTodo={setEditedTodo}
/>
Expand Down

0 comments on commit 5fa9eb7

Please sign in to comment.