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

feat(todo): implement modal opening, filtering, and state management #2752

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
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://misha-vynnyk.github.io/react_dynamic-list-of-todos/) and add it to the PR description.
119 changes: 114 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,108 @@
/* eslint-disable max-len */
import React from 'react';
// #region import
import React, { useEffect, useMemo, useState } from 'react';
import 'bulma/css/bulma.css';
import '@fortawesome/fontawesome-free/css/all.css';
import { getTodos, getUser } from './api';

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';
// #endregion import

export enum Filter {
all = 'all',
active = 'active',
completed = 'completed',
}

export const filterLabels: { [key in Filter]: string } = {
[Filter.all]: 'All',
[Filter.active]: 'Active',
[Filter.completed]: 'Completed',
};

export const App: React.FC = () => {
const [todos, setTodos] = useState<Todo[]>([]);
const [currentTodo, setCurrentTodo] = useState<Todo | null>(null);

const [isModalOpen, setIsModalOpen] = useState(false);
const [openTodoId, setOpenTodoId] = useState<number | null>(null);

const [todosLoading, setTodosLoading] = useState(false);
const [modalLoading, setModalLoading] = useState(false);

const [filterValue, setFilterValue] = useState<Filter>(Filter.all);
const [searchTerm, setSearchTerm] = useState('');

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

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

useEffect(() => {
if (!currentTodo || currentTodo.user) {

Choose a reason for hiding this comment

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

The condition !currentTodo || currentTodo.user might lead to unexpected behavior if currentTodo.user is falsy but valid. Ensure this logic correctly reflects the intended check for whether the user data needs to be fetched.

return;
}

getUser(currentTodo.userId)
.then(user => {
setCurrentTodo(prevTodo => {
if (prevTodo?.id === currentTodo.id) {
return { ...prevTodo, user };
}

return prevTodo;
Comment on lines +56 to +61

Choose a reason for hiding this comment

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

The logic inside setCurrentTodo could be simplified or clarified. Ensure that the condition prevTodo?.id === currentTodo.id is necessary and correctly handles all cases where the user data should be updated.

});
})
.finally(() => setModalLoading(false));
}, [currentTodo]);

const handleOpenModal = (todo: Todo): void => {
setOpenTodoId(todo.id);
setModalLoading(true);
setIsModalOpen(true);

setCurrentTodo(todo);
};

const filteredAndSearchedTodos = useMemo(() => {
return todos
.filter(todo => {
switch (filterValue) {
case Filter.completed:
return todo.completed;
case Filter.active:
return !todo.completed;
case Filter.all:
default:
return true;
}
})
.filter(todo => {
return todo.title.toLowerCase().includes(searchTerm.toLowerCase());
});
}, [todos, filterValue, searchTerm]);

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

const handleCloseModal = (): void => {
setIsModalOpen(false);
setCurrentTodo(null);
};

const handleClearSearch = () => {
setSearchTerm('');
};

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

<div className="block">
<TodoFilter />
<TodoFilter
setFilterValue={setFilterValue}
searchTerm={searchTerm}
handleSearchChange={handleSearchChange}
handleClearSearch={handleClearSearch}
/>
</div>

<div className="block">
<Loader />
<TodoList />
{todosLoading && <Loader />}
<TodoList
todos={filteredAndSearchedTodos}
handleOpenModal={handleOpenModal}
isModalOpen={isModalOpen}
openTodoId={openTodoId}
/>
</div>
</div>
</div>
</div>

<TodoModal />
<TodoModal
currentTodo={currentTodo}
isModalOpen={isModalOpen}
modalLoading={modalLoading}
handleCloseModal={handleCloseModal}
/>
</>
);
};
86 changes: 58 additions & 28 deletions src/components/TodoFilter/TodoFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,60 @@
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, filterLabels } from '../../App';

<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>
interface Props {
setFilterValue: React.Dispatch<React.SetStateAction<Filter>>;
searchTerm: string;
handleSearchChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
handleClearSearch: () => 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> = ({
setFilterValue,
searchTerm,
handleSearchChange,
handleClearSearch,
}) => {
return (
<form className="field has-addons">
<p className="control">
<span className="select">
<select
data-cy="statusSelect"
onChange={event => setFilterValue(event.target.value as Filter)}
>
{Object.values(Filter).map(filter => (
<option key={filter} value={filter}>
{filterLabels[filter]}
</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={searchTerm}
onChange={handleSearchChange}
/>
<span className="icon is-left">
<i className="fas fa-magnifying-glass" />
</span>

<span className="icon is-right" style={{ pointerEvents: 'all' }}>
{searchTerm && (
<button
aria-label="Clear search input"
data-cy="clearSearchButton"
type="button"
className="delete"
onClick={handleClearSearch}
/>
)}
</span>
</p>
</form>
);
};
Loading