-
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
implement editing functionality #778
Open
voievodik
wants to merge
8
commits into
mate-academy:master
Choose a base branch
from
voievodik: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 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ea780aa
implement editing functionality
voievodik 812b638
provided fixes according to review
voievodik f8195a2
provided fixes according to review
voievodik 8ff9e63
provided fixes according to review
voievodik 4281a01
added toggle saving on api
voievodik 5f19eba
added loader for toggle todo
voievodik acec467
fixed onblur cancel request
voievodik 3181e58
removed todotemp component, changed loader functionality
voievodik 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,45 @@ | ||
/* 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, { useEffect, useState } from 'react'; | ||
import { TodoHeader } from './components/TodoHeader'; | ||
import { TodoList } from './components/TodoList'; | ||
import { TodoFooter } from './components/TodoFooter'; | ||
import { TodoNotification } from './components/TodoNotification'; | ||
import { useTodo } from './context/TodoContext'; | ||
import { getTodos } from './api/todos'; | ||
import { useError } from './context/ErrorContext'; | ||
import { USER_ID } from './utils/variables'; | ||
|
||
export const App: React.FC = () => { | ||
if (!USER_ID) { | ||
return <UserWarning />; | ||
} | ||
const { todos, setTodos } = useTodo(); | ||
const { setErrorMessage } = useError(); | ||
|
||
const [isActive, setIsActive] = useState(false); | ||
|
||
useEffect(() => { | ||
getTodos(USER_ID) | ||
.then(setTodos) | ||
.catch(() => { | ||
setErrorMessage('Unable to load todos'); | ||
}); | ||
}, []); | ||
|
||
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"> | ||
<TodoHeader onHandleActive={setIsActive} /> | ||
|
||
<TodoList | ||
isActive={isActive} | ||
onHandleActive={setIsActive} | ||
/> | ||
|
||
{Boolean(todos.length) && ( | ||
<TodoFooter /> | ||
)} | ||
</div> | ||
|
||
<TodoNotification /> | ||
</div> | ||
); | ||
}; |
This file was deleted.
Oops, something went wrong.
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,20 @@ | ||
import { Todo } from '../types/Todo'; | ||
import { client } from '../utils/fetchClient'; | ||
|
||
export const getTodos = (userId: number) => { | ||
return client.get<Todo[]>(`/todos?userId=${userId}`); | ||
}; | ||
|
||
export const deleteTodo = (todoId: number) => { | ||
return client.delete(`/todos/${todoId}`); | ||
}; | ||
|
||
export const createTodo = ({ | ||
userId, title, completed, | ||
}: Omit<Todo, 'id'>) => { | ||
return client.post<Todo>('/todos', { userId, title, completed }); | ||
}; | ||
|
||
export const updateTodo = (todoId: number, todo: Partial<Todo>) => { | ||
return client.patch<Todo>(`/todos/${todoId}`, todo); | ||
}; |
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,128 @@ | ||
import { | ||
useEffect, useRef, useState, | ||
} from 'react'; | ||
import classnames from 'classnames'; | ||
import { Todo } from '../../types/Todo'; | ||
import { useTodo } from '../../context/TodoContext'; | ||
import { useError } from '../../context/ErrorContext'; | ||
import { updateTodo } from '../../api/todos'; | ||
// import { useTitle } from '../../context/TitleContext'; | ||
|
||
type Props = { | ||
todo: Todo; | ||
onEditedId: () => void; | ||
onDelete: (value: Todo) => void; | ||
isLoading: boolean; | ||
onLoad: (value: boolean) => void; | ||
}; | ||
|
||
export const TodoEditItem: React.FC<Props> = ({ | ||
todo, onEditedId, onDelete, isLoading, onLoad, | ||
}) => { | ||
const { id, completed } = todo; | ||
|
||
const { todos, setTodos } = useTodo(); | ||
const { setErrorMessage } = useError(); | ||
|
||
const editInputRef = useRef<HTMLInputElement>(null); | ||
|
||
useEffect(() => { | ||
editInputRef.current?.focus(); | ||
}, []); | ||
|
||
const [newTitle, setNewTitle] = useState(todo.title); | ||
|
||
const updateNewTodo = () => { | ||
if (!newTitle.trim()) { | ||
onDelete(todo); | ||
|
||
return; | ||
} | ||
|
||
const updatedTodos = todos.map((currentTodo) => { | ||
if (currentTodo.id === id) { | ||
return { | ||
...currentTodo, | ||
title: newTitle, | ||
}; | ||
} | ||
|
||
return currentTodo; | ||
}); | ||
|
||
const updatedTodo = updatedTodos.find(({ id: updatedId }) => { | ||
return updatedId === id; | ||
}); | ||
|
||
onLoad(true); | ||
|
||
if (updatedTodo) { | ||
updateTodo(todo.id, updatedTodo) | ||
.then(() => setTodos(updatedTodos)) | ||
.catch(() => setErrorMessage('Unable to update a todo')) | ||
.finally(() => { | ||
onLoad(false); | ||
onEditedId(); | ||
}); | ||
} | ||
}; | ||
|
||
const handlePressEscape = ( | ||
event: React.KeyboardEvent<HTMLInputElement>, | ||
) => { | ||
if (event.key === 'Escape') { | ||
onEditedId(); | ||
} | ||
}; | ||
|
||
const handleEditTodo = (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
|
||
if (todo.title === newTitle) { | ||
onEditedId(); | ||
|
||
return; | ||
} | ||
|
||
updateNewTodo(); | ||
}; | ||
|
||
return ( | ||
<div data-cy="Todo" className="todo"> | ||
<label className="todo__status-label"> | ||
<input | ||
data-cy="TodoStatus" | ||
type="checkbox" | ||
className="todo__status" | ||
checked={completed} | ||
/> | ||
</label> | ||
|
||
<form onSubmit={handleEditTodo}> | ||
<input | ||
ref={editInputRef} | ||
data-cy="TodoTitleField" | ||
type="text" | ||
className="todo__title-field" | ||
placeholder="Empty todo will be deleted" | ||
value={newTitle} | ||
onChange={(event) => setNewTitle(event.target.value)} | ||
onBlur={updateNewTodo} | ||
onKeyUp={handlePressEscape} | ||
/> | ||
</form> | ||
|
||
<div | ||
data-cy="TodoLoader" | ||
className={classnames( | ||
'modal overlay', { | ||
'is-active': isLoading, | ||
}, | ||
)} | ||
> | ||
<div className="modal-background has-background-white-ter" /> | ||
<div className="loader" /> | ||
</div> | ||
</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 @@ | ||
export * from './TodoEditItem'; |
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,98 @@ | ||
import { useContext, useMemo } from 'react'; | ||
import classnames from 'classnames'; | ||
import { Status } from '../../types/Status'; | ||
import { FilterContext } from '../../context/FilterContext'; | ||
import { useTodo } from '../../context/TodoContext'; | ||
import { deleteTodo } from '../../api/todos'; | ||
import { useError } from '../../context/ErrorContext'; | ||
|
||
export const TodoFooter = () => { | ||
const { selectedFilter, setSelectedFilter } = useContext(FilterContext); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You created hooks for contexts, but not for this one) Let's create for the |
||
const { todos, setTodos } = useTodo(); | ||
const { setErrorMessage } = useError(); | ||
|
||
const isActiveItemsLeft = todos | ||
.filter(({ completed }) => !completed).length; | ||
|
||
const hasCompletedTodo = useMemo(() => { | ||
return todos.some(todo => todo.completed); | ||
}, [todos]); | ||
|
||
const clearCompleted = () => { | ||
const deletePromises = todos | ||
.filter((todo) => todo.completed) | ||
.map(({ id }) => deleteTodo(id)); | ||
|
||
Promise.all(deletePromises) | ||
.then(() => { | ||
setTodos((prevState) => prevState.filter((todo) => !todo.completed)); | ||
}) | ||
.catch(() => { | ||
setErrorMessage('Unable to delete a todo'); | ||
}); | ||
}; | ||
|
||
return ( | ||
<footer data-cy="Footer" className="todoapp__footer"> | ||
<span className="todo-count" data-cy="TodosCounter"> | ||
{isActiveItemsLeft > 1 | ||
? `${isActiveItemsLeft} items left` | ||
: '1 item left'} | ||
</span> | ||
|
||
<nav data-cy="Filter" className="filter"> | ||
<a | ||
data-cy="FilterLinkAll" | ||
href="#/" | ||
className={classnames( | ||
'filter__link', | ||
{ | ||
selected: selectedFilter === Status.All, | ||
}, | ||
)} | ||
onClick={() => setSelectedFilter(Status.All)} | ||
> | ||
All | ||
</a> | ||
|
||
<a | ||
data-cy="FilterLinkActive" | ||
href="#/active" | ||
className={classnames( | ||
'filter__link', | ||
{ | ||
selected: selectedFilter === Status.Active, | ||
}, | ||
)} | ||
onClick={() => setSelectedFilter(Status.Active)} | ||
> | ||
Active | ||
</a> | ||
|
||
<a | ||
data-cy="FilterLinkCompleted" | ||
href="#/completed" | ||
className={classnames( | ||
'filter__link', | ||
{ | ||
selected: selectedFilter === Status.Completed, | ||
}, | ||
)} | ||
onClick={() => setSelectedFilter(Status.Completed)} | ||
> | ||
Completed | ||
</a> | ||
</nav> | ||
|
||
<button | ||
data-cy="ClearCompletedButton" | ||
type="button" | ||
className="todoapp__clear-completed" | ||
disabled={!hasCompletedTodo} | ||
onClick={clearCompleted} | ||
> | ||
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 @@ | ||
export * from './TodoFooter'; |
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.
Remove comments