-
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
solution #1540
base: master
Are you sure you want to change the base?
solution #1540
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,26 +1,175 @@ | ||||||
/* eslint-disable max-len */ | ||||||
/* eslint-disable jsx-a11y/label-has-associated-control */ | ||||||
/* eslint-disable jsx-a11y/control-has-associated-label */ | ||||||
import React from 'react'; | ||||||
import React, { useCallback, useEffect, useState } from 'react'; | ||||||
import { UserWarning } from './UserWarning'; | ||||||
|
||||||
const USER_ID = 0; | ||||||
import { | ||||||
createTodo, | ||||||
deleteTodo, | ||||||
getTodos, | ||||||
updateTodo, | ||||||
USER_ID, | ||||||
} from './api/todos'; | ||||||
import { Todo } from './types/Todo'; | ||||||
import { ErrorNotification } from './components/ErrorNotification'; | ||||||
import { FilterType } from './types/FilterType'; | ||||||
import { TodoList } from './components/TodoList'; | ||||||
import { Footer } from './components/Footer'; | ||||||
import { Header } from './components/Header'; | ||||||
import { ErrorType } from './types/ErrorType'; | ||||||
|
||||||
export const App: React.FC = () => { | ||||||
const [todos, setTodos] = useState<Todo[]>([]); | ||||||
const [errorTodos, setErrorTodos] = useState(ErrorType.NoErrors); | ||||||
const [status, setStatus] = useState(FilterType.All); | ||||||
const [isLoading, setIsLoading] = useState(true); | ||||||
const [loadingTodosIds, setLoadingTodosIds] = useState<number[]>([]); | ||||||
|
||||||
const [tempTodo, setTempTodo] = useState<Todo | null>(null); | ||||||
|
||||||
const handleErrorClose = useCallback(() => { | ||||||
setErrorTodos(ErrorType.NoErrors); | ||||||
}, []); | ||||||
|
||||||
const visibleTodos = todos.filter(todo => { | ||||||
switch (status) { | ||||||
case FilterType.Completed: | ||||||
return todo.completed; | ||||||
case FilterType.Active: | ||||||
return !todo.completed; | ||||||
default: | ||||||
return true; | ||||||
} | ||||||
}); | ||||||
|
||||||
const activeTodosCount = todos.filter(todo => !todo.completed).length; | ||||||
|
||||||
const completedTodos = todos.filter(todo => todo.completed); | ||||||
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. the same, useMemo? |
||||||
const completedTodosCount = completedTodos.length; | ||||||
|
||||||
useEffect(() => { | ||||||
setIsLoading(true); | ||||||
getTodos() | ||||||
.then(setTodos) | ||||||
.catch(() => { | ||||||
setErrorTodos(ErrorType.Loading); | ||||||
}) | ||||||
.finally(() => { | ||||||
setIsLoading(false); | ||||||
}); | ||||||
}, []); | ||||||
|
||||||
const onAddTodo = async (newTodo: Omit<Todo, 'id'>): Promise<void> => { | ||||||
try { | ||||||
const createdTodo = await createTodo(newTodo); | ||||||
|
||||||
setTodos(currentTodos => [...currentTodos, createdTodo]); | ||||||
} catch (error) { | ||||||
setErrorTodos(ErrorType.Add); | ||||||
throw error; | ||||||
} | ||||||
}; | ||||||
|
||||||
const onDeleteTodo = async (todoId: number) => { | ||||||
setLoadingTodosIds(currentIds => [...currentIds, todoId]); | ||||||
try { | ||||||
await deleteTodo(todoId); | ||||||
setTodos(currentTodos => currentTodos.filter(todo => todo.id !== todoId)); | ||||||
} catch (error) { | ||||||
setErrorTodos(ErrorType.Delete); | ||||||
throw error; | ||||||
} finally { | ||||||
setLoadingTodosIds(currentIds => currentIds.filter(id => id !== todoId)); | ||||||
} | ||||||
}; | ||||||
|
||||||
const onDeleteAllCompleted = () => { | ||||||
completedTodos.forEach(completedTodo => { | ||||||
onDeleteTodo(completedTodo.id); | ||||||
}); | ||||||
}; | ||||||
|
||||||
const onUpdateTodo = async (newTodo: Todo) => { | ||||||
setLoadingTodosIds(currentIds => [...currentIds, newTodo.id]); | ||||||
try { | ||||||
const updatedTodo = await updateTodo(newTodo); | ||||||
|
||||||
setTodos(currentTodos => | ||||||
currentTodos.map(todo => { | ||||||
return todo.id === updatedTodo.id ? updatedTodo : todo; | ||||||
}), | ||||||
); | ||||||
} catch (error) { | ||||||
setErrorTodos(ErrorType.Update); | ||||||
throw error; | ||||||
} finally { | ||||||
setLoadingTodosIds(currentIds => | ||||||
currentIds.filter(id => id !== newTodo.id), | ||||||
); | ||||||
} | ||||||
}; | ||||||
|
||||||
const onUpdateToggleAll = () => { | ||||||
todos.forEach(todo => { | ||||||
if (todos.length === completedTodos.length) { | ||||||
onUpdateTodo({ ...todo, completed: false }); | ||||||
|
||||||
return; | ||||||
} | ||||||
|
||||||
if (todo.completed === false) { | ||||||
onUpdateTodo({ ...todo, completed: true }); | ||||||
|
||||||
return; | ||||||
} | ||||||
}); | ||||||
}; | ||||||
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. useCallback for all the cases? |
||||||
|
||||||
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> | ||||||
|
||||||
<p className="subtitle">Styles are already copied</p> | ||||||
</section> | ||||||
<div className="todoapp"> | ||||||
<h1 className="todoapp__title">todos</h1> | ||||||
|
||||||
<div className="todoapp__content"> | ||||||
<Header | ||||||
todos={todos} | ||||||
completedTodosCount={completedTodosCount} | ||||||
onAddTodo={onAddTodo} | ||||||
setErrorTodos={setErrorTodos} | ||||||
tempTodo={tempTodo} | ||||||
setTempTodo={setTempTodo} | ||||||
onUpdateToggleAll={onUpdateToggleAll} | ||||||
/> | ||||||
|
||||||
{todos.length > 0 && ( | ||||||
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. better option(it will be converted to boolean value)
Suggested change
|
||||||
<> | ||||||
<TodoList | ||||||
todos={visibleTodos} | ||||||
onDeleteTodo={onDeleteTodo} | ||||||
onUpdateTodo={onUpdateTodo} | ||||||
isDataLoading={isLoading} | ||||||
loadingTodosIds={loadingTodosIds} | ||||||
tempTodo={tempTodo} | ||||||
/> | ||||||
|
||||||
<Footer | ||||||
activeCount={activeTodosCount} | ||||||
completedCount={completedTodosCount} | ||||||
status={status} | ||||||
setStatus={setStatus} | ||||||
onDeleteAllCompleted={onDeleteAllCompleted} | ||||||
/> | ||||||
</> | ||||||
)} | ||||||
</div> | ||||||
|
||||||
<ErrorNotification | ||||||
error={errorTodos} | ||||||
handleErrorClose={handleErrorClose} | ||||||
/> | ||||||
</div> | ||||||
); | ||||||
}; |
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 USER_ID = 2153; | ||
|
||
export const getTodos = () => { | ||
return client.get<Todo[]>(`/todos?userId=${USER_ID}`); | ||
}; | ||
|
||
export const deleteTodo = (todoId: number) => { | ||
return client.delete(`/todos/${todoId}`); | ||
}; | ||
|
||
export const createTodo = (newTodo: Omit<Todo, 'id'>) => { | ||
return client.post<Todo>(`/todos`, newTodo); | ||
}; | ||
|
||
export const updateTodo = ({ id, title, userId, completed }: Todo) => { | ||
return client.patch<Todo>(`/todos/${id}`, { id, title, userId, completed }); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React, { useEffect } from 'react'; | ||
import cn from 'classnames'; | ||
import { ErrorType } from '../../types/ErrorType'; | ||
|
||
type Props = { | ||
error: string; | ||
handleErrorClose: () => void; | ||
}; | ||
|
||
export const ErrorNotification: React.FC<Props> = props => { | ||
const { error, handleErrorClose } = props; | ||
|
||
useEffect(() => { | ||
if (error === ErrorType.NoErrors) { | ||
return; | ||
} | ||
|
||
const timeoutId = setTimeout(() => { | ||
handleErrorClose(); | ||
}, 3000); | ||
|
||
return () => { | ||
clearTimeout(timeoutId); | ||
}; | ||
}, [error, handleErrorClose]); | ||
|
||
return ( | ||
<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" | ||
onClick={handleErrorClose} | ||
/> | ||
{error} | ||
</div> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './ErrorNotification'; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,58 @@ | ||||||
import React from 'react'; | ||||||
import cn from 'classnames'; | ||||||
import { FilterType } from '../../types/FilterType'; | ||||||
import { Dispatch, SetStateAction } from 'react'; | ||||||
|
||||||
type Props = { | ||||||
activeCount: number | undefined; | ||||||
completedCount: number | undefined; | ||||||
status: FilterType; | ||||||
setStatus: Dispatch<SetStateAction<FilterType>>; | ||||||
onDeleteAllCompleted: () => void; | ||||||
}; | ||||||
|
||||||
export const Footer: React.FC<Props> = props => { | ||||||
const { | ||||||
activeCount, | ||||||
completedCount, | ||||||
status, | ||||||
setStatus, | ||||||
onDeleteAllCompleted, | ||||||
} = props; | ||||||
|
||||||
return ( | ||||||
<footer className="todoapp__footer" data-cy="Footer"> | ||||||
<span className="todo-count" data-cy="TodosCounter"> | ||||||
{activeCount} items left | ||||||
</span> | ||||||
|
||||||
<nav className="filter" data-cy="Filter"> | ||||||
{Object.values(FilterType).map(filter => ( | ||||||
<a | ||||||
key={filter} | ||||||
href={`#/${filter === FilterType.All ? '' : filter.toLowerCase()}`} | ||||||
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. better or no?what do u think? 🤔
Suggested change
|
||||||
className={cn('filter__link', { | ||||||
selected: status === filter, | ||||||
})} | ||||||
data-cy={`FilterLink${filter}`} | ||||||
onClick={() => { | ||||||
setStatus(filter); | ||||||
}} | ||||||
> | ||||||
{filter} | ||||||
</a> | ||||||
))} | ||||||
</nav> | ||||||
|
||||||
<button | ||||||
type="button" | ||||||
className="todoapp__clear-completed" | ||||||
data-cy="ClearCompletedButton" | ||||||
onClick={onDeleteAllCompleted} | ||||||
disabled={!completedCount} | ||||||
> | ||||||
Clear completed | ||||||
</button> | ||||||
</footer> | ||||||
); | ||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Footer'; |
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.
useMemo?