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 all 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: 5 additions & 149 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,157 +1,13 @@
/* 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>

<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>
<TodosProvider>
<Content />
</TodosProvider>
</div>
);
};
46 changes: 46 additions & 0 deletions src/components/Content.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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 [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 22 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 (
<>
<h1 className="todoapp__title">todos</h1>

<div className="todoapp__content">
<Header />

<TodoList visibleTodos={visibleTodos} />

{todos.length > 0 && (
<Footer
visibleTodos={visibleTodos}
setVisibleTodos={setVisibleTodos}
/>
)}
</div>
</>
);
};
100 changes: 100 additions & 0 deletions src/components/Footer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
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;
};

export const Footer: React.FC<Props> = ({ visibleTodos, setVisibleTodos }) => {
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 = () => {
setTodos(todos.filter(t => !t.completed));
};

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

filterFunction(filter);
};

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

Check warning on line 65 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>
);
};
83 changes: 83 additions & 0 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import React, { useContext, useEffect, useRef, useState } from 'react';
import { TodosContext } from '../context/TodosContex';
import classNames from 'classnames';
import { Todo } from '../types/Todo';

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

const [title, setTitle] = useState('');

const inputRef = useRef<HTMLInputElement>(null);

useEffect(() => {
if (inputRef.current) {
inputRef.current.focus();
}
}, [todos]);

const checkActiveTodos = (): boolean => {
if (todos.length > 0) {
return todos.every(todo => todo.completed);
}

return false;
};

const onSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();

const normalizedTitle = title.trim();

if (!normalizedTitle) {
return;
}

const newTodo: Todo = {
id: +new Date(),
title: normalizedTitle,
completed: false,
};

setTodos([...todos, newTodo]);
setTitle('');
};

const toggleAll = () => {
const allCompleted = checkActiveTodos();

const toggledTodos = todos.map(todo => ({
...todo,
completed: allCompleted ? false : true,
}));

setTodos(toggledTodos);
};

return (
<header className="todoapp__header">
{todos.length > 0 && (
<button
type="button"
className={classNames('todoapp__toggle-all', {
active: checkActiveTodos(),
})}
onClick={toggleAll}
data-cy="ToggleAllButton"
/>
)}

<form onSubmit={event => onSubmit(event)}>
<input
data-cy="NewTodoField"
ref={inputRef}
value={title}
onChange={event => setTitle(event.target.value)}
type="text"
className="todoapp__new-todo"
placeholder="What needs to be done?"
/>
</form>
</header>
);
};
Loading
Loading