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

add solution #1088

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ Implement a simple [TODO app](https://mate-academy.github.io/react_todo-app/) th
- Implement a solution following the [React task guidelines](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline).
- Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript).
- Open another terminal and run tests with `npm test` to ensure your solution is correct.
- Replace `<your_account>` with your GitHub username in the [DEMO LINK](https://<your_account>.github.io/react_todo-app/) and add it to the PR description.
- Replace `<your_account>` with your GitHub username in the [DEMO LINK](https://associate2coder.github.io/react_todo-app/) and add it to the PR description.
148 changes: 6 additions & 142 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,156 +1,20 @@
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
import { Footer } from './components/Footer';
import { Header } from './components/Header';
import { TodoList } from './components/TodoList';

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"
/>
<Header />

{/* 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>
<TodoList />

<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>
<Footer />
</div>
</div>
);
Expand Down
42 changes: 42 additions & 0 deletions src/components/Filter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import cn from 'classnames';

import { FilterOption } from '../types/FilterOption';
import { useCallback, useContext } from 'react';
import { StateContext } from '../context/GlobalContextProvider';
import { DispatchContext } from '../context/GlobalContextProvider';

export const Filter: React.FC = () => {
const { filter } = useContext(StateContext);
const dispatch = useContext(DispatchContext);

const handleFilterSelection = useCallback(
(option: FilterOption) =>
dispatch({
type: 'setFilter',
payload: option,
}),
[dispatch],
);

return (
<nav className="filter" data-cy="Filter">
{Object.values(FilterOption).map(curOption => {
const capitalizedFilter = `${curOption.charAt(0).toUpperCase()}${curOption.substring(1)}`;

return (
<a
key={curOption}
href={`#/${curOption === 'all' ? '' : curOption}`}
className={cn('filter__link', {
selected: filter === curOption,
})}
data-cy={`FilterLink${capitalizedFilter}`}
onClick={() => handleFilterSelection(curOption)}
>
{capitalizedFilter}
</a>
);
})}
</nav>
);
};
46 changes: 46 additions & 0 deletions src/components/Footer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { useCallback, useContext, useMemo } from 'react';
import { Filter } from './Filter';
import { StateContext } from '../context/GlobalContextProvider';
import { DispatchContext } from '../context/GlobalContextProvider';

export const Footer: React.FC = () => {
const { todos } = useContext(StateContext);
const dispatch = useContext(DispatchContext);

const [todoCount, completedTodoCount, activeTodoCount] = useMemo(() => {
const allCount = todos.length;
const completedCount = todos.filter(todo => todo.completed).length;
const activeCount = allCount - completedCount;

return [allCount, completedCount, activeCount];
}, [todos]);

const handleClearCompleted = useCallback(() => {
dispatch({
type: 'setTodos',
payload: todos.filter(todo => !todo.completed),
});
}, [dispatch, todos]);

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

<Filter />

<button
type="button"
className="todoapp__clear-completed"
disabled={!completedTodoCount} //disabled if there are no completed todos
onClick={handleClearCompleted}
data-cy="ClearCompletedButton"
>
Clear completed
</button>
</footer>
)
);
};
44 changes: 44 additions & 0 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import cn from 'classnames';

import { useCallback, useContext, useMemo } from 'react';
import { NewTodoForm } from './NewTodoForm';
import { StateContext } from '../context/GlobalContextProvider';
import { DispatchContext } from '../context/GlobalContextProvider';

export const Header: React.FC = () => {
const { todos } = useContext(StateContext);
const dispatch = useContext(DispatchContext);

const toggleAllBtnShown = useMemo(() => todos.length !== 0, [todos.length]);
const toggleAllBtnActive = useMemo(
() => todos.every(todo => todo.completed),
[todos],
);

const handleToggleAllTodos = useCallback(() => {
const newStatus = todos.some(todo => !todo.completed);

const updatedTodos = todos.map(todo => {
return todo.completed === newStatus
? todo
: { ...todo, completed: newStatus };
});

dispatch({ type: 'setTodos', payload: updatedTodos });
}, [dispatch, todos]);

return (
<header className="todoapp__header">
{toggleAllBtnShown && (
<button
type="button"
className={cn('todoapp__toggle-all', { active: toggleAllBtnActive })}
onClick={handleToggleAllTodos}
data-cy="ToggleAllButton"
/>
)}

<NewTodoForm />
</header>
);
};
61 changes: 61 additions & 0 deletions src/components/NewTodoForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { useCallback, useContext, useEffect, useRef, useState } from 'react';
import { todoService } from '../services/todoService';
import { DispatchContext } from '../context/GlobalContextProvider';
import { StateContext } from '../context/GlobalContextProvider';

export const NewTodoForm: React.FC = () => {
const dispatch = useContext(DispatchContext);
const { todos } = useContext(StateContext);

const [title, setTitle] = useState('');
const inputElementRef = useRef<HTMLInputElement>(null);

// handling return of focus to the Form when todos change
useEffect(() => {
inputElementRef.current?.focus();
}, [todos]);

// Handle Form submit
const handleSubmit = useCallback(
(e: React.FormEvent) => {
e.preventDefault();

const inputElement = inputElementRef.current;

if (inputElement) {
inputElement.disabled = true; // disable while

const trimmedTitle = title.trim();

// If there is a title, form submited and field cleared
if (trimmedTitle) {
dispatch({
type: 'addTodo',
payload: todoService.create(trimmedTitle),
});
setTitle('');
}

// When submitted or title is empty, form enabled and focused
inputElement.disabled = false;
inputElement.focus();
}
},
[dispatch, title],
);

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