-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Develop #2134
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
Open
rekverr
wants to merge
4
commits into
mate-academy:master
Choose a base branch
from
rekverr: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
Develop #2134
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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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,190 @@ | ||
| /* eslint-disable max-len */ | ||
| /* eslint-disable jsx-a11y/control-has-associated-label */ | ||
| import React from 'react'; | ||
| import { useState, useEffect, useRef } from 'react'; | ||
| import classNames from 'classnames'; | ||
| import { TodoList } from './components/TodoList'; | ||
| import { UserWarning } from './UserWarning'; | ||
|
|
||
| const USER_ID = 0; | ||
| import { USER_ID } from './api/todos'; | ||
| import { HeaderTodo } from './components/HeaderTodo'; | ||
| import { FilterTodo } from './components/FilterTodo'; | ||
| import { useTodos } from './hooks/useTodos'; | ||
| import { useErrorMessage } from './hooks/useErrorMessage'; | ||
| import { filterTodos, FilterType } from './utils/todoFilters'; | ||
| import { ErrorMessage } from './types/ErrorMessage'; | ||
|
|
||
| export const App: React.FC = () => { | ||
| const [filter, setFilter] = useState<FilterType>(FilterType.ALL); | ||
| const [newTodoTitle, setNewTodoTitle] = useState(''); | ||
| const { errorMessage, setErrorMessage } = useErrorMessage(); | ||
| const { | ||
| todos, | ||
| isAdding, | ||
| deletingIds, | ||
| tempTodo, | ||
| isClearing, | ||
| handleAddTodo, | ||
| handleDeleteTodo, | ||
| handleClearCompleted, | ||
| handleCompletedTodo, | ||
| handleToggleAllTodos, | ||
| completingIds, | ||
| editingId, | ||
| editingIds, | ||
| editingTitle, | ||
| setEditingTitle, | ||
| handleStartEdit, | ||
| handleCommitEdit, | ||
| handleCancelEdit, | ||
| } = useTodos(setErrorMessage); | ||
|
|
||
| const inputRef = useRef<HTMLInputElement>(null); | ||
|
|
||
| useEffect(() => { | ||
| inputRef.current?.focus(); | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| if (!isAdding && deletingIds.length === 0 && !isClearing) { | ||
| inputRef.current?.focus(); | ||
| } | ||
| }, [isAdding, deletingIds.length, isClearing]); | ||
|
|
||
| const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => { | ||
| e.preventDefault(); | ||
|
|
||
| const trimmedTitle = newTodoTitle.trim(); | ||
|
|
||
| if (!trimmedTitle) { | ||
| setErrorMessage(ErrorMessage.EMPTY_TITLE); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| try { | ||
| await handleAddTodo(trimmedTitle, USER_ID); | ||
| setNewTodoTitle(''); | ||
| } catch { | ||
| setErrorMessage(ErrorMessage.ADD_TODO); | ||
| } | ||
| }; | ||
|
|
||
| const handleComplete = async (id: number, completed: boolean) => { | ||
| try { | ||
| await handleCompletedTodo(id, completed); | ||
| } catch { | ||
| setErrorMessage(ErrorMessage.UPDATE_TODO); | ||
| } | ||
| }; | ||
|
|
||
| const handleToggleAll = async () => { | ||
| const hasActiveTodos = todos.some(todo => !todo.completed); | ||
|
|
||
| try { | ||
| await handleToggleAllTodos(hasActiveTodos); | ||
| } catch { | ||
| setErrorMessage(ErrorMessage.UPDATE_TODO); | ||
| } | ||
| }; | ||
|
|
||
| const handleEditTodo = async (id: number, title: string) => { | ||
| let shouldDelete = false; | ||
|
|
||
| try { | ||
| shouldDelete = await handleCommitEdit(id, title); | ||
| } catch { | ||
| setErrorMessage(ErrorMessage.UPDATE_TODO); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (shouldDelete) { | ||
| try { | ||
| await handleDeleteTodo(id); | ||
| handleCancelEdit(); | ||
| } catch { | ||
| setErrorMessage(ErrorMessage.DELETE_TODO); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const handleDelete = async (id: number) => { | ||
| try { | ||
| await handleDeleteTodo(id); | ||
| } catch { | ||
| setErrorMessage(ErrorMessage.DELETE_TODO); | ||
| } | ||
| }; | ||
|
|
||
| const handleClear = async () => { | ||
| try { | ||
| await handleClearCompleted(); | ||
| } catch (error) { | ||
| if (error instanceof Error) { | ||
| setErrorMessage(error.message); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| if (!USER_ID) { | ||
| return <UserWarning />; | ||
| } | ||
|
|
||
| const filteredTodos = filterTodos(todos, filter, tempTodo); | ||
|
|
||
| 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"> | ||
| <HeaderTodo | ||
| handlerSubmit={handleSubmit} | ||
| handleToggleAll={handleToggleAll} | ||
| inputRef={inputRef} | ||
| newTodoTitle={newTodoTitle} | ||
| setNewTodoTitle={setNewTodoTitle} | ||
| isAdding={isAdding} | ||
| todos={todos} | ||
| /> | ||
|
|
||
| <TodoList | ||
| todos={filteredTodos} | ||
| isAdding={isAdding} | ||
| deletingIds={deletingIds} | ||
| completingIds={completingIds} | ||
| editingId={editingId} | ||
| editingIds={editingIds} | ||
| editingTitle={editingTitle} | ||
| setEditingTitle={setEditingTitle} | ||
| handleDelete={handleDelete} | ||
| handleComplete={handleComplete} | ||
| handleStartEdit={handleStartEdit} | ||
| handleEditTodo={handleEditTodo} | ||
| handleCancelEdit={handleCancelEdit} | ||
| /> | ||
|
|
||
| <FilterTodo | ||
| todos={todos} | ||
| filter={filter} | ||
| setFilter={setFilter} | ||
| handleClearCompleted={handleClear} | ||
| /> | ||
| </div> | ||
|
|
||
| <div | ||
| data-cy="ErrorNotification" | ||
| className={classNames( | ||
| 'notification is-danger is-light has-text-weight-normal', | ||
| { | ||
| hidden: !errorMessage, | ||
| }, | ||
| )} | ||
| > | ||
| <button | ||
| data-cy="HideErrorButton" | ||
| type="button" | ||
| className="delete" | ||
| onClick={() => setErrorMessage('')} | ||
| /> | ||
| {errorMessage} | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
This file contains hidden or 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,28 @@ | ||
| import { Todo } from '../types/Todo'; | ||
| import { client } from '../utils/fetchClient'; | ||
|
|
||
| export const USER_ID = 4019; | ||
|
|
||
| export const getTodos = () => { | ||
| return client.get<Todo[]>(`/todos?userId=${USER_ID}`); | ||
| }; | ||
|
|
||
| export const addTodo = (title: string) => { | ||
| return client.post<Todo>(`/todos`, { | ||
| title, | ||
| userId: USER_ID, | ||
| completed: false, | ||
| }); | ||
| }; | ||
|
|
||
| export const deleteTodo = (id: number) => { | ||
| return client.delete(`/todos/${id}`); | ||
| }; | ||
|
|
||
| export const completeTodo = (id: number, completed: boolean) => { | ||
| return client.patch<Todo>(`/todos/${id}`, { completed }); | ||
| }; | ||
|
|
||
| export const updateTodoTitle = (id: number, title: string) => { | ||
| return client.patch<Todo>(`/todos/${id}`, { title }); | ||
| }; |
This file contains hidden or 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,74 @@ | ||
| import classNames from 'classnames'; | ||
| import { Todo } from '../types/Todo'; | ||
| import { FilterType } from '../utils/todoFilters'; | ||
|
|
||
| type Props = { | ||
| todos: Todo[]; | ||
| filter: FilterType; | ||
| setFilter: (filter: FilterType) => void; | ||
| handleClearCompleted: () => void; | ||
| }; | ||
|
|
||
| export const FilterTodo = ({ | ||
| todos, | ||
| filter, | ||
| setFilter, | ||
| handleClearCompleted, | ||
| }: Props) => { | ||
| return ( | ||
| <> | ||
| {todos.length > 0 && ( | ||
| <footer className="todoapp__footer" data-cy="Footer"> | ||
| <span className="todo-count" data-cy="TodosCounter"> | ||
| {todos.filter(todo => !todo.completed).length} items left | ||
| </span> | ||
|
|
||
| <nav className="filter" data-cy="Filter"> | ||
| <a | ||
| href="#/" | ||
| className={classNames('filter__link', { | ||
| selected: filter === FilterType.ALL, | ||
| })} | ||
| data-cy="FilterLinkAll" | ||
| onClick={() => setFilter(FilterType.ALL)} | ||
| > | ||
| All | ||
| </a> | ||
|
|
||
| <a | ||
| href="#/active" | ||
| className={classNames('filter__link', { | ||
| selected: filter === FilterType.ACTIVE, | ||
| })} | ||
| data-cy="FilterLinkActive" | ||
| onClick={() => setFilter(FilterType.ACTIVE)} | ||
| > | ||
| Active | ||
| </a> | ||
|
|
||
| <a | ||
| href="#/completed" | ||
| className={classNames('filter__link', { | ||
| selected: filter === FilterType.COMPLETED, | ||
| })} | ||
| data-cy="FilterLinkCompleted" | ||
| onClick={() => setFilter(FilterType.COMPLETED)} | ||
| > | ||
| Completed | ||
| </a> | ||
| </nav> | ||
|
|
||
| <button | ||
| type="button" | ||
| className="todoapp__clear-completed" | ||
| data-cy="ClearCompletedButton" | ||
| disabled={!todos.some(todo => todo.completed)} | ||
| onClick={handleClearCompleted} | ||
| > | ||
| Clear completed | ||
| </button> | ||
| </footer> | ||
| )} | ||
| </> | ||
| ); | ||
| }; |
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.
Consider making a custom type / enum for error states (e.g.
ErrorMessage) and re-use it to avoid hard-coded typos