Skip to content
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

Solution #1072

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 6 additions & 148 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,157 +1,15 @@
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
import { Content } from './components/Content';
import { TodosProvider } from './context/TodosContex';

export const App: React.FC = () => {
return (
<div className="todoapp">
<h1 className="todoapp__title">todos</h1>
<TodosProvider>
<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>
</div>
<Content />
</TodosProvider>
</div>
);
};
44 changes: 44 additions & 0 deletions src/components/Content.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React, { useContext, useEffect, useState } from 'react';
import { Header } from './Header';
import { TodoList } from './TodoList';
import { Footer } from './Footer';
import { TodosContext } from '../context/TodosContex';

export const Content: React.FC = () => {
const { todos, setTodos } = useContext(TodosContext);

const [focused, setFocused] = useState(true);
const [visibleTodos, setVisibleTodos] = useState(todos);

useEffect(() => {
localStorage.setItem('todos', JSON.stringify(todos));
}, [todos]);

const savedTodos = localStorage.getItem('todos');

useEffect(() => {
if (savedTodos) {
setTodos(JSON.parse(savedTodos));
}
}, []);

Check warning on line 23 in src/components/Content.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

React Hook useEffect has missing dependencies: 'savedTodos' and 'setTodos'. Either include them or remove the dependency array

useEffect(() => {
setVisibleTodos(todos);
}, [todos]);

return (
<div className="todoapp__content">
<Header focused={focused} setFocused={setFocused} />

<TodoList visibleTodos={visibleTodos} setFocused={setFocused} />

{todos.length > 0 && (
<Footer
visibleTodos={visibleTodos}
setVisibleTodos={setVisibleTodos}
setFocused={setFocused}
/>
)}
</div>
);
};
110 changes: 110 additions & 0 deletions src/components/Footer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import React, { useCallback, useContext, useEffect, useState } from 'react';
import { Filter } from '../types/Filter';
import classNames from 'classnames';
import { TodosContext } from '../context/TodosContex';
import { Todo } from '../types/Todo';

type Props = {
visibleTodos: Todo[];
setVisibleTodos: (vilteredTodos: Todo[]) => void;
setFocused: (value: boolean) => void;
};

export const Footer: React.FC<Props> = ({
visibleTodos,
setVisibleTodos,
setFocused,
}) => {
const { todos, setTodos } = useContext(TodosContext);

const [selectedFilter, setSelectedFilter] = useState<Filter>(Filter.All);

const findFilterKey = (value: string): string | undefined => {
return Object.keys(Filter).find(
key => Filter[key as keyof typeof Filter] === value,
);
};

const todosCount = useCallback(
(type: Filter.Active | Filter.Completed) => {
const value = type === Filter.Active ? false : true;

return todos.filter(todo => todo.completed === value).length;
},
[todos],
);

const filterFunction = (filter: Filter) => {
switch (filter) {
case Filter.All:
setVisibleTodos(todos);
break;

case Filter.Active:
setVisibleTodos(todos.filter(todo => !todo.completed));
break;

case Filter.Completed:
setVisibleTodos(todos.filter(todo => todo.completed));
break;

default:
setTodos(todos);
}
};

const clearFunction = () => {
const completedTodos = todos
.filter(todo => todo.completed)
.map(todo => todo.id);

setTodos(todos.filter(todo => !completedTodos.includes(todo.id)));
setFocused(true);
};

const setFilter = (filter: Filter) => {
setSelectedFilter(filter);

filterFunction(filter);
};

useEffect(() => {
if (selectedFilter) {
filterFunction(selectedFilter);
}
}, [visibleTodos]);

Check warning on line 75 in src/components/Footer.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

React Hook useEffect has missing dependencies: 'filterFunction' and 'selectedFilter'. Either include them or remove the dependency array

return (
<footer className="todoapp__footer" data-cy="Footer">
<span className="todo-count" data-cy="TodosCounter">
{todosCount(Filter.Active)} items left
</span>

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

<button
type="button"
className="todoapp__clear-completed"
data-cy="ClearCompletedButton"
disabled={todosCount(Filter.Completed) === 0}
onClick={() => clearFunction()}
>
Clear completed
</button>
</footer>
);
};
Loading
Loading