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

task complete #1066

Open
wants to merge 1 commit 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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ using the Redux. It should look and work identically, so use the same markup.
- implement `features/todos` storing an array of todos;
- load the todos in the `App` on page load (don't use Redux Thunk for now);
- `useAppSelector` already aware of `RootState` so you can write selectors in your
components (no need to write them in the store file)
components (no need to write them in the store file)

## Instructions

- Install Prettier Extention and use this [VSCode settings](https://mate-academy.github.io/fe-program/tools/vscode/settings.json) to enable format on save.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_redux-list-of-todos/)
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://boikoua.github.io/react_redux-list-of-todos/)
- Follow the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline)
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 @@ -18,7 +18,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
78 changes: 62 additions & 16 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,72 @@
import 'bulma/css/bulma.css';
import '@fortawesome/fontawesome-free/css/all.css';
import { Loader, TodoFilter, TodoList, TodoModal } from './components';
import { useEffect, useState } from 'react';
import { getTodos } from './api';
import { StatusType } from './types/Status';
import { useDispatch } from 'react-redux';
import { setTodos } from './features/todos';
import { setStatus } from './features/filter';
import { useAppSelector } from './app/hooks';

export const App = () => (
<>
<div className="section">
<div className="container">
<div className="box">
<h1 className="title">Todos:</h1>
export const App = () => {
const dispatch = useDispatch();
const todos = useAppSelector(state => state.todos);
const { query, status } = useAppSelector(state => state.filter);
const selectTodo = useAppSelector(state => state.currentTodo);

<div className="block">
<TodoFilter />
</div>
const [isLoading, setIsLoading] = useState(false);

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

getTodos()
.then(todosFromServer => {
dispatch(setTodos(todosFromServer));
})
.catch(error => {
throw new Error(error);
})
.finally(() => {
setIsLoading(false);
dispatch(setStatus('all'));
});
Comment on lines +23 to +33

Choose a reason for hiding this comment

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

Error handling: You're throwing an error when the promise from getTodos() is rejected, but you're not catching it anywhere. This could potentially crash your application. Consider handling this error in a user-friendly way, for instance, by showing an error message to the user.

}, [dispatch]);
Comment on lines +20 to +34

Choose a reason for hiding this comment

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

Dependency array in useEffect: You've added dispatch to the dependency array of your useEffect. However, since dispatch does not change during the lifecycle of your component, it is not necessary to include it in the dependency array. You can safely remove it.


const preparedTodos = todos
.filter(todo => todo.title.toLowerCase().includes(query.toLowerCase()))
.filter(todo => {
switch (status) {
case StatusType.Active:
return !todo.completed;

case StatusType.Completed:
return todo.completed;

default:
return true;
}
});
Comment on lines +36 to +49

Choose a reason for hiding this comment

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

Performance issue: You're filtering your todos twice. This could be inefficient if your list of todos is very large. Consider combining these two filters into one to improve performance.


return (
<>
<div className="section">
<div className="container">
<div className="box">
<h1 className="title">Todos:</h1>

<div className="block">
<TodoFilter />
</div>

<div className="block">
<Loader />
<TodoList />
<div className="block">
{isLoading ? <Loader /> : <TodoList todos={preparedTodos} />}
</div>
</div>
</div>
</div>
</div>

<TodoModal />
</>
);
{selectTodo && <TodoModal />}
</>
);
};
4 changes: 4 additions & 0 deletions src/app/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { TypedUseSelectorHook, useSelector } from 'react-redux';
import { RootState } from './store';

export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
9 changes: 8 additions & 1 deletion src/app/store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { combineSlices, configureStore } from '@reduxjs/toolkit';
import todosReducer from '../features/todos';
import filterReducer from '../features/filter';
import currentTodoReducer from '../features/currentTodo';

const rootReducer = combineSlices();
const rootReducer = combineSlices({
todos: todosReducer,
filter: filterReducer,
currentTodo: currentTodoReducer,
});

