-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Develop #2619
base: master
Are you sure you want to change the base?
Develop #2619
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,34 @@ | ||
/* eslint-disable max-len */ | ||
import React from 'react'; | ||
import React, { useEffect, useMemo, 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 } from './api'; | ||
import { TodoList } from './components/TodoList'; | ||
import { TodoModal } from './components/TodoModal'; | ||
import { Todo } from './types/Todo'; | ||
|
||
export const App: React.FC = () => { | ||
const [todosFromServer, setTodosFromServer] = useState<Todo[]>([]); | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [filteredTodos, setFilteredTodos] = useState<Todo[]>([]); | ||
const [selectedTodoId, setSelectedTodoId] = useState(0); | ||
|
||
useEffect(() => { | ||
setIsLoading(true); | ||
getTodos() | ||
.then(todos => { | ||
setTodosFromServer(todos); | ||
setFilteredTodos(todos); | ||
}) | ||
.finally(() => setIsLoading(false)); | ||
}, []); | ||
|
||
const selectedTodo = useMemo(() => { | ||
return filteredTodos.find(todo => todo.id === selectedTodoId); | ||
}, [filteredTodos, selectedTodoId]); | ||
|
||
return ( | ||
<> | ||
<div className="section"> | ||
|
@@ -17,18 +37,32 @@ export const App: React.FC = () => { | |
<h1 className="title">Todos:</h1> | ||
|
||
<div className="block"> | ||
<TodoFilter /> | ||
<TodoFilter | ||
todos={todosFromServer} | ||
handleFilterChange={setFilteredTodos} | ||
/> | ||
</div> | ||
|
||
<div className="block"> | ||
<Loader /> | ||
<TodoList /> | ||
{isLoading && <Loader />} | ||
{!isLoading && ( | ||
<TodoList | ||
todos={filteredTodos} | ||
selectedTodoId={selectedTodoId} | ||
handleSelectTodoId={setSelectedTodoId} | ||
/> | ||
)} | ||
Comment on lines
+49
to
+53
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. Check that the |
||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<TodoModal /> | ||
{selectedTodo && ( | ||
<TodoModal | ||
todo={selectedTodo} | ||
onSetSelectedTodoId={setSelectedTodoId} | ||
/> | ||
Comment on lines
+61
to
+63
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. Ensure that the |
||
)} | ||
</> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,105 @@ | ||
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> | ||
|
||
<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> | ||
|
||
<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> | ||
); | ||
import { useState, useEffect, useCallback } from 'react'; | ||
import { Todo } from '../../types/Todo'; | ||
|
||
interface TodoFilterProps { | ||
todos: Todo[]; | ||
handleFilterChange: (filteredTodos: Todo[]) => void; | ||
} | ||
|
||
export enum FilterOptions { | ||
ALL = 'all', | ||
COMPLETED = 'completed', | ||
ACTIVE = 'active', | ||
} | ||
|
||
export const TodoFilter: React.FC<TodoFilterProps> = ({ | ||
todos, | ||
handleFilterChange, | ||
}) => { | ||
const [filter, setFilter] = useState<FilterOptions>(FilterOptions.ALL); | ||
const [query, setQuery] = useState(''); | ||
|
||
const handleFilterTodos = () => { | ||
let filteredTodos = todos.filter(todo => { | ||
switch (filter) { | ||
case FilterOptions.ACTIVE: | ||
return !todo.completed; | ||
case FilterOptions.COMPLETED: | ||
return todo.completed; | ||
default: | ||
return true; | ||
} | ||
}); | ||
|
||
if (query) { | ||
filteredTodos = filteredTodos.filter(todo => | ||
todo.title.toLowerCase().includes(query.toLowerCase()), | ||
); | ||
} | ||
|
||
handleFilterChange(filteredTodos); | ||
}; | ||
|
||
useEffect(() => { | ||
handleFilterTodos(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [filter, query, 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. The comment to disable the eslint rule for exhaustive dependencies is present. Ensure that the dependencies for |
||
const handleChangeFilter = useCallback( | ||
(event: React.ChangeEvent<HTMLSelectElement>) => { | ||
setFilter(event.target.value as FilterOptions); | ||
}, | ||
[], | ||
); | ||
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 |
||
const handleChangeQuery = useCallback( | ||
(event: React.ChangeEvent<HTMLInputElement>) => { | ||
setQuery(event.target.value); | ||
}, | ||
[], | ||
); | ||
const handleClearQuery = useCallback(() => { | ||
setQuery(''); | ||
}, []); | ||
|
||
return ( | ||
<form className="field has-addons"> | ||
{' '} | ||
<p className="control"> | ||
<span className="select"> | ||
<select | ||
data-cy="statusSelect" | ||
value={filter} | ||
onChange={handleChangeFilter} | ||
> | ||
<option value="all">All</option> | ||
<option value="active">Active</option> | ||
<option value="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={query} | ||
onChange={handleChangeQuery} | ||
/> | ||
<span className="icon is-left"> | ||
<i className="fas fa-magnifying-glass" /> | ||
</span> | ||
|
||
{query && ( | ||
<span className="icon is-right" style={{ pointerEvents: 'all' }}> | ||
<button | ||
data-cy="clearSearchButton" | ||
type="button" | ||
className="delete" | ||
onClick={handleClearQuery} | ||
/> | ||
</span> | ||
)} | ||
</p> | ||
</form> | ||
); | ||
}; |
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.
Ensure that the
handleFilterChange
function inTodoFilter
correctly updates thefilteredTodos
state. Verify that theTodoFilter
component is implemented to handle this function properly, as it is crucial for filtering functionality.