diff --git a/src/App.tsx b/src/App.tsx index d46111e825..92b5ea2a87 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,5 +1,5 @@ /* 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'; @@ -7,8 +7,39 @@ 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 { Todo } from './types/Todo'; +import { Filters } from './types/Filters'; export const App: React.FC = () => { + const [todos, setTodos] = useState([]); + const [isLoading, setIsLoading] = useState(true); + const [selectedTodoId, setSelectedTodoId] = useState(null); + const [query, setQuery] = useState(''); + const [filterParameter, setFilterParameter] = useState(Filters.All); + + const selectedTodo = todos.find(todo => todo.id === selectedTodoId); + + const filteredTodos: Todo[] = todos + .filter(todo => todo.title.toLowerCase().includes(query.toLowerCase())) + .filter(todo => { + switch (filterParameter) { + case Filters.Completed: + return todo.completed; + case Filters.Active: + return !todo.completed; + case Filters.All: + default: + return true; + } + }); + + useEffect(() => { + getTodos() + .then(data => setTodos(data)) + .finally(() => setIsLoading(false)); + }, []); + return ( <>
@@ -17,18 +48,34 @@ export const App: React.FC = () => {

Todos:

- +
- - + {isLoading ? ( + + ) : ( + + )}
- + {selectedTodoId && selectedTodo && ( + setSelectedTodoId(null)} + /> + )} ); }; diff --git a/src/components/TodoFilter/TodoFilter.tsx b/src/components/TodoFilter/TodoFilter.tsx index 193f1cd2b2..45bb350dd8 100644 --- a/src/components/TodoFilter/TodoFilter.tsx +++ b/src/components/TodoFilter/TodoFilter.tsx @@ -1,30 +1,57 @@ -export const TodoFilter = () => ( -
-

- - - -

+import React, { Dispatch, SetStateAction } from 'react'; +import { Filters } from '../../types/Filters'; -

- - - - +type Props = { + query: string; + setQuery: Dispatch>; + setFilter: Dispatch>; +}; - - {/* eslint-disable-next-line jsx-a11y/control-has-associated-label */} -