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

Develop #2619

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Develop #2619

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://Vladimir-Zadorozhnyi.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
50 changes: 42 additions & 8 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
/* 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 { TodoList } from './components/TodoList';
import { TodoFilter } from './components/TodoFilter';
import { TodoModal } from './components/TodoModal';
import { Loader } from './components/Loader';
import { getTodos } from './api';
import { TodoList } from './components/TodoList';
import { TodoModal } from './components/TodoModal';
import { Todo } from './types/Todo';

export const App: React.FC = () => {
const [todosFromServer, setTodosFromServer] = useState<Todo[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [filteredTodos, setFilteredTodos] = useState<Todo[]>([]);
const [selectedTodoId, setSelectedTodoId] = useState(0);

useEffect(() => {
setIsLoading(true);
getTodos()
.then(todos => {
setTodosFromServer(todos);
setFilteredTodos(todos);
})
.finally(() => setIsLoading(false));
}, []);

const selectedTodo = useMemo(() => {
return filteredTodos.find(todo => todo.id === selectedTodoId);
}, [filteredTodos, selectedTodoId]);

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

<div className="block">
<TodoFilter />
<TodoFilter
todos={todosFromServer}
handleFilterChange={setFilteredTodos}
/>
</div>
Comment on lines +40 to +43

Choose a reason for hiding this comment

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

Ensure that the handleFilterChange function in TodoFilter correctly updates the filteredTodos state. Verify that the TodoFilter component is implemented to handle this function properly, as it is crucial for filtering functionality.


<div className="block">
<Loader />
<TodoList />
{isLoading && <Loader />}
{!isLoading && (
<TodoList
todos={filteredTodos}
selectedTodoId={selectedTodoId}
handleSelectTodoId={setSelectedTodoId}
/>
)}
Comment on lines +49 to +53

Choose a reason for hiding this comment

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

Check that the TodoList component correctly handles the handleSelectTodoId function to update the selectedTodoId state. This is important for selecting and displaying the correct todo in the TodoModal.

</div>
</div>
</div>
</div>

<TodoModal />
{selectedTodo && (
<TodoModal
todo={selectedTodo}
onSetSelectedTodoId={setSelectedTodoId}
/>
Comment on lines +61 to +63

Choose a reason for hiding this comment

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

Ensure that the TodoModal component uses the onSetSelectedTodoId function to clear or update the selectedTodoId when the modal is closed or the todo is modified. This will help maintain the correct state of the selected todo.

)}
</>
);
};
2 changes: 0 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Todo } from './types/Todo';
import { User } from './types/User';

// eslint-disable-next-line operator-linebreak
const BASE_URL =
'https://mate-academy.github.io/react_dynamic-list-of-todos/api';

Expand All @@ -14,7 +13,6 @@ function wait(delay: number): Promise<void> {
}

function get<T>(url: string): Promise<T> {
// eslint-disable-next-line prefer-template
const fullURL = BASE_URL + url + '.json';

// we add some delay to see how the loader works
Expand Down
135 changes: 105 additions & 30 deletions src/components/TodoFilter/TodoFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,105 @@
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 { useState, useEffect, useCallback } from 'react';
import { Todo } from '../../types/Todo';

interface TodoFilterProps {
todos: Todo[];
handleFilterChange: (filteredTodos: Todo[]) => void;
}

export enum FilterOptions {
ALL = 'all',
COMPLETED = 'completed',
ACTIVE = 'active',
}

export const TodoFilter: React.FC<TodoFilterProps> = ({
todos,
handleFilterChange,
}) => {
const [filter, setFilter] = useState<FilterOptions>(FilterOptions.ALL);
const [query, setQuery] = useState('');

const handleFilterTodos = () => {
let filteredTodos = todos.filter(todo => {
switch (filter) {
case FilterOptions.ACTIVE:
return !todo.completed;
case FilterOptions.COMPLETED:
return todo.completed;
default:
return true;
}
});

if (query) {
filteredTodos = filteredTodos.filter(todo =>
todo.title.toLowerCase().includes(query.toLowerCase()),
);
}

handleFilterChange(filteredTodos);
};

useEffect(() => {
handleFilterTodos();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [filter, query, todos]);

Choose a reason for hiding this comment

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

The comment to disable the eslint rule for exhaustive dependencies is present. Ensure that the dependencies for useEffect are correct and that this rule is disabled intentionally. If the handleFilterTodos function changes, consider updating the dependencies accordingly.

const handleChangeFilter = useCallback(
(event: React.ChangeEvent<HTMLSelectElement>) => {
setFilter(event.target.value as FilterOptions);
},
[],
);

Choose a reason for hiding this comment

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

The handleChangeFilter and handleChangeQuery callbacks have empty dependency arrays, which is correct if they don't rely on any external variables. Ensure that this is intentional and that no external state or props are used within these callbacks.

const handleChangeQuery = useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
setQuery(event.target.value);
},
[],
);
const handleClearQuery = useCallback(() => {
setQuery('');
}, []);

return (
<form className="field has-addons">
{' '}
<p className="control">
<span className="select">
<select
data-cy="statusSelect"
value={filter}
onChange={handleChangeFilter}
>
<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..."
value={query}
onChange={handleChangeQuery}
/>
<span className="icon is-left">
<i className="fas fa-magnifying-glass" />
</span>

{query && (
<span className="icon is-right" style={{ pointerEvents: 'all' }}>
<button
data-cy="clearSearchButton"
type="button"
className="delete"
onClick={handleClearQuery}
/>
</span>
)}
</p>
</form>
);
};
Loading
Loading