-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
8bf0f2b
commit fb519c9
Showing
5 changed files
with
185 additions
and
115 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,78 @@ | ||
/* 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 { Todo } from './types/Todo'; | ||
import { getTodos } from './api'; | ||
|
||
export const App: React.FC = () => { | ||
const [todos, setTodos] = useState<Todo[]>([]); | ||
const [filteredTodos, setFilteredTodos] = useState<Todo[]>([]); | ||
const [selectedTodo, setSelectedTodo] = useState<Todo | null>(null); | ||
const [loading, setLoading] = useState(false); | ||
const [query, setQuery] = useState(''); | ||
const [status, setStatus] = useState<'all' | 'completed' | 'active'>('all'); | ||
|
||
useEffect(() => { | ||
setLoading(true); | ||
getTodos() | ||
.then(data => { | ||
setTodos(data); | ||
setFilteredTodos(data); | ||
}) | ||
.finally(() => setLoading(false)); | ||
}, []); | ||
|
||
useEffect(() => { | ||
let result = todos; | ||
|
||
if (status !== 'all') { | ||
result = todos.filter( | ||
todo => todo.completed === (status === 'completed'), | ||
); | ||
} | ||
|
||
if (query) { | ||
result = result.filter(todo => | ||
todo.title.toLowerCase().includes(query.toLowerCase()), | ||
); | ||
} | ||
|
||
setFilteredTodos(result); | ||
}, [todos, status, query]); | ||
|
||
return ( | ||
<> | ||
<div className="section"> | ||
<div className="container"> | ||
<div className="box"> | ||
<h1 className="title">Todos:</h1> | ||
|
||
<div className="block"> | ||
<TodoFilter /> | ||
<TodoFilter | ||
query={query} | ||
setQuery={setQuery} | ||
status={status} | ||
setStatus={setStatus} | ||
/> | ||
</div> | ||
|
||
<div className="block"> | ||
<Loader /> | ||
<TodoList /> | ||
{loading && <Loader />} | ||
<TodoList | ||
todos={filteredTodos} | ||
selectedTodoId={selectedTodo?.id ?? null} | ||
onSelectTodo={setSelectedTodo} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<TodoModal /> | ||
{selectedTodo && ( | ||
<TodoModal todo={selectedTodo} onClose={() => setSelectedTodo(null)} /> | ||
)} | ||
</> | ||
); | ||
}; |
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
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