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 #2572

Open
wants to merge 9 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ loaded and show them using `TodoList` (check the code in the `api.ts`);
- Implement a solution following the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline).
- Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript).
- Open one more terminal and run tests with `npm test` to ensure your solution is correct.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_dynamic-list-of-todos/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://Vasyl-Zhyliakov.github.io/react_dynamic-list-of-todos/) and add it to the PR description.
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"devDependencies": {
"@cypress/react18": "^2.0.1",
"@mate-academy/scripts": "^1.8.5",
"@mate-academy/scripts": "^1.9.12",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^20.14.10",
Expand Down
66 changes: 61 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,57 @@
/* 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 { Todo } from './types/Todo';
import { Filter } from './types/Filter';

import { TodoList } from './components/TodoList';
import { TodoFilter } from './components/TodoFilter';
import { TodoModal } from './components/TodoModal';
import { Loader } from './components/Loader';
import { getTodos } from './api';

export const App: React.FC = () => {
const [todos, setTodos] = useState<Todo[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [filter, setFilter] = useState(Filter.All);
const [searchValue, setSearchValue] = useState('');
const [activeTodo, setActiveTodo] = useState<Todo | null>(null);

const handleEyeChange = (id: number) => {
setActiveTodo(todos.find(todo => todo.id === id) || null);
};

const filteredTodos = useMemo(() => {
return todos.filter(todo =>
todo.title.toLowerCase().includes(searchValue.toLowerCase()),
);
}, [todos, searchValue]);

const getVisibleTodos = () => {
switch (filter) {
case Filter.All:
return filteredTodos;
case Filter.Active:
return filteredTodos.filter(todo => !todo.completed);
case Filter.Completed:
return filteredTodos.filter(todo => todo.completed);

default:
return filteredTodos;
}
};

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

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

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

<div className="block">
<TodoFilter />
<TodoFilter
setFilter={setFilter}
setSearchValue={setSearchValue}
searchValue={searchValue}
/>
</div>

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

{!isLoading && (
<TodoList
handleEyeChange={handleEyeChange}
visibleTodos={getVisibleTodos()}
activeTodo={activeTodo}
/>
)}
</div>
</div>
</div>
</div>

<TodoModal />
{activeTodo && (
<TodoModal activeTodo={activeTodo} setActiveTodo={setActiveTodo} />
)}
</>
);
};
2 changes: 1 addition & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ function get<T>(url: string): Promise<T> {

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

export const getUser = (userId: number) => get<User>(`/users/${userId}`);
export const getUser = (userId: number | null) => get<User>(`/users/${userId}`);
87 changes: 59 additions & 28 deletions src/components/TodoFilter/TodoFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,61 @@
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 { Filter } from '../../types/Filter';

<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 = {
setFilter: (filter: Filter) => void;
setSearchValue: React.Dispatch<React.SetStateAction<string>>;
searchValue: string;
};

<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> = ({
setFilter = () => {},
setSearchValue = () => {},
searchValue = '',
}) => {
const handleSelectChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
setFilter(event.target.value as Filter);
};

const handleSearchChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setSearchValue(event.target.value);
};

return (
<form className="field has-addons">
<p className="control">
<span className="select">
<select data-cy="statusSelect" onChange={handleSelectChange}>
<option value={Filter.All}>All</option>
<option value={Filter.Active}>Active</option>
<option value={Filter.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={searchValue}
onChange={handleSearchChange}
/>
<span className="icon is-left">
<i className="fas fa-magnifying-glass" />
</span>

{searchValue && (
<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"
onClick={() => setSearchValue('')}
/>
</span>
)}
</p>
</form>
);
};
Loading
Loading