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

Everything works outside of escape. Would love some advice on that. #772

Open
wants to merge 5 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
87 changes: 69 additions & 18 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,75 @@
/* eslint-disable max-len */
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
import { UserWarning } from './UserWarning';

const USER_ID = 0;
import React, { useState } from 'react';
import { Todo } from './types/Todo';
import { Errors } from './types/Errors';
import { FilterOption } from './types/filterOption';
import { TodoList } from './components/todoList';
import { InCaseOfError } from './components/inCaseOfError';
import { InputOfTodos } from './components/inputOfTodos';
import { Footer } from './components/footer';

export const App: React.FC = () => {
if (!USER_ID) {
return <UserWarning />;
}
const [todos, setTodos] = useState<Todo[]>([]);
const [error, setError] = useState<Errors | null>(null);
const [filterTodos, setFilterTodos]
= useState<FilterOption>(FilterOption.ALL);
const [newTodo, setNewTodo] = useState<string>('');

const handleSetFilter = (newFilter: FilterOption) => {
setFilterTodos(newFilter);
};

const closeError = () => {
setError(null);
};

const addTodo = () => {
if (newTodo.trim() !== '') {
const newTodoItem: Todo = {
id: todos.length + 1,
title: newTodo,
completed: false,
removed: false,
editing: false,
};

setTodos([...todos, newTodoItem]);
setNewTodo('');
}
};

return (
<section className="section container">
<p className="title is-4">
Copy all you need from the prev task:
<br />
<a href="https://github.com/mate-academy/react_todo-app-add-and-delete#react-todo-app-add-and-delete">React Todo App - Add and Delete</a>
</p>

<p className="subtitle">Styles are already copied</p>
</section>
<div className="todoapp">
<h1 className="todoapp__title">todos</h1>

<div className="todoapp__content">
<InputOfTodos
setNewTodo={setNewTodo}
newTodo={newTodo}
addTodo={addTodo}
todos={todos}
setTodos={setTodos}
/>

{todos && (
<TodoList
todos={todos}
filterTodos={filterTodos}
setTodos={setTodos}
/>
)}

{todos.length > 0 && (
<Footer
handleSetFilter={handleSetFilter}
todos={todos}
setTodos={setTodos}
/>
)}
</div>

{error && (
<InCaseOfError error={error} closeError={closeError} />
)}
</div>
);
};
58 changes: 58 additions & 0 deletions src/components/footer.tsx
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

current filter should have different styles, when picked, as a user I want to know what kind of todos the app is displaying

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React from 'react';
import { TodoCount } from './todoCount';
import { Todo } from '../types/Todo';
import { FilterOption } from '../types/filterOption';

interface Props {
handleSetFilter: (newFilter: FilterOption) => void;
todos: Todo[];
setTodos: (todos: Todo[]) => void;
}

export const Footer: React.FC<Props> = ({
handleSetFilter,
todos,
setTodos,
}) => {
const handleRemoveComplited = () => {
setTodos(todos.filter((todo) => todo.completed !== true));
};

return (
<footer className="todoapp__footer">
<TodoCount todos={todos} />
<nav className="filter">
<a
href="#/"
className="filter__link selected"
onClick={() => handleSetFilter(FilterOption.ALL)}
>
All
</a>

<a
href="#/active"
className="filter__link"
onClick={() => handleSetFilter(FilterOption.ACTIVE)}
>
Active
</a>

<a
href="#/completed"
className="filter__link"
onClick={() => handleSetFilter(FilterOption.COMPLETED)}
>
Completed
</a>
</nav>
<button
type="button"
className="todoapp__clear-completed"
onClick={handleRemoveComplited}
>
Clear completed
</button>
</footer>
);
};
27 changes: 27 additions & 0 deletions src/components/inCaseOfError.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React, { useEffect } from 'react';
import { Errors } from '../types/Errors';

interface Props {
error: Errors;
closeError: () => void;
}

export const InCaseOfError: React.FC<Props> = ({ error, closeError }) => {
useEffect(() => {
const timeoutId = setTimeout(() => {
closeError();
}, 3000);

return () => {
clearTimeout(timeoutId);
};
}, [closeError]);

return (
<div className="notification is-danger is-light has-text-weight-normal">
<button type="button" className="delete" onClick={closeError}>
{error}
</button>
</div>
);
};
77 changes: 77 additions & 0 deletions src/components/inputOfTodos.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
import { Todo } from '../types/Todo';

interface Props {
setNewTodo: (newTodo: string) => void;
newTodo: string;
addTodo: (title: string) => void;
todos: Todo[];
setTodos: (todos: Todo[]) => void;
}

