-
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
add task solution #760
base: master
Are you sure you want to change the base?
add task solution #760
Changes from 1 commit
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,24 +1,161 @@ | ||
/* eslint-disable max-len */ | ||
/* eslint-disable @typescript-eslint/no-unused-expressions */ | ||
/* eslint-disable jsx-a11y/control-has-associated-label */ | ||
import React from 'react'; | ||
import React, { useMemo, useState } from 'react'; | ||
import { UserWarning } from './UserWarning'; | ||
import { Todo } from './types/Todo'; | ||
import { SortBy } from './types/SortBy'; | ||
import { deleteTodo, updateTodo } from './api/todos'; | ||
import { Todos } from './components/Todos'; | ||
import { useGetTodos } from './hooks'; | ||
import { Errors } from './types'; | ||
import { Footer } from './components/Footer'; | ||
import { Header } from './components/Header'; | ||
|
||
const USER_ID = 0; | ||
const USER_ID = 11361; | ||
|
||
export const App: React.FC = () => { | ||
const [userId] = useState(USER_ID); | ||
const [sortBy, setSortBy] = useState<SortBy>(SortBy.all); | ||
const [selectedTodo, setSelectedTodo] = useState<number[]>([]); | ||
const [tempTodo, setTempTodo] = useState<Todo | null>(null); | ||
const [isDeleteUpdateTodo, setIsDeleteUpdateTodo] = useState<boolean>(false); | ||
const [makeAnyChange, setMakeAnyChange] = useState<boolean>(false); | ||
const { | ||
isLoading, | ||
todos, | ||
errorMessage, | ||
handleError, | ||
} = useGetTodos(USER_ID, makeAnyChange); | ||
|
||
const handleSelectedTodo = (value: number[]) => { | ||
setSelectedTodo(value); | ||
}; | ||
|
||
const handleSetTempTodo = (value:Todo | null) => { | ||
setTempTodo(value); | ||
}; | ||
|
||
const handleSetMakeAnyChange = (value:boolean) => { | ||
setMakeAnyChange(value); | ||
}; | ||
|
||
const handleDeleteUptadeTodo = (value: boolean) => { | ||
setIsDeleteUpdateTodo(value); | ||
}; | ||
|
||
const todosNotCompleted = useMemo(() => { | ||
return todos.filter(todo => todo.completed === false).length; | ||
}, [todos]); | ||
|
||
const deleteOneTodo = async (todoId: number) => { | ||
setSelectedTodo(prevSelectedTodo => [...prevSelectedTodo, todoId]); | ||
setIsDeleteUpdateTodo(true); | ||
try { | ||
await deleteTodo(USER_ID, todoId); | ||
} catch (error) { | ||
handleError(Errors.delete); | ||
} finally { | ||
setSelectedTodo([]); | ||
} | ||
|
||
setIsDeleteUpdateTodo(false); | ||
setMakeAnyChange(!makeAnyChange); | ||
}; | ||
|
||
const updateCheckTodo = async (todoId: number) => { | ||
setSelectedTodo(prevSelectedTodo => [...prevSelectedTodo, todoId]); | ||
setIsDeleteUpdateTodo(true); | ||
let updatedTodo: Todo | undefined = todos.find(todo => todo.id === todoId); | ||
|
||
updatedTodo | ||
? updatedTodo = { ...updatedTodo, completed: !updatedTodo.completed } | ||
: null; | ||
|
||
try { | ||
await updateTodo(USER_ID, updatedTodo, todoId); | ||
} catch (error) { | ||
handleError(Errors.update); | ||
} finally { | ||
setSelectedTodo([]); | ||
} | ||
|
||
setIsDeleteUpdateTodo(true); | ||
setMakeAnyChange(!makeAnyChange); | ||
}; | ||
|
||
const handleUpdateCheckTodo = (value: number) => updateCheckTodo(value); | ||
const handleDeleteTodo = (value: number) => deleteOneTodo(value); | ||
|
||
const handleSetSortBy = (value: SortBy) => { | ||
setSortBy(value); | ||
}; | ||
|
||
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 | ||
todosNotCompleted={todosNotCompleted} | ||
todos={todos} | ||
handleUpdateCheckTodo={handleUpdateCheckTodo} | ||
handleSelectedTodo={handleSelectedTodo} | ||
handleError={handleError} | ||
handleSetTempTodo={handleSetTempTodo} | ||
userId={userId} | ||
handleSetMakeAnyChange={handleSetMakeAnyChange} | ||
makeAnyChange={makeAnyChange} | ||
selectedTodo={selectedTodo} | ||
/> | ||
|
||
<Todos | ||
todos={todos} | ||
tempTodo={tempTodo} | ||
sortBy={sortBy} | ||
handleDeleteTodo={handleDeleteTodo} | ||
isLoading={isLoading} | ||
selectedTodo={selectedTodo} | ||
handleUpdateCheckTodo={handleUpdateCheckTodo} | ||
handleSelectedTodo={handleSelectedTodo} | ||
handleError={handleError} | ||
userId={userId} | ||
handleSetMakeAnyChange={handleSetMakeAnyChange} | ||
makeAnyChange={makeAnyChange} | ||
isDeleteUpdateTodo={isDeleteUpdateTodo} | ||
handleDeleteUptadeTodo={handleDeleteUptadeTodo} | ||
/> | ||
|
||
{todos.length > 0 && ( | ||
<Footer | ||
todosNotCompleted={todosNotCompleted} | ||
handleSetSortBy={handleSetSortBy} | ||
sortBy={sortBy} | ||
todos={todos} | ||
handleDeleteTodo={handleDeleteTodo} | ||
handleSelectedTodo={handleSelectedTodo} | ||
selectedTodo={selectedTodo} | ||
/> | ||
)} | ||
|
||
</div> | ||
|
||
<div | ||
className={errorMessage | ||
? 'notification is-danger is-light has-text-weight-normal' | ||
: 'notification is-danger is-light has-text-weight-normal hidden'} | ||
> | ||
<button | ||
type="button" | ||
className="delete" | ||
onClick={() => handleError(Errors.noEroor)} | ||
/> | ||
|
||
{errorMessage} | ||
</div> | ||
</div> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Todo } from '../types/Todo'; | ||
import { client } from '../utils/fetchClient'; | ||
|
||
export const getTodos = (userId: number) => client.get<Todo[]>(`/todos?userId=${userId}`); | ||
|
||
export const deleteTodo = (userId: number, todoId: number) => { | ||
return client.delete(`/todos/${todoId}/?userId=${userId}`); | ||
}; | ||
|
||
export const addTodo = (userId: number, data: Todo | null) => { | ||
return client.post<Todo[]>(`/todos/?userId=${userId}`, data); | ||
}; | ||
|
||
export const updateTodo = ( | ||
userId: number, data: Todo | undefined, todoId: number, | ||
) => { | ||
return client.patch<Todo[]>(`/todos/${todoId}/?userId=${userId}`, data); | ||
}; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,93 @@ | ||||||
// import { deleteTodo } from '../api/todos'; | ||||||
import { SortBy, Todo } from '../types'; | ||||||
|
||||||
type Props = { | ||||||
todosNotCompleted: number, | ||||||
handleSetSortBy: (value:SortBy) => void, | ||||||
sortBy: SortBy, | ||||||
todos: Todo[], | ||||||
handleDeleteTodo: (value: number) => void, | ||||||
handleSelectedTodo: (todoID: number[]) => void; | ||||||
selectedTodo: number[]; | ||||||
}; | ||||||
|
||||||
export const Footer: React.FC<Props> = ({ | ||||||
todosNotCompleted, | ||||||
handleSetSortBy, | ||||||
sortBy, | ||||||
todos, | ||||||
handleDeleteTodo, | ||||||
handleSelectedTodo, | ||||||
selectedTodo, | ||||||
}) => { | ||||||
const deleteCompletedtodo = () => { | ||||||
try { | ||||||
todos.forEach(async todo => { | ||||||
if (todo.completed === true) { | ||||||
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.
Suggested change
|
||||||
handleSelectedTodo([...selectedTodo, todo.id]); | ||||||
} | ||||||
}); | ||||||
} finally { | ||||||
handleSelectedTodo([]); | ||||||
} | ||||||
|
||||||
todos.forEach(todo => { | ||||||
if (todo.completed === true) { | ||||||
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.
Suggested change
|
||||||
handleDeleteTodo(todo.id); | ||||||
} | ||||||
}); | ||||||
}; | ||||||
|
||||||
return ( | ||||||
<footer className="todoapp__footer"> | ||||||
<span className="todo-count"> | ||||||
{`${todosNotCompleted} items left`} | ||||||
</span> | ||||||
|
||||||
<nav className="filter"> | ||||||
<a | ||||||
href="#/" | ||||||
className={sortBy === 'all' | ||||||
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.
Suggested change
|
||||||
? 'filter__link selected' | ||||||
: 'filter__link'} | ||||||
onClick={() => handleSetSortBy(SortBy.all)} | ||||||
> | ||||||
All | ||||||
</a> | ||||||
|
||||||
<a | ||||||
href="#/active" | ||||||
className={sortBy === 'active' | ||||||
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 should use SortBy.active instead of string 'active' to make the code more consistent and less prone to typos. |
||||||
? 'filter__link selected' | ||||||
: 'filter__link'} | ||||||
onClick={() => handleSetSortBy(SortBy.active)} | ||||||
> | ||||||
Active | ||||||
</a> | ||||||
|
||||||
<a | ||||||
href="#/completed" | ||||||
className={sortBy === '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. You should use SortBy.completed instead of string 'completed' to make the code more consistent and less prone to typos. |
||||||
? 'filter__link selected' | ||||||
: 'filter__link'} | ||||||
onClick={() => handleSetSortBy(SortBy.completed)} | ||||||
> | ||||||
Completed | ||||||
</a> | ||||||
</nav> | ||||||
|
||||||
<button | ||||||
type="button" | ||||||
data-cy="ClearCompletedButton" | ||||||
className="todoapp__clear-completed" | ||||||
disabled={todosNotCompleted === todos.length} | ||||||
onClick={() => { | ||||||
deleteCompletedtodo(); | ||||||
}} | ||||||
> | ||||||
Clear completed | ||||||
</button> | ||||||
|
||||||
</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.
Remove all comments from the code.