-
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
implement update logic, failed from 2 to 5 tests, every time different. Work in progress #806
base: master
Are you sure you want to change the base?
Changes from 2 commits
c7ec635
710ad87
30bc1d3
fd98591
5e1fe48
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,62 @@ | ||
/* eslint-disable max-len */ | ||
/* eslint-disable jsx-a11y/control-has-associated-label */ | ||
import React from 'react'; | ||
import React, { useContext, useEffect } from 'react'; | ||
|
||
import { UserWarning } from './UserWarning'; | ||
import { Footer } from './components/Footer'; | ||
import { Header } from './components/Header'; | ||
import { ErrorNotification } from './components/ErrorNotification'; | ||
|
||
const USER_ID = 0; | ||
import { getTodos } from './api/todos'; | ||
import { USER_ID } from './utils/variables'; | ||
import { TodoList } from './components/TodoList'; | ||
import { ErrorMessage, TodosContext } from './components/TodosContext'; | ||
|
||
export const App: React.FC = () => { | ||
const { | ||
todos, | ||
setTodos, | ||
setFilteredTodos, | ||
setAlarm, | ||
} = useContext(TodosContext); | ||
|
||
useEffect(() => { | ||
if (USER_ID) { | ||
getTodos(USER_ID) | ||
.then(todosFromServer => { | ||
setTodos(todosFromServer); | ||
setFilteredTodos(todosFromServer); | ||
setAlarm(ErrorMessage.Default); | ||
}) | ||
.catch(errorMessage => { | ||
// eslint-disable-next-line no-console | ||
console.log(errorMessage); | ||
setAlarm(ErrorMessage.isLoadTodoError); | ||
setTodos([]); | ||
}); | ||
} | ||
}, [USER_ID]); | ||
|
||
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.length && ( | ||
<> | ||
<TodoList /> | ||
|
||
<Footer /> | ||
</> | ||
)} | ||
</div> | ||
|
||
<ErrorNotification /> | ||
</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) => { | ||
return client.get<Todo[]>(`/todos?userId=${userId}`); | ||
}; | ||
|
||
export const createTodo = ({ title, userId, completed }: Omit<Todo, 'id'>) => { | ||
return client.post<Todo>('/todos', { title, userId, completed }); | ||
}; | ||
|
||
export const removeTodo = (postId: number) => { | ||
return client.delete(`/todos/${postId}`); | ||
}; | ||
|
||
export const updateTodo = (todo: Todo) => { | ||
return client.patch(`/todos/${todo.id}`, todo); | ||
}; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,46 @@ | ||||||
/* eslint-disable jsx-a11y/control-has-associated-label */ | ||||||
import React, { useContext, useEffect, useRef } from 'react'; | ||||||
import cn from 'classnames'; | ||||||
import { ErrorMessage, TodosContext } from '../TodosContext'; | ||||||
|
||||||
type Props = {}; | ||||||
|
||||||
export const ErrorNotification: React.FC<Props> = () => { | ||||||
const { | ||||||
alarm, | ||||||
setAlarm, | ||||||
} = useContext(TodosContext); | ||||||
|
||||||
const timerId = useRef<number>(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.
Suggested change
|
||||||
|
||||||
useEffect(() => { | ||||||
if (timerId.current) { | ||||||
window.clearTimeout(timerId.current); | ||||||
} | ||||||
|
||||||
timerId.current = window.setTimeout(() => { | ||||||
setAlarm(ErrorMessage.Default); | ||||||
}, 3000); | ||||||
}, [alarm]); | ||||||
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. |
||||||
|
||||||
return ( | ||||||
<div | ||||||
data-cy="ErrorNotification" | ||||||
className={cn( | ||||||
'notification', | ||||||
'is-light', | ||||||
'is-danger', | ||||||
'has-text-weight-normal', | ||||||
{ hidden: !alarm }, | ||||||
)} | ||||||
> | ||||||
<button | ||||||
data-cy="HideErrorButton" | ||||||
type="button" | ||||||
className="delete" | ||||||
onClick={() => setAlarm(ErrorMessage.Default)} | ||||||
/> | ||||||
{alarm} | ||||||
</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,110 @@ | ||||||
/* eslint-disable jsx-a11y/control-has-associated-label */ | ||||||
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. Try to rewrite the code so that it is not necessary to disable the linter |
||||||
import React, { useContext, useEffect, useState } from 'react'; | ||||||
import cn from 'classnames'; | ||||||
import { Todo } from '../../types/Todo'; | ||||||
import { TodosContext } from '../TodosContext'; | ||||||
|
||||||
enum FilterOption { | ||||||
Default = '', | ||||||
All = 'All', | ||||||
Active = 'Active', | ||||||
Completed = '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. Move types to the types folder |
||||||
|
||||||
type Props = {}; | ||||||
|
||||||
function filterTodos(option: FilterOption, todos: Todo[]) { | ||||||
const filteredTodos = todos.filter(todo => { | ||||||
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. Move to utils folder |
||||||
switch (option) { | ||||||
case FilterOption.Active: { | ||||||
return todo.completed === false; | ||||||
} | ||||||
|
||||||
case FilterOption.Completed: { | ||||||
return todo.completed === true; | ||||||
} | ||||||
|
||||||
default: { | ||||||
return todo; | ||||||
} | ||||||
} | ||||||
}); | ||||||
|
||||||
return filteredTodos; | ||||||
} | ||||||
|
||||||
const OPTIONS = [FilterOption.All, FilterOption.Completed, FilterOption.Active]; | ||||||
|
||||||
export const Footer: React.FC<Props> = () => { | ||||||
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
If you don't have any props the types for them are redundant |
||||||
const { | ||||||
todos, | ||||||
setFilteredTodos, | ||||||
handleRemoveTodo, | ||||||
isTodoChange, | ||||||
changingItems, | ||||||
} = useContext(TodosContext); | ||||||
const [selectedOption, setSelectedOption] = useState(FilterOption.Default); | ||||||
const [hasCompleted, setHasCompleted] = useState(false); | ||||||
|
||||||
useEffect(() => { | ||||||
setSelectedOption(FilterOption.All); | ||||||
}, []); | ||||||
|
||||||
useEffect(() => { | ||||||
setFilteredTodos(filterTodos(selectedOption, todos)); | ||||||
setHasCompleted(todos.some(todo => todo.completed)); | ||||||
}, [selectedOption, isTodoChange, changingItems]); | ||||||
|
||||||
const handleFilterTodos = (option: FilterOption) => { | ||||||
setFilteredTodos(filterTodos(option, todos)); | ||||||
setSelectedOption(option); | ||||||
}; | ||||||
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 do |
||||||
|
||||||
const removeAllCompletedTodos = () => { | ||||||
const completedTodos = filterTodos(FilterOption.Completed, todos); | ||||||
|
||||||
completedTodos.forEach(todo => handleRemoveTodo(todo)); | ||||||
setHasCompleted(false); | ||||||
}; | ||||||
|
||||||
const counterTodos = todos.filter(todo => !todo.completed).length; | ||||||
|
||||||
return ( | ||||||
<footer className="todoapp__footer" data-cy="Footer"> | ||||||
<span className="todo-count" data-cy="TodosCounter"> | ||||||
{`${counterTodos} items left`} | ||||||
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. When there is only one todo there should not be plural in the message. |
||||||
</span> | ||||||
|
||||||
<nav className="filter" data-cy="Filter"> | ||||||
{OPTIONS.map(option => { | ||||||
const isSelected = selectedOption === option; | ||||||
|
||||||
return ( | ||||||
<a | ||||||
key={option} | ||||||
href={`#/${option.toLowerCase()}`} | ||||||
data-cy={`FilterLink${option}`} | ||||||
className={cn( | ||||||
'filter__link', | ||||||
{ selected: isSelected }, | ||||||
)} | ||||||
onClick={() => handleFilterTodos(option)} | ||||||
> | ||||||
{option} | ||||||
</a> | ||||||
); | ||||||
})} | ||||||
</nav> | ||||||
|
||||||
<button | ||||||
data-cy="ClearCompletedButton" | ||||||
type="button" | ||||||
className="todoapp__clear-completed" | ||||||
disabled={!hasCompleted} | ||||||
onClick={removeAllCompletedTodos} | ||||||
> | ||||||
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.
Try to rewrite the code so that it is not necessary to disable the linter