-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
szymonpachucki
wants to merge
5
commits into
mate-academy:master
Choose a base branch
from
szymonpachucki:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7a54662
Everything works outside of escape. Would love some advice on that.
szymonpachucki ae96632
fixed counter
szymonpachucki 060a99f
add task solution
szymonpachucki 1437e35
add task solution
szymonpachucki 5acf45c
add task solution
szymonpachucki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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