-
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
Solutiion #1511
Open
ab3MN
wants to merge
4
commits into
mate-academy:master
Choose a base branch
from
ab3MN: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
Solutiion #1511
Changes from all commits
Commits
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,26 +1,16 @@ | ||
/* eslint-disable max-len */ | ||
/* eslint-disable jsx-a11y/control-has-associated-label */ | ||
import React from 'react'; | ||
import { UserWarning } from './UserWarning'; | ||
|
||
const USER_ID = 0; | ||
// /* eslint-disable jsx-a11y/control-has-associated-label */ | ||
// /* eslint-disable jsx-a11y/label-has-associated-control */ | ||
|
||
export const App: React.FC = () => { | ||
if (!USER_ID) { | ||
return <UserWarning />; | ||
} | ||
|
||
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> | ||
import { USER_ID } from './api/todos'; | ||
import { Todos } from './components/Todo/Todos'; | ||
import { TodosProvider } from './context/TodoContext'; | ||
import { UserWarning } from './UserWarning'; | ||
|
||
<p className="subtitle">Styles are already copied</p> | ||
</section> | ||
export const App = () => | ||
!USER_ID ? ( | ||
<UserWarning /> | ||
) : ( | ||
<TodosProvider> | ||
<Todos /> | ||
</TodosProvider> | ||
); | ||
}; |
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,24 @@ | ||
import { TODOS_API_PATH } from '../constants/api'; | ||
import { Todo } from '../types/Todo'; | ||
import { client } from '../utils/fetchClient'; | ||
|
||
export const USER_ID = 1826; | ||
|
||
export const getTodos = () => { | ||
return client.get<Todo[]>(`${TODOS_API_PATH}?userId=${USER_ID}`); | ||
}; | ||
|
||
export const getTodoById = (id: number) => client.get(`/todos/${id}`); | ||
|
||
export const getAllCompletedTodoByUserId = (id: number) => { | ||
client.get(`${TODOS_API_PATH}?userId=${id}&completed=true`); | ||
}; | ||
|
||
export const addTodo = ({ title, completed, userId }: Omit<Todo, 'id'>) => | ||
client.post<Todo>(TODOS_API_PATH, { title, completed, userId }); | ||
|
||
export const updateTodo = (todo: Todo, id: number): Promise<Todo> => | ||
client.patch(`${TODOS_API_PATH}/${id}`, todo); | ||
|
||
export const deleteTodo = (id: number) => | ||
client.delete(`${TODOS_API_PATH}/${id}`); |
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 { FC } from 'react'; | ||
import { getTodoErrorsMessage } from '../../utils/todos/getTodoErrorsMessage'; | ||
import cn from 'classnames'; | ||
import { TodoErrors } from '../../utils/enums/TodoErrors'; | ||
|
||
interface ErrorNotificationProps { | ||
error: TodoErrors | null; | ||
} | ||
|
||
export const ErrorNotification: FC<ErrorNotificationProps> = ({ error }) => ( | ||
<div | ||
data-cy="ErrorNotification" | ||
className={cn('notification is-danger is-light has-text-weight-normal', { | ||
hidden: !error, | ||
})} | ||
> | ||
<button data-cy="HideErrorButton" type="button" className="delete" /> | ||
{error && getTodoErrorsMessage(error)} | ||
</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,61 @@ | ||
import { Dispatch, FC, SetStateAction } from 'react'; | ||
import cn from 'classnames'; | ||
|
||
import { Todo } from '../../../types/Todo'; | ||
import { TODO_FILTER_OPTIONS } from '../../../constants/TodoFilter'; | ||
|
||
import { FilterStatuses } from '../../../utils/enums/FilterStatuses'; | ||
import { | ||
getInCompletedTodos, | ||
hasCompletedTodos, | ||
} from '../../../utils/todos/getTodos'; | ||
import { useDeleteTodo } from '../../../hooks/useDeleteTodo'; | ||
|
||
interface TodoFooterProps { | ||
todos: Todo[]; | ||
setStatus: Dispatch<SetStateAction<FilterStatuses>>; | ||
status: FilterStatuses; | ||
} | ||
|
||
export const TodoFooter: FC<TodoFooterProps> = ({ | ||
todos, | ||
setStatus, | ||
status, | ||
}) => { | ||
const isCompletedTodoCounter = getInCompletedTodos(todos).length; | ||
const { handleDeleteCompletedTodos } = useDeleteTodo(); | ||
|
||
return ( | ||
<footer className="todoapp__footer" data-cy="Footer"> | ||
<span className="todo-count" data-cy="TodosCounter"> | ||
{isCompletedTodoCounter} | ||
{isCompletedTodoCounter === 1 ? ' item ' : ' items '} | ||
left | ||
</span> | ||
|
||
<nav className="filter" data-cy="Filter"> | ||
{TODO_FILTER_OPTIONS.map(({ value, title, href, id }) => ( | ||
<a | ||
href={href} | ||
className={cn('filter__link', { selected: status === value })} | ||
data-cy={`FilterLink${title}`} | ||
key={id} | ||
onClick={() => setStatus(value)} | ||
> | ||
{title} | ||
</a> | ||
))} | ||
</nav> | ||
|
||
<button | ||
type="button" | ||
className="todoapp__clear-completed" | ||
data-cy="ClearCompletedButton" | ||
disabled={!hasCompletedTodos(todos)} | ||
onClick={handleDeleteCompletedTodos} | ||
> | ||
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,25 @@ | ||
import { useContext } from 'react'; | ||
|
||
import { TodosContext } from '../../../context/TodoContext'; | ||
import { useTodoFormManager } from '../../../hooks/useTodoFormManager'; | ||
|
||
export const TodoForm = () => { | ||
const { inputRef } = useContext(TodosContext); | ||
const { title, handleSubmit, handleChangeTitle, isInputDisabled } = | ||
useTodoFormManager(); | ||
|
||
return ( | ||
<form onSubmit={handleSubmit}> | ||
<input | ||
data-cy="NewTodoField" | ||
type="text" | ||
className="todoapp__new-todo" | ||
placeholder="What needs to be done?" | ||
value={title} | ||
ref={inputRef} | ||
disabled={isInputDisabled} | ||
onChange={handleChangeTitle} | ||
/> | ||
</form> | ||
); | ||
}; |
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,32 @@ | ||
import { FC, useEffect, useMemo, useState } from 'react'; | ||
Check failure on line 1 in src/components/Todo/TodoHeader/TodoHeader.tsx GitHub Actions / run_linter (20.x)
Check failure on line 1 in src/components/Todo/TodoHeader/TodoHeader.tsx GitHub Actions / run_linter (20.x)
|
||
import cn from 'classnames'; | ||
|
||
import { Todo } from '../../../types/Todo'; | ||
import { isAllTodosCompleted } from '../../../utils/todos/getTodos'; | ||
import { TodoForm } from '../TodoForm/TodoForm'; | ||
import { useTodoFormManager } from '../../../hooks/useTodoFormManager'; | ||
|
||
interface TodoHeaderProps { | ||
todos: Todo[]; | ||
} | ||
|
||
export const TodoHeader: FC<TodoHeaderProps> = ({ todos }) => { | ||
const { handleToogleAllTodoStatus } = useTodoFormManager(); | ||
|
||
return ( | ||
<header className="todoapp__header"> | ||
{!!todos.length && ( | ||
<button | ||
type="button" | ||
className={cn('todoapp__toggle-all', { | ||
active: isAllTodosCompleted(todos), | ||
})} | ||
data-cy="ToggleAllButton" | ||
onClick={handleToogleAllTodoStatus} | ||
/> | ||
)} | ||
|
||
<TodoForm /> | ||
</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,100 @@ | ||
import { FormEvent, forwardRef } from 'react'; | ||
import { Todo } from '../../../types/Todo'; | ||
import cn from 'classnames'; | ||
import { useDeleteTodo } from '../../../hooks/useDeleteTodo'; | ||
import { useTodoFormManager } from '../../../hooks/useTodoFormManager'; | ||
import { useSelectedTodo } from '../../../hooks/useSelectedTodo'; | ||
|
||
interface TodoItemProps { | ||
todo: Todo; | ||
isLoading?: boolean; | ||
} | ||
|
||
export const TodoItem = forwardRef<HTMLDivElement, TodoItemProps>( | ||
({ todo, isLoading = false }, ref) => { | ||
const { completed, id, title } = todo; | ||
const { isDeleting, handleDeleteTodo } = useDeleteTodo(); | ||
const { selectedTodo, setSelectedTodo } = useSelectedTodo(); | ||
const { | ||
title: updatingTitle, | ||
isUpdating, | ||
setTitle, | ||
handleUpdateTodo, | ||
} = useTodoFormManager(todo.title); | ||
|
||
const handleSubmit = async (event: FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
|
||
if (title == updatingTitle) { | ||
return setSelectedTodo(null); | ||
} | ||
|
||
const res = await handleUpdateTodo({ ...todo, title: updatingTitle }); | ||
|
||
if (res) { | ||
setSelectedTodo(null); | ||
} | ||
}; | ||
|
||
return ( | ||
<div ref={ref} data-cy="Todo" className={cn('todo', { completed })}> | ||
{/* eslint-disable jsx-a11y/label-has-associated-control */} | ||
<label className="todo__status-label"> | ||
<input | ||
data-cy="TodoStatus" | ||
type="checkbox" | ||
className="todo__status" | ||
checked={completed} | ||
onChange={() => { | ||
handleUpdateTodo({ ...todo, completed: !todo.completed }); | ||
}} | ||
/> | ||
</label> | ||
|
||
{selectedTodo ? ( | ||
<form onSubmit={handleSubmit} onBlur={handleSubmit}> | ||
<input | ||
data-cy="TodoTitleField" | ||
type="text" | ||
className="todo__title-field" | ||
placeholder="Empty todo will be deleted" | ||
value={updatingTitle} | ||
onChange={e => setTitle(e.target.value)} | ||
autoFocus | ||
/> | ||
</form> | ||
) : ( | ||
<span | ||
data-cy="TodoTitle" | ||
className="todo__title" | ||
onDoubleClick={() => { | ||
setSelectedTodo(todo); | ||
}} | ||
> | ||
{title.trim()} | ||
</span> | ||
)} | ||
{!selectedTodo && ( | ||
<button | ||
type="button" | ||
className="todo__remove" | ||
data-cy="TodoDelete" | ||
onClick={() => handleDeleteTodo(id)} | ||
> | ||
× | ||
</button> | ||
)} | ||
|
||
<div | ||
data-cy="TodoLoader" | ||
className={cn('modal overlay', { | ||
'is-active': isLoading || isDeleting || isUpdating, | ||
})} | ||
> | ||
<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,56 @@ | ||
import React, { FC, useContext, useRef } from 'react'; | ||
import { Todo } from '../../../types/Todo'; | ||
import { TodoItem } from '../TodoItem/TodoItem'; | ||
import { TodosContext } from '../../../context/TodoContext'; | ||
import { CSSTransition, TransitionGroup } from 'react-transition-group'; | ||
|
||
interface TodoListProps { | ||
todos: Todo[]; | ||
} | ||
|
||
export const TodoList: FC<TodoListProps> = ({ todos }) => { | ||
const { tempTodo } = useContext(TodosContext); | ||
|
||
const refs = useRef<Record<number, React.RefObject<HTMLDivElement>>>({}); | ||
|
||
todos.forEach(todo => { | ||
if (!refs.current[todo.id]) { | ||
refs.current[todo.id] = React.createRef(); | ||
} | ||
}); | ||
|
||
if (tempTodo && !refs.current[tempTodo.id]) { | ||
refs.current[tempTodo.id] = React.createRef(); | ||
} | ||
|
||
return ( | ||
<section className="todoapp__main" data-cy="TodoList"> | ||
<TransitionGroup> | ||
{todos.map(todo => ( | ||
<CSSTransition | ||
key={todo.id} | ||
timeout={300} | ||
classNames="item" | ||
nodeRef={refs.current[todo.id]} | ||
> | ||
<TodoItem ref={refs.current[todo.id]} key={todo.id} todo={todo} /> | ||
</CSSTransition> | ||
))} | ||
{tempTodo && ( | ||
<CSSTransition | ||
key={tempTodo.id} | ||
timeout={300} | ||
classNames="temp-item" | ||
nodeRef={refs.current[tempTodo.id]} | ||
> | ||
<TodoItem | ||
ref={refs.current[tempTodo.id]} | ||
todo={tempTodo} | ||
isLoading={true} | ||
/> | ||
</CSSTransition> | ||
)} | ||
</TransitionGroup> | ||
</section> | ||
); | ||
}; |
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.
here we can also use
TODOS_API_PATH