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

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

Develop #1029

Show file tree
Hide file tree
Changes from 4 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
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
55 changes: 39 additions & 16 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,49 @@
import 'bulma/css/bulma.css';
import '@fortawesome/fontawesome-free/css/all.css';
import { Loader, TodoFilter, TodoList, TodoModal } from './components';
import { AppDispatch, RootState } from './app/store';
import { useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { getTodos } from './api';
import { todosActions } from './features/todos';

export const App = () => (
<>
<div className="section">
<div className="container">
<div className="box">
<h1 className="title">Todos:</h1>
export const App = () => {
const dispatch = useDispatch<AppDispatch>();
const currentTodo = useSelector((state: RootState) => state.currentTodo);

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

useEffect(() => {
setIsTodosLoading(true);
getTodos()
.then(todosFromServer => {
dispatch(todosActions.setTodos(todosFromServer));
})
.catch()
.finally(() => {
setIsTodosLoading(false);
});
}, [dispatch]);

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">
{isTodosLoading ? <Loader /> : <TodoList />}
</div>
</div>
</div>
</div>
</div>

<TodoModal />
</>
);
{currentTodo && <TodoModal />}
</>
);
};
5 changes: 4 additions & 1 deletion src/app/store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { combineSlices, configureStore } from '@reduxjs/toolkit';
import { currentTodoSlice } from '../features/currentTodo';
import { filterSlice } from '../features/filter';
import { todosSlice } from '../features/todos';

const rootReducer = combineSlices();
const rootReducer = combineSlices(currentTodoSlice, filterSlice, todosSlice);

export const store = configureStore({
reducer: rootReducer,
Expand Down
48 changes: 36 additions & 12 deletions src/components/TodoFilter/TodoFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { AppDispatch, RootState } from '../../app/store';
import { filterActions } from '../../features/filter';
import { Status } from '../../types/Status';

export const TodoFilter: React.FC = () => {
const dispatch = useDispatch<AppDispatch>();
const query = useSelector((state: RootState) => state.filter.query);
Copy link

@etojeDenys etojeDenys Sep 17, 2024

Choose a reason for hiding this comment

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

do you know how to create useAppSelector and useAppDispatch hooks? better to use them and remove types everywhere

Suggested change
const query = useSelector((state: RootState) => state.filter.query);
const query = useAppSelector(state => state.filter.query);


const handleFilterChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
dispatch(filterActions.setQuery(event.target.value));
};

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" onChange={handleFilterChange}>
<select>
{Object.values(Status).map(status => (
<option key={status} value={status}>
{status}
</option>
))}
</select>
</select>
</span>
</p>
Expand All @@ -22,19 +37,28 @@ export const TodoFilter: React.FC = () => {
type="text"
className="input"
placeholder="Search..."
value={query}
onChange={event => {
dispatch(filterActions.setQuery(event.target.value));
}}

Choose a reason for hiding this comment

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

Consider creating handler function

/>
<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 && (
<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={() => {
dispatch(filterActions.clearQuery());
}}
/>
</span>
)}
</p>
</form>
);
Expand Down
55 changes: 55 additions & 0 deletions src/components/TodoList/TodoItem/TodoItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { useDispatch, useSelector } from 'react-redux';
import { Todo } from '../../../types/Todo';
import { AppDispatch, RootState } from '../../../app/store';
import { currTodoActions } from '../../../features/currentTodo';

type Props = {
todo: Todo;
};

export const TodoItem: React.FC<Props> = ({ todo }) => {
const dispatch = useDispatch<AppDispatch>();
const currentTodo = useSelector((state: RootState) => state.currentTodo);

const { id, completed, title } = todo;

const handleButtonClick = () => {
dispatch(currTodoActions.setCurrentTodo(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 className={completed ? 'has-text-success' : 'has-text-danger'}>
{title}
</p>
</td>

<td className="has-text-right is-vcentered">
<button
data-cy="selectButton"
className="button"
type="button"
onClick={handleButtonClick}
>
<span className="icon">
<i
className={
currentTodo?.id === id ? 'far fa-eye-slash' : 'far fa-eye'
}
/>
</span>
</button>
</td>
</tr>
);
};
1 change: 1 addition & 0 deletions src/components/TodoList/TodoItem/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './TodoItem';
Loading
Loading