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

add task solution #2719

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
64 changes: 58 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,53 @@
/* 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 { OptionType } from './types/OptionType';
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 [option, setOption] = useState<OptionType>('all');
const [listLoading, setListLoading] = useState(false);
const [searchInput, setSearchInput] = useState('');
const [selectedTodo, setSelectedTodo] = useState<Todo | null>(null);

useEffect(() => {
const fetchData = async () => {
setListLoading(true);
const todosDb = await getTodos();

setTodos(todosDb);
setListLoading(false);
};

fetchData();
}, []);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure the Loader component is displayed while waiting for the todos to be fetched. This aligns with the task requirement to show the Loader when waiting for any data from the server .


useEffect(() => {
let filtered = todos;

switch (option) {
case 'active':
filtered = todos.filter(todo => !todo.completed);
break;
case 'completed':
filtered = todos.filter(todo => todo.completed);
break;
default:
filtered = todos;
}

setFilteredTodos(filtered);
}, [option, todos]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filtering logic based on the option state is correct. However, ensure that the searchInput is also used to filter the todos by title, as required by the task .


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

<div className="block">
<TodoFilter />
<TodoFilter
setOption={setOption}
searchInput={searchInput}
setSearchInput={setSearchInput}
/>
</div>

<div className="block">
<Loader />
<TodoList />
{listLoading ? (
<Loader />
) : (
<TodoList
todos={filteredTodos}
searchParams={searchInput}
setSelectedTodo={setSelectedTodo}
selectedTodo={selectedTodo}
/>
)}
</div>
</div>
</div>
</div>

<TodoModal />
{selectedTodo && (
<TodoModal todo={selectedTodo} setSelectedTodo={setSelectedTodo} />
)}
</>
);
};
83 changes: 55 additions & 28 deletions src/components/TodoFilter/TodoFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,57 @@
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 { OptionType } from '../../types/OptionType';

<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 = {
setOption: (option: OptionType) => void;
searchInput: string;
setSearchInput: (searchInput: 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> = ({
setOption,
searchInput,
setSearchInput,
}) => {
return (
<form className="field has-addons">
<p className="control">
<span className="select">
<select
data-cy="statusSelect"
onChange={e => setOption(e.currentTarget.value as OptionType)}
>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the type casting of e.currentTarget.value to OptionType is safe. Consider validating the value before casting to avoid potential runtime errors if the value is not one of the expected OptionType values.

<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"
value={searchInput}
onChange={e => setSearchInput(e.target.value)}
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 */}
{searchInput && (
<button
data-cy="clearSearchButton"
type="button"
className="delete"
onClick={() => setSearchInput('')}
/>
)}
</span>
</p>
</form>
);
};
177 changes: 84 additions & 93 deletions src/components/TodoList/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1,100 +1,91 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import { Todo } from '../../types/Todo';

export const TodoList: React.FC = () => (
<table className="table is-narrow is-fullwidth">
<thead>
<tr>
<th>#</th>
<th>
<span className="icon">
<i className="fas fa-check" />
</span>
</th>
<th>Title</th>
<th> </th>
</tr>
</thead>
type Props = {
todos: Todo[];
searchParams: string;
setSelectedTodo: (todo: Todo | null) => void;
selectedTodo: Todo | null;
};

<tbody>
<tr data-cy="todo" className="">
<td className="is-vcentered">1</td>
<td className="is-vcentered" />
<td className="is-vcentered is-expanded">
<p className="has-text-danger">delectus aut autem</p>
</td>
<td className="has-text-right is-vcentered">
<button data-cy="selectButton" className="button" type="button">
<span className="icon">
<i className="far fa-eye" />
</span>
</button>
</td>
</tr>
<tr data-cy="todo" className="has-background-info-light">
<td className="is-vcentered">2</td>
<td className="is-vcentered" />
<td className="is-vcentered is-expanded">
<p className="has-text-danger">quis ut nam facilis et officia qui</p>
</td>
<td className="has-text-right is-vcentered">
<button data-cy="selectButton" className="button" type="button">
<span className="icon">
<i className="far fa-eye-slash" />
</span>
</button>
</td>
</tr>
export const TodoList: React.FC<Props> = ({
todos,
searchParams,
setSelectedTodo,
selectedTodo,
}) => {
const [showTodos, setShowTodos] = useState(todos);

<tr data-cy="todo" className="">
<td className="is-vcentered">1</td>
<td className="is-vcentered" />
<td className="is-vcentered is-expanded">
<p className="has-text-danger">delectus aut autem</p>
</td>
<td className="has-text-right is-vcentered">
<button data-cy="selectButton" className="button" type="button">
<span className="icon">
<i className="far fa-eye" />
</span>
</button>
</td>
</tr>
useEffect(() => {
if (searchParams === '') {
setShowTodos(todos);

<tr data-cy="todo" className="">
<td className="is-vcentered">6</td>
<td className="is-vcentered" />
<td className="is-vcentered is-expanded">
<p className="has-text-danger">
qui ullam ratione quibusdam voluptatem quia omnis
</p>
</td>
<td className="has-text-right is-vcentered">
<button data-cy="selectButton" className="button" type="button">
<span className="icon">
<i className="far fa-eye" />
</span>
</button>
</td>
</tr>
return;
} else {
setShowTodos(
todos.filter(todo =>
todo.title.toLowerCase().includes(searchParams.toLowerCase()),
),
);
}
}, [searchParams, todos]);

<tr data-cy="todo" className="">
<td className="is-vcentered">8</td>
<td className="is-vcentered">
<span className="icon" data-cy="iconCompleted">
<i className="fas fa-check" />
</span>
</td>
<td className="is-vcentered is-expanded">
<p className="has-text-success">quo adipisci enim quam ut ab</p>
</td>
<td className="has-text-right is-vcentered">
<button data-cy="selectButton" className="button" type="button">
return (
<table className="table is-narrow is-fullwidth">
<thead>
<tr>
<th>#</th>
<th>
<span className="icon">
<i className="far fa-eye" />
<i className="fas fa-check" />
</span>
</button>
</td>
</tr>
</tbody>
</table>
);
</th>
<th>Title</th>
<th> </th>
</tr>
</thead>

<tbody>
{showTodos.map(todo => (
<tr key={todo.id} data-cy="todo" className="">
<td className="is-vcentered">{todo.id}</td>
<td className="is-vcentered">
{todo.completed && (
<span className="icon" data-cy="iconCompleted">
<i className="fas fa-check" />
</span>
)}
</td>
<td className="is-vcentered is-expanded">
<p
className={
todo.completed ? 'has-text-success' : 'has-text-danger'
}
>
{todo.title}
</p>
</td>
<td className="has-text-right is-vcentered">
<button
data-cy="selectButton"
className="button"
type="button"
onClick={() => setSelectedTodo(todo)}
>
<span className="icon">
<i
className={
todo.id === selectedTodo?.id
? 'far fa-eye-slash'
: 'far fa-eye'
}
/>
</span>
</button>
</td>
</tr>
))}
</tbody>
</table>
);
};
Loading
Loading