-
Notifications
You must be signed in to change notification settings - Fork 2.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 #2700
base: master
Are you sure you want to change the base?
add task solution #2700
Changes from all 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,14 +1,80 @@ | ||
/* eslint-disable max-len */ | ||
import React from 'react'; | ||
import React, { useEffect, useState } from 'react'; | ||
import 'bulma/css/bulma.css'; | ||
import '@fortawesome/fontawesome-free/css/all.css'; | ||
|
||
import { TodoList } from './components/TodoList'; | ||
import { TodoFilter } from './components/TodoFilter'; | ||
import { TodoModal } from './components/TodoModal'; | ||
import { Loader } from './components/Loader'; | ||
import { getTodos, getUser } from './api'; | ||
import { Todo } from './types/Todo'; | ||
import { User } from './types/User'; | ||
|
||
export enum FilterType { | ||
All = 'all', | ||
Active = 'active', | ||
Completed = 'completed', | ||
} | ||
export const App: React.FC = () => { | ||
|
||
const [todos, setTodo] = useState<Todo[]>([]); | ||
const [filterTodos, setFilterTodos] = useState<Todo[]>([]); | ||
const [filter, setFilter] = useState<string>(FilterType.All); | ||
const [searchFilter, setSearchFilter] = useState<string>(''); | ||
|
||
const [selectedTodo, setSelectedTodo] = useState<Todo | null>(null); | ||
const [loading, setLoading] = useState(false); | ||
const [user, setUser] = useState<User | null>(null); | ||
const [loadingModal, setLoadingModal] = useState(true); | ||
|
||
const [modal, setModal] = useState(false); | ||
|
||
|
||
useEffect(() => { | ||
if (selectedTodo?.userId) { | ||
setLoadingModal(true); | ||
getUser(selectedTodo?.userId).then(fetchedUser => setUser(fetchedUser)); | ||
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 email address in the anchor tag should be dynamically set based on the |
||
setTimeout(() => { | ||
setLoadingModal(false); | ||
}, 500); | ||
Comment on lines
+38
to
+40
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 logic for setting the loading state is inefficient. It sets the loading state to true and then immediately to false after a timeout. This should be revised to better reflect the actual loading state of the application. |
||
} | ||
}, [selectedTodo]); | ||
|
||
useEffect(() => { | ||
getTodos().then(todoFromServer => { | ||
setTodo(todoFromServer); | ||
setFilterTodos(todoFromServer); | ||
}); | ||
}, []); | ||
|
||
useEffect(() => { | ||
setLoading(true); | ||
let updatedTodos = [...todos]; | ||
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. There's a typo in the variable name |
||
|
||
switch (filter) { | ||
case FilterType.Active: | ||
updatedTodos = updatedTodos.filter(todo => !todo.completed); | ||
break; | ||
case FilterType.Completed: | ||
updatedTodos = updatedTodos.filter(todo => todo.completed); | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
if (searchFilter) { | ||
updatedTodos = updatedTodos.filter(todo => | ||
todo.title.toLowerCase().includes(searchFilter.toLowerCase()), | ||
); | ||
} | ||
|
||
setTimeout(() => { | ||
setFilterTodos(updatedTodos); | ||
setLoading(false); | ||
}, 500); | ||
}, [filter, searchFilter, todos]); | ||
|
||
return ( | ||
<> | ||
<div className="section"> | ||
|
@@ -17,18 +83,35 @@ export const App: React.FC = () => { | |
<h1 className="title">Todos:</h1> | ||
|
||
<div className="block"> | ||
<TodoFilter /> | ||
<TodoFilter | ||
setFilter={setFilter} | ||
searchFilter={searchFilter} | ||
setSearchFilter={setSearchFilter} | ||
/> | ||
</div> | ||
|
||
<div className="block"> | ||
<Loader /> | ||
<TodoList /> | ||
{loading && <Loader />} | ||
<TodoList | ||
filterTodos={filterTodos} | ||
setModal={setModal} | ||
selectedTodo={selectedTodo} | ||
setSelectedTodo={setSelectedTodo} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<TodoModal /> | ||
{modal && ( | ||
<TodoModal | ||
setModal={setModal} | ||
selectedTodo={selectedTodo} | ||
user={user} | ||
loadingModal={loadingModal} | ||
setSelectedTodo={setSelectedTodo} | ||
/> | ||
)} | ||
</> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,64 @@ | ||
export const TodoFilter = () => ( | ||
<form className="field has-addons"> | ||
<p className="control"> | ||
<span className="select"> | ||
<select data-cy="statusSelect"> | ||
<option value="all">All</option> | ||
<option value="active">Active</option> | ||
<option value="completed">Completed</option> | ||
</select> | ||
</span> | ||
</p> | ||
import React from 'react'; | ||
import { FilterType } from '../../App'; | ||
|
||
<p className="control is-expanded has-icons-left has-icons-right"> | ||
<input | ||
data-cy="searchInput" | ||
type="text" | ||
className="input" | ||
placeholder="Search..." | ||
/> | ||
<span className="icon is-left"> | ||
<i className="fas fa-magnifying-glass" /> | ||
</span> | ||
type Props = { | ||
setFilter: (value: string) => void; | ||
searchFilter: string; | ||
setSearchFilter: (value: string) => void; | ||
}; | ||
|
||
<span className="icon is-right" style={{ pointerEvents: 'all' }}> | ||
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */} | ||
<button data-cy="clearSearchButton" type="button" className="delete" /> | ||
</span> | ||
</p> | ||
</form> | ||
); | ||
export const TodoFilter: React.FC<Props> = ({ | ||
setFilter, | ||
searchFilter, | ||
setSearchFilter, | ||
}) => { | ||
const deleteSearch = () => { | ||
setSearchFilter(''); | ||
}; | ||
|
||
return ( | ||
<form className="field has-addons"> | ||
<p className="control"> | ||
<span className="select"> | ||
<select | ||
data-cy="statusSelect" | ||
onChange={e => setFilter(e.target.value)} | ||
> | ||
<option value={FilterType.All}>All</option> | ||
<option value={FilterType.Active}>Active</option> | ||
<option value={FilterType.Completed}>Completed</option> | ||
</select> | ||
</span> | ||
</p> | ||
|
||
<p className="control is-expanded has-icons-left has-icons-right"> | ||
<input | ||
data-cy="searchInput" | ||
type="text" | ||
className="input" | ||
placeholder="Search..." | ||
value={searchFilter} | ||
onChange={e => setSearchFilter(e.target.value)} | ||
/> | ||
<span className="icon is-left"> | ||
<i className="fas fa-magnifying-glass" /> | ||
</span> | ||
|
||
<span | ||
className="icon is-right" | ||
style={{ pointerEvents: FilterType.All }} | ||
> | ||
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */} | ||
{searchFilter && ( | ||
<button | ||
data-cy="clearSearchButton" | ||
type="button" | ||
className="delete" | ||
onClick={deleteSearch} | ||
/> | ||
)} | ||
</span> | ||
</p> | ||
</form> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React from 'react'; | ||
import { Todo } from '../../types/Todo'; | ||
import classNames from 'classnames'; | ||
|
||
type Props = { | ||
todo: Todo; | ||
setModal: (value: boolean) => void; | ||
selectedTodo: Todo | null; | ||
setSelectedTodo: (value: Todo) => void; | ||
}; | ||
|
||
export const TodoItem: React.FC<Props> = ({ | ||
todo, | ||
setModal, | ||
selectedTodo, | ||
setSelectedTodo, | ||
}) => { | ||
const modalClick = () => { | ||
setModal(true); | ||
setSelectedTodo(todo); | ||
}; | ||
|
||
const{id, title, completed} = todo; | ||
|
||
return ( | ||
<tr data-cy="todo" className=""> | ||
<td className="is-vcentered">{id}</td> | ||
<td className="is-vcentered"> | ||
{completed && ( | ||
<span className="icon" data-cy="iconCompleted"> | ||
<i className="fas fa-check" /> | ||
</span> | ||
)} | ||
</td> | ||
<td className="is-vcentered is-expanded"> | ||
<p | ||
className={classNames({ | ||
'has-text-success': completed, | ||
'has-text-danger': !completed, | ||
})} | ||
> | ||
{title} | ||
</p> | ||
</td> | ||
<td className="has-text-right is-vcentered"> | ||
<button | ||
data-cy="selectButton" | ||
className="button" | ||
type="button" | ||
onClick={modalClick} | ||
> | ||
<span className="icon"> | ||
<i | ||
className={classNames('far', { | ||
'fa-eye-slash': id === selectedTodo?.id, | ||
'fa-eye': id !== selectedTodo?.id, | ||
})} | ||
/> | ||
</span> | ||
</button> | ||
</td> | ||
</tr> | ||
); | ||
}; |
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.
Don't need to create additional state for filtered todos, use only one state