export const InputOfTodos: React.FC<Props> = ({
setNewTodo, newTodo, addTodo, todos, setTodos,
}) => {
const handleAddNewTodo = () => {
if (newTodo) {
addTodo(newTodo);
setNewTodo('');
}
};

const allComplited = (todos.every((t) => t.completed));

const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setNewTodo(event.target.value);
};

const handleInputKeyPress
= (event: React.KeyboardEvent<HTMLInputElement>) => {
if (event.key === 'Enter') {
event.preventDefault();
handleAddNewTodo();
}
};

const handleTaggleAll = () => {
let updatedTodos = todos;

if (!allComplited) {
updatedTodos = todos.map((t) => ({
...t,
completed: true,
}));
} else {
updatedTodos = todos.map((t) => ({
...t,
completed: !t.completed,
}));
}

setTodos(updatedTodos);
};

return (
<header className="todoapp__header">
{todos.length > 0 && (
<button
type="button"
className={`todoapp__toggle-all ${allComplited ? 'active' : ''}`}
onClick={handleTaggleAll}
/>
)}

<form>
<input
type="text"
className="todoapp__new-todo"
placeholder="What needs to be done?"
value={newTodo}
onChange={handleInputChange}
onKeyPress={handleInputKeyPress}
/>
</form>
</header>
);
};
118 changes: 118 additions & 0 deletions src/components/task.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import React, { useState, useRef, useEffect } from 'react';
import { Todo } from '../types/Todo';

type Props = {
todo: Todo;
setTodos: React.Dispatch<React.SetStateAction<Todo[]>>;
todos: Todo[];
};

export const Task: React.FC<Props> = ({ todo, setTodos, todos }) => {
const [inputValue, setInputValue] = useState<string>(todo.title);
const inputRef = useRef<HTMLInputElement | null>(null);

const handleToggleCompletion = () => {
const updatedTodo = { ...todo, completed: !todo.completed };

setTodos(todos.map((t) => (t.id === updatedTodo.id ? updatedTodo : t)));
};

const handleRemove = () => {
const updatedTodo = { ...todo, removed: true };

setTodos(todos.map((t) => (t.id === updatedTodo.id ? updatedTodo : t)));
};

const handleEditStart = () => {
const updatedTodo = { ...todo, editing: true };

setInputValue(todo.title);
setTodos(todos.map((t) => (t.id === updatedTodo.id ? updatedTodo : t)));
};

const saveChanges = () => {
const updatedTodo = { ...todo, title: inputValue, editing: false };

if (updatedTodo.title === '') {
handleRemove();
} else {
setTodos(todos.map((t) => (t.id === updatedTodo.id ? updatedTodo : t)));
}

setInputValue('');
};

const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setInputValue(event.target.value);
};

const cancelEditing = () => {
const updatedTodo = { ...todo, editing: false };

setTodos(todos.map((t) => (t.id === updatedTodo.id ? updatedTodo : t)));

setInputValue('');
};

const handleInputKeyPress
= (event: React.KeyboardEvent<HTMLInputElement>) => {
if (event.key === 'Enter') {
event.preventDefault();
saveChanges();
}
};

const handleInputEscPress
= (event: React.KeyboardEvent<HTMLInputElement>) => {
if (event.key === 'Escape') {
event.preventDefault();
cancelEditing();
}
};

const handleInputBlur = () => {
saveChanges();
};

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

return (
<div
className={todo.completed ? 'todo completed' : 'todo'}
onDoubleClick={handleEditStart}
>
<label className="todo__status-label">
<input
type="checkbox"
className="todo__status"
checked={todo.completed}
onChange={handleToggleCompletion}
/>
</label>
{!todo.editing ? (
<>
<span className="todo__title">{todo.title}</span>
<button type="button" className="todo__remove" onClick={handleRemove}>
×
</button>
</>
) : (
<input
ref={inputRef}
type="text"
className="todoapp__new"
placeholder="Empty todo will be deleted"
value={inputValue}
onChange={handleInputChange}
onKeyPress={handleInputKeyPress}
onBlur={handleInputBlur}
onKeyUp={handleInputEscPress}
/>
)}
</div>
);
};
21 changes: 21 additions & 0 deletions src/components/todoCount.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';
import { Todo } from '../types/Todo';

interface Props {
todos: Todo[];
}

export const TodoCount: React.FC<Props> = ({ todos }) => {
const todosLeftCount = todos.filter((todo) => !todo.completed
&& !todo.removed).length;

return (
<span className="todo-count">
{todosLeftCount}
{' '}
{todosLeftCount === 1 ? 'item' : 'items'}
{' '}
left
</span>
);
};
Loading
Loading