export const store = configureStore({
reducer: rootReducer,
Expand Down
58 changes: 45 additions & 13 deletions src/components/TodoFilter/TodoFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
import React from 'react';
import { Status, StatusType } from '../../types/Status';
import { useDispatch } from 'react-redux';
import { setQuery, setStatus } from '../../features/filter';
import { useAppSelector } from '../../app/hooks';

export const TodoFilter = () => {
const dispatch = useDispatch();
const { status, query } = useAppSelector(state => state.filter);

const showOptions = Object.values(StatusType).map(stat => (
<option key={stat} value={stat}>
{stat[0].toUpperCase() + stat.slice(1)}
</option>
));
Comment on lines +11 to +15

Choose a reason for hiding this comment

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

The showOptions constant is re-calculated on each render of the component. It doesn't depend on any props or state, so you could move it outside of the component to avoid unnecessary calculations. For example, you can define it in the module scope, just after imports.


const handleChangeStatus = (event: React.ChangeEvent<HTMLSelectElement>) => {
dispatch(setStatus(event.target.value as Status));

Choose a reason for hiding this comment

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

You are casting the event target value to Status type. It would be safer to check if the value is actually a valid Status before dispatching the action. This will help prevent any unexpected values from being dispatched to the state.

};

const handleChangeSearch = (event: React.ChangeEvent<HTMLInputElement>) => {
dispatch(setQuery(event.target.value));
};

const handleClearSearch = () => {
dispatch(setQuery(''));
};

export const TodoFilter: React.FC = () => {
return (
<form
className="field has-addons"
onSubmit={event => event.preventDefault()}
>
<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
data-cy="statusSelect"
value={status}
onChange={handleChangeStatus}
>
{showOptions}
</select>
</span>
</p>
Expand All @@ -22,19 +49,24 @@ export const TodoFilter: React.FC = () => {
type="text"
className="input"
placeholder="Search..."
value={query}
onChange={handleChangeSearch}
/>
<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>
{!!query.length && (
<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={handleClearSearch}
/>
Comment on lines +62 to +67

Choose a reason for hiding this comment

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

While it's great that you're considering accessibility by using jsx-a11y/control-has-associated-label, it would be better to actually provide a label for the button. It can be hidden visually but still accessible to screen readers. This would improve the accessibility of your app.

</span>
)}
</p>
</form>
);
Expand Down
57 changes: 57 additions & 0 deletions src/components/TodoItem/TodoItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import cn from 'classnames';
import { Todo } from '../../types/Todo';
import { useAppSelector } from '../../app/hooks';

type Props = {
todo: Todo;
onSelect: (todo: Todo) => void;
};

export const TodoItem: React.FC<Props> = ({ todo, onSelect }) => {
const { id, title, completed } = todo;
const currentTodo = useAppSelector(state => state.currentTodo);

const handleSelectTodo = () => {
onSelect(todo);
};

return (
<tr data-cy="todo">
<td className="is-vcentered">{id}</td>
<td className="is-vcentered">
{completed ? (
<span className="icon" data-cy="iconCompleted">
<i className="fas fa-check" />
</span>
) : (
''
)}
</td>

<td className="is-vcentered is-expanded">
<p

Choose a reason for hiding this comment

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

The use of the 'cn' library here seems unnecessary. You could directly write the ternary operator inside the className property. It would make the code cleaner and easier to read.

className={cn({
'has-text-danger': !completed,
'has-text-success': completed,
})}
>
{title}
</p>
</td>

<td className="has-text-right is-vcentered">
<button data-cy="selectButton" className="button" type="button">
{id === currentTodo?.id ? (
<span className="icon">
<i className="far fa-eye-slash" />
</span>
) : (
<span className="icon" onClick={handleSelectTodo}>

Choose a reason for hiding this comment

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

The onClick event is applied to the span element, it is better to apply it to the button element. This way, the entire button is clickable, not just the icon within the span.

<i className="far fa-eye" />
</span>
)}
</button>
</td>
</tr>
);
};
1 change: 1 addition & 0 deletions src/components/TodoItem/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './TodoItem';
Loading
Loading