-
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
finishing the chalenge #1517
Open
italomagno
wants to merge
6
commits into
mate-academy:master
Choose a base branch
from
italomagno: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
finishing the chalenge #1517
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b5bd2d3
finishing the chalenge
italomagno a5964a9
finishing again with all 179 passing
italomagno 8237f2f
changes on route to handle internal error 500
italomagno 1f50892
this will be the last time
italomagno fac32f0
finishing the challenge
italomagno c60f04b
finishing the changes proposal
italomagno 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
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
Large diffs are not rendered by default.
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
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,109 @@ | ||
/* eslint-disable max-len */ | ||
/* eslint-disable jsx-a11y/control-has-associated-label */ | ||
import React from 'react'; | ||
import React, { useEffect, useState } from 'react'; | ||
import { UserWarning } from './UserWarning'; | ||
|
||
const USER_ID = 0; | ||
import { getTodos, USER_ID } from './api/todos'; | ||
import { Todo } from './types/Todo'; | ||
import { TodoList } from './components/TodoList'; | ||
import { Header } from './components/Header'; | ||
import { Footer } from './components/Footer'; | ||
import { ErrorComponent } from './components/ErrorComponent'; | ||
|
||
export const App: React.FC = () => { | ||
const [todos, setTodos] = useState<Todo[]>([]); | ||
const [filteredTodos, setFilteredTodos] = useState<Todo[]>([]); | ||
const [error, setError] = useState<string | null>(null); | ||
const [editingTodosId, setEditingTodosId] = useState<number[]>([]); | ||
const [isFooterActive, setIsFooterActiveFilter] = useState(false); | ||
const [filter, setFilter] = useState('all'); | ||
|
||
async function loadAllTodos() { | ||
try { | ||
const loadedTodos = await getTodos(); | ||
|
||
if (loadedTodos.length > 0) { | ||
setIsFooterActiveFilter(true); | ||
} | ||
|
||
setTodos(loadedTodos); | ||
setFilteredTodos(loadedTodos); | ||
} catch (e) { | ||
setError('Unable to load todos'); | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
loadAllTodos(); | ||
}, []); | ||
|
||
useEffect(() => { | ||
setTimeout(() => { | ||
setError(null); | ||
}, 3000); | ||
}, [error]); | ||
|
||
useEffect(() => { | ||
if (todos.length === 0) { | ||
setIsFooterActiveFilter(false); | ||
|
||
return; | ||
} | ||
|
||
setIsFooterActiveFilter(true); | ||
}, [todos.length]); | ||
|
||
useEffect(() => { | ||
if (filter === 'completed') { | ||
setFilteredTodos(prev => prev.filter(t => t.completed)); | ||
} | ||
|
||
if (filter === 'active') { | ||
setFilteredTodos(prev => prev.filter(t => !t.completed)); | ||
} | ||
}, [filter, todos]); | ||
|
||
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} | ||
setEditingTodosId={setEditingTodosId} | ||
setError={setError} | ||
editingTodosId={editingTodosId} | ||
setTodos={setTodos} | ||
setFilteredTodos={setFilteredTodos} | ||
filteredTodos={filteredTodos} | ||
/> | ||
<section className="todoapp__main" data-cy="TodoList"> | ||
<TodoList | ||
filter={filter} | ||
filteredTodos={filteredTodos} | ||
editingTodosId={editingTodosId} | ||
setEditingTodosId={setEditingTodosId} | ||
setError={setError} | ||
todos={todos} | ||
setTodos={setTodos} | ||
setFilteredTodos={setFilteredTodos} | ||
/> | ||
</section> | ||
<Footer | ||
filter={filter} | ||
setFilter={setFilter} | ||
setFilteredTodos={setFilteredTodos} | ||
todos={todos} | ||
isFooterActive={isFooterActive} | ||
setEditingTodosId={setEditingTodosId} | ||
loadAllTodos={loadAllTodos} | ||
setTodos={setTodos} | ||
setError={setError} | ||
editingTodosId={editingTodosId} | ||
/> | ||
</div> | ||
|
||
<ErrorComponent error={error} setError={setError} /> | ||
</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,24 @@ | ||
import { Todo } from '../types/Todo'; | ||
import { client } from '../utils/fetchClient'; | ||
|
||
export const USER_ID = 1865; | ||
|
||
export const getTodos = () => { | ||
return client.get<Todo[]>(`/todos?userId=${USER_ID}`); | ||
}; | ||
|
||
export const addTodo = (title: string) => { | ||
return client.post<Todo>('/todos', { | ||
userId: USER_ID, | ||
title, | ||
completed: false, | ||
}); | ||
}; | ||
|
||
export const updateTodo = (todo: Todo) => { | ||
return client.patch<Todo>(`/todos/${todo.id}`, todo); | ||
}; | ||
|
||
export const deleteTodo = (id: number) => { | ||
return client.delete(`/todos/${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,21 @@ | ||
interface ErrorComponentProps { | ||
error: string | null; | ||
setError: (error: string | null) => void; | ||
} | ||
|
||
export function ErrorComponent({ error, setError }: ErrorComponentProps) { | ||
return ( | ||
<div | ||
data-cy={`ErrorNotification`} | ||
className={`notification is-danger is-light has-text-weight-normal ${error ? '' : 'hidden'}`} | ||
> | ||
<button | ||
data-cy="HideErrorButton" | ||
type="button" | ||
className="delete" | ||
onClick={() => setError(null)} | ||
/> | ||
{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,122 @@ | ||
import { deleteTodo } from '../api/todos'; | ||
import { Todo } from '../types/Todo'; | ||
|
||
export interface FooterProps { | ||
todos: Todo[]; | ||
isFooterActive: boolean; | ||
setEditingTodosId: (ids: number[]) => void; | ||
loadAllTodos: () => void; | ||
setError: (error: string) => void; | ||
editingTodosId: number[]; | ||
setFilteredTodos: (todos: Todo[]) => void; | ||
setTodos: (todos: Todo[]) => void; | ||
filter: string; | ||
setFilter: (filter: string) => void; | ||
} | ||
export function Footer({ | ||
todos, | ||
isFooterActive, | ||
setEditingTodosId, | ||
setError, | ||
editingTodosId, | ||
setTodos, | ||
setFilteredTodos, | ||
filter, | ||
setFilter, | ||
}: FooterProps) { | ||
function handleFilterTodos(filterString: string) { | ||
setFilter(filterString); | ||
switch (filterString) { | ||
case 'all': | ||
setFilteredTodos(todos); | ||
break; | ||
case 'active': | ||
setFilteredTodos(todos.filter(t => !t.completed)); | ||
break; | ||
case 'completed': | ||
setFilteredTodos(todos.filter(t => t.completed)); | ||
break; | ||
default: | ||
setFilteredTodos(todos); | ||
} | ||
} | ||
|
||
async function handleDeleteAllCompletedTodos() { | ||
const completedTodos = todos.filter(t => t.completed); | ||
const completedTodoIds = completedTodos.map(t => t.id); | ||
|
||
setEditingTodosId([...editingTodosId, ...completedTodoIds]); | ||
|
||
const successfulDeletes: number[] = []; | ||
|
||
for (const todo of completedTodos) { | ||
try { | ||
await deleteTodo(todo.id); | ||
successfulDeletes.push(todo.id); | ||
} catch (error) { | ||
setError('Unable to delete a todo'); | ||
} | ||
} | ||
|
||
const newTodos = todos.filter(t => !successfulDeletes.includes(t.id)); | ||
|
||
setEditingTodosId( | ||
editingTodosId.filter(id => !completedTodoIds.includes(id)), | ||
); | ||
setFilteredTodos(newTodos); | ||
setTodos(newTodos); | ||
} | ||
|
||
return ( | ||
<> | ||
{isFooterActive && ( | ||
<footer className="todoapp__footer" data-cy="Footer"> | ||
<span className="todo-count" data-cy="TodosCounter"> | ||
{`${todos.filter(t => !t.completed).length} items left`} | ||
</span> | ||
|
||
<nav className="filter" data-cy="Filter"> | ||
<a | ||
onClick={() => handleFilterTodos('all')} | ||
href="#/" | ||
className={`filter__link ${filter === 'all' ? 'selected' : ''}`} | ||
data-cy="FilterLinkAll" | ||
> | ||
All | ||
</a> | ||
|
||
<a | ||
onClick={() => handleFilterTodos('active')} | ||
href="#/active" | ||
className={`filter__link ${filter === 'active' ? 'selected' : ''}`} | ||
data-cy="FilterLinkActive" | ||
> | ||
Active | ||
</a> | ||
|
||
<a | ||
onClick={() => { | ||
handleFilterTodos('completed'); | ||
}} | ||
href="#/completed" | ||
className={`filter__link ${filter === 'completed' ? 'selected' : ''}`} | ||
data-cy="FilterLinkCompleted" | ||
> | ||
Completed | ||
</a> | ||
</nav> | ||
|
||
<button | ||
type="button" | ||
className={`todoapp__clear-completed`} | ||
data-cy="ClearCompletedButton" | ||
disabled={!todos.some(t => t.completed)} | ||
onClick={handleDeleteAllCompletedTodos} | ||
> | ||
Clear completed | ||
</button> | ||
</footer> | ||
)} | ||
</> | ||
); | ||
} |
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.
Try to keep the name of the variable and the setter with the same name.