Skip to content
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

solution #2584

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -8,7 +8,26 @@ import { TodoFilter } from './components/TodoFilter';
import { TodoModal } from './components/TodoModal';
import { Loader } from './components/Loader';

import { getTodos } from './api';

import { Todo } from './types/Todo';

export const App: React.FC = () => {
const [todos, setTodos] = useState<Todo[]>([]);
const [activeTodo, setActiveTodo] = useState<Todo | null>(null);
const [loading, setLoading] = useState(true);
const [showModal, setShowModal] = useState(false);

useEffect(() => {
setLoading(true);

getTodos()
.then(setTodos)
.finally(() => {
setLoading(false);
});
}, []);

return (
<>
<div className="section">
Expand All @@ -17,18 +36,32 @@ export const App: React.FC = () => {
<h1 className="title">Todos:</h1>

<div className="block">
<TodoFilter />
<TodoFilter setTodos={setTodos} />
</div>

<div className="block">
<Loader />
<TodoList />
{loading && <Loader />}

{!loading && todos.length > 0 && (
<TodoList
todos={todos}
setActiveTodo={setActiveTodo}
activeTodo={activeTodo}
setShowModal={setShowModal}
/>
)}
</div>
</div>
</div>
</div>

<TodoModal />
{showModal && activeTodo && (
<TodoModal
setActiveTodo={setActiveTodo}
activeTodo={activeTodo}
setShowModal={setShowModal}
/>
)}
</>
);
};
33 changes: 33 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Todo } from './types/Todo';
import { TodoStatus } from './types/TodoStatus';
import { User } from './types/User';

// eslint-disable-next-line operator-linebreak
Expand All @@ -25,4 +26,36 @@ function get<T>(url: string): Promise<T> {

export const getTodos = () => get<Todo[]>('/todos');

export const getSortedTodos = () => {
return getTodos().then(todos => todos.sort((a, b) => a.id - b.id));
};

export const getCompletedTodos = () => {
return getSortedTodos().then(todos =>
todos.filter(todo => todo.completed === true),
);
};

export const getActiveTodos = () => {
return getSortedTodos().then(todos =>
todos.filter(todo => todo.completed === false),
);
};

export const getFilteredTodos = (value: TodoStatus) => {
switch (value) {
case TodoStatus.all:
return getSortedTodos();

case TodoStatus.active:
return getActiveTodos();

case TodoStatus.completed:
return getCompletedTodos();

default:
return getSortedTodos();
}
};

export const getUser = (userId: number) => get<User>(`/users/${userId}`);
118 changes: 88 additions & 30 deletions src/components/TodoFilter/TodoFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,88 @@
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 React, { useEffect, useState } from 'react';
import { getFilteredTodos } from '../../api';
import { Todo } from '../../types/Todo';
import { TodoStatus } from '../../types/TodoStatus';

type Props = {
setTodos: (todos: Todo[]) => void;
};

export const TodoFilter: React.FC<Props> = ({ setTodos = () => {} }) => {
const [query, setQuery] = useState('');
const [selectedValue, setSelectedValue] = useState<TodoStatus>(
TodoStatus.all,
);

const handleQueryChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const newValue = event.target.value;

setQuery(newValue);
};

const handleDelete = () => {
setQuery('');
};

useEffect(() => {
if (query) {
getFilteredTodos(selectedValue)
.then(todos =>
todos.filter(todo =>
todo.title.toLowerCase().includes(query.toLowerCase()),
),
)
.then(setTodos);
} else {
getFilteredTodos(selectedValue).then(setTodos);
}
}, [selectedValue, query]);

Check warning on line 38 in src/components/TodoFilter/TodoFilter.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

React Hook useEffect has a missing dependency: 'setTodos'. Either include it or remove the dependency array

return (
<form className="field has-addons">
<p className="control">
<span className="select">
<select
data-cy="statusSelect"
onChange={e => {
setSelectedValue(e.target.value as TodoStatus);
}}
>
<option value={TodoStatus.all}>All</option>
<option value={TodoStatus.active}>Active </option>
<option value={TodoStatus.completed}>Completed</option>
</select>
</span>
</p>

<p className="control is-expanded has-icons-left has-icons-right">
<input
value={query}
onChange={event => {
handleQueryChange(event);
}}
data-cy="searchInput"
type="text"
className="input"
placeholder="Search..."
/>
<span className="icon is-left">
<i className="fas fa-magnifying-glass" />
</span>

{query && (
<span className="icon is-right" style={{ pointerEvents: 'all' }}>
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */}
<button
onClick={() => {
handleDelete();
}}
data-cy="clearSearchButton"
type="button"
className="delete"
/>
</span>
)}
</p>
</form>
);
};
Loading
Loading