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

add task solution #1041

Open
wants to merge 3 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 @@ -17,5 +17,5 @@ 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://pakhomovalex.github.io/react_redux-list-of-todos/)
- Follow the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline)
46 changes: 30 additions & 16 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
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 { useAppDispatch, useAppSelector } from './utils/hooks';
import { setTodos } from './features/todos';

export const App = () => (
<>
<div className="section">
<div className="container">
<div className="box">
<h1 className="title">Todos:</h1>
export const App = () => {
const [isLoading, setIsLoading] = useState(true);
const currentTodo = useAppSelector(state => state.currentTodo.currentTodo);
const dispatch = useAppDispatch();

<div className="block">
<TodoFilter />
</div>
useEffect(() => {
getTodos().then(todosFromServer => {
setIsLoading(false);
dispatch(setTodos(todosFromServer));
});
}, [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">{isLoading ? <Loader /> : <TodoList />}</div>
</div>
</div>
</div>
</div>

<TodoModal />
</>
);
{currentTodo && <TodoModal />}
</>
);
};
15 changes: 10 additions & 5 deletions src/app/store.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { combineSlices, configureStore } from '@reduxjs/toolkit';

const rootReducer = combineSlices();
import { configureStore } from '@reduxjs/toolkit';
import currentTodo from '../features/currentTodo';
import filterSlice from '../features/filter';
import todosSlice from '../features/todos';

export const store = configureStore({
reducer: rootReducer,
reducer: {
currentTodo: currentTodo,
filter: filterSlice,
todos: todosSlice,
},
});

export type RootState = ReturnType<typeof rootReducer>;
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
41 changes: 32 additions & 9 deletions src/components/TodoFilter/TodoFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import React from 'react';
import { useAppDispatch, useAppSelector } from '../../utils/hooks';
import { queryFilter, statusFilter } from '../../features/filter';

export const TodoFilter: React.FC = () => {
const dispatch = useAppDispatch();
const status = useAppSelector(state => state.filter.status);
const query = useAppSelector(state => state.filter.query);

const handleCompleted = (event: React.ChangeEvent<HTMLSelectElement>) => {
dispatch(statusFilter(event.target.value));
};

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

return (
<form
className="field has-addons"
onSubmit={event => event.preventDefault()}
>
<p className="control">
<span className="select">
<select data-cy="statusSelect">
<select
data-cy="statusSelect"
onChange={handleCompleted}
value={status}
>
<option value="all">All</option>
<option value="active">Active</option>
<option value="completed">Completed</option>
Expand All @@ -22,19 +40,24 @@ export const TodoFilter: React.FC = () => {
type="text"
className="input"
placeholder="Search..."
value={query}
onChange={handleQuery}
/>
<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 > 0 && (
<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(queryFilter(''))}
/>
</span>
)}
</p>
</form>
);
Expand Down
Loading
Loading