Skip to content
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 cypress/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"compilerOptions": {
"sourceMap": false
}
}
}
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"devDependencies": {
"@cypress/react18": "^2.0.1",
"@mate-academy/scripts": "^1.9.12",
"@mate-academy/scripts": "^2.1.3",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^20.14.10",
Expand Down
201 changes: 66 additions & 135 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,156 +1,87 @@
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
import React, { useState, useContext, useRef, useEffect } from 'react';
import classNames from 'classnames';
import { TodoContext } from './components/TodoContext';
import { TodoList } from './components/TodoList';
import { Footer } from './components/Footer';
import { Todo } from './types/Todo';

export const App: React.FC = () => {
const { todos, addTodo, toggleAll } = useContext(TodoContext);
const [filter, setFilter] = useState<'All' | 'Active' | 'Completed'>('All');
const [title, setTitle] = useState<string>('');

const newTodoField = useRef<HTMLInputElement>(null);

useEffect(() => {
newTodoField.current?.focus();
}, [todos.length]);

const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const trimmedTitle = title.trim();

if (!trimmedTitle) {
return;
}

addTodo(trimmedTitle);
setTitle('');
};

const visibleTodos = todos.filter((todo: Todo) => {
if (filter === 'Active') {
return !todo.completed;
}

if (filter === 'Completed') {
return todo.completed;
}

return true;
});

const isAllCompleted =
todos.length > 0 && todos.every((todo: Todo) => todo.completed);

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"
/>
{todos.length > 0 && (
<button
type="button"
className={classNames('todoapp__toggle-all', {
active: isAllCompleted,
})}
data-cy="ToggleAllButton"
onClick={toggleAll}
/>
)}

{/* Add a todo on form submit */}
<form>
<form onSubmit={handleSubmit}>
<input
data-cy="NewTodoField"
type="text"
className="todoapp__new-todo"
placeholder="What needs to be done?"
autoFocus
ref={newTodoField}
value={title}
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
setTitle(e.target.value)
}
/>
</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>
{todos.length > 0 && (
<>
<TodoList visibleTodos={visibleTodos} />
<Footer filter={filter} setFilter={setFilter} />
</>
)}
</div>
</div>
);
Expand Down
15 changes: 15 additions & 0 deletions src/UserWarning.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';

export const UserWarning: React.FC = () => (
<section className="section">
<p className="box is-size-3">
Please get your <b> userId </b>{' '}
<a href="https://mate-academy.github.io/react_student-registration">
here
</a>{' '}
and save it in the app <pre>const USER_ID = ...</pre>
All requests to the API must be sent with this
<b> userId.</b>
</p>
</section>
);
23 changes: 23 additions & 0 deletions src/components/ErrorNotification.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
type Prop = {
errorMessage: string;
setErrorMessage: (errorMessage: string) => void;
};

export const ErrorNotification = ({ errorMessage, setErrorMessage }: Prop) => {
return (
<div
data-cy="ErrorNotification"
className={`notification is-danger is-light has-text-weight-normal ${
!errorMessage ? 'hidden' : ''
}`}
>
<button
data-cy="HideErrorButton"
type="button"
className="delete"
onClick={() => setErrorMessage('')}
/>
{errorMessage}
</div>
);
};
52 changes: 52 additions & 0 deletions src/components/Footer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { useContext } from 'react';
import type { Dispatch, SetStateAction } from 'react';
import classNames from 'classnames';
import { TodoContext } from './TodoContext';

type FilterType = 'All' | 'Active' | 'Completed';

export const Footer: React.FC<{
filter: FilterType;
setFilter: Dispatch<SetStateAction<FilterType>>;
}> = ({ filter, setFilter }) => {
const { todos, clearCompleted } = useContext(TodoContext);

const activeCount = todos.filter(todo => !todo.completed).length;
const hasCompleted = todos.some(todo => todo.completed);

const filters: FilterType[] = ['All', 'Active', 'Completed'];

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

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

<button
type="button"
className="todoapp__clear-completed"
data-cy="ClearCompletedButton"
disabled={!hasCompleted}
onClick={clearCompleted}
>
Clear completed
</button>
</footer>
);
};
Loading
Loading