Skip to content

Commit

Permalink
update_footer
Browse files Browse the repository at this point in the history
  • Loading branch information
inesshtepa committed Dec 12, 2024
1 parent 0da38f6 commit 809c804
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 49 deletions.
55 changes: 9 additions & 46 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import * as todoService from './api/todos';
import { FilterOptions } from './types/FilterOptions';
import { Todolist } from './components/TodoList';
import classNames from 'classnames';
import { Footer } from './components/Footer';

export const App: React.FC = () => {
const [loading, setLoading] = useState(false);
const [todos, setTodos] = useState<Todo[]>([]);
const [currentFilter, setCurrentFilter] = useState<FilterOptions>(
FilterOptions.All,
);
const [currentFilter, setCurrentFilter] = useState<string>('All');
const [errorMessage, setErrorMessage] = useState('');
const [newTodoTitle, setNewTodoTitle] = useState('');
const [tempTodo, setTempTodo] = useState<Todo | null>(null);
Expand Down Expand Up @@ -113,8 +112,6 @@ export const App: React.FC = () => {
}
};

const activeTodos = todos.filter(todo => !todo.completed).length;

const completedAllTodos = () => {
const allCompleted = todos.every(todo => todo.completed);

Expand Down Expand Up @@ -171,16 +168,6 @@ export const App: React.FC = () => {
return <UserWarning />;
}

const handleClearCompleted = () => {
const completedTodoId = todos
.filter(todo => todo.completed)
.map(todo => todo.id);

completedTodoId.forEach(todo => {
handleDelete(todo);
});
};

return (
<>
<div className="todoapp">
Expand Down Expand Up @@ -228,37 +215,13 @@ export const App: React.FC = () => {
/>

{todos.length !== 0 && (
<footer className="todoapp__footer" data-cy="Footer">
<span className="todo-count" data-cy="TodosCounter">
{`${activeTodos} items left`}
</span>

<nav className="filter" data-cy="Filter">
{Object.values(FilterOptions).map(option => (
<a
key={option}
href={`#/${option}`}
className={classNames('filter__link', {
selected: currentFilter === option,
})}
data-cy={`FilterLink${option}`}
onClick={() => setCurrentFilter(option)}
>
{option}
</a>
))}
</nav>

<button
type="button"
className="todoapp__clear-completed"
data-cy="ClearCompletedButton"
onClick={() => handleClearCompleted()}
disabled={todos.every(todo => !todo.completed) || loading}
>
Clear completed
</button>
</footer>
<Footer
todos={todos}
onDelete={handleDelete}
currentFilter={currentFilter}
setCurrentFilter={setCurrentFilter}
loading={loading}
/>
)}
</div>

Expand Down
68 changes: 68 additions & 0 deletions src/components/Footer/Footer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* eslint-disable jsx-a11y/label-has-associated-control */
import React from 'react';

import { Todo } from '../../types/Todo';
import { FilterOptions } from '../../types/FilterOptions';
import classNames from 'classnames';

type Props = {
todos: Todo[];
onDelete: (todoId: number) => void;
currentFilter: string;
setCurrentFilter: (currentFilter: string) => void;
loading: boolean;
};

export const Footer: React.FC<Props> = ({
todos,
onDelete,
currentFilter,
setCurrentFilter,
loading,
}) => {
const activeTodos = todos.filter(todo => !todo.completed).length;

const handleClearCompleted = () => {
const completedTodoId = todos
.filter(todo => todo.completed)
.map(todo => todo.id);

completedTodoId.forEach(todo => {
onDelete(todo);
});
};

return (
<footer className="todoapp__footer" data-cy="Footer">
<span className="todo-count" data-cy="TodosCounter">
{`${activeTodos} items left`}
</span>

<nav className="filter" data-cy="Filter">
{Object.values(FilterOptions).map(option => (
<a
key={option}
href={`#/${option}`}
className={classNames('filter__link', {
selected: currentFilter === option,
})}
data-cy={`FilterLink${option}`}
onClick={() => setCurrentFilter(option)}
>
{option}
</a>
))}
</nav>

<button
type="button"
className="todoapp__clear-completed"
data-cy="ClearCompletedButton"
onClick={() => handleClearCompleted()}
disabled={todos.every(todo => !todo.completed) || loading}
>
Clear completed
</button>
</footer>
);
};
1 change: 1 addition & 0 deletions src/components/Footer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Footer';
8 changes: 5 additions & 3 deletions src/components/todo_filters/TodoFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ export const TodoFilter: React.FC<Props> = ({ setFilter }) => {
data-cy="statusSelect"
onChange={element => setFilter(element.target.value)}
>
<option value={FilterOptions.All}>All</option>
<option value={FilterOptions.Active}>Active</option>
<option value={FilterOptions.Completed}>Completed</option>
<option value={FilterOptions.All}>{FilterOptions.All}</option>
<option value={FilterOptions.Active}>{FilterOptions.Active}</option>
<option value={FilterOptions.Completed}>
{FilterOptions.Completed}
</option>
</select>
</span>
</p>
Expand Down

0 comments on commit 809c804

Please sign in to comment.