Skip to content

Commit

Permalink
add task solution
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin19939 committed Sep 22, 2024
1 parent 71932b1 commit a603430
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 296 deletions.
51 changes: 34 additions & 17 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,43 @@
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 { useDispatch } from 'react-redux';
import { Todo } from './types/Todo';
import { todosSlice } from './features/todos';
import { getTodos } from './api';
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 [loading, setLoading] = useState(false);
const currentTodo = useAppSelector(state => state.currentTodo);
const dispatch = useDispatch();

<div className="block">
<TodoFilter />
</div>
useEffect(() => {
setLoading(true);
getTodos()
.then((data: Todo[]) => dispatch(todosSlice.actions.setTodos(data)))
.finally(() => setLoading(false));
}, [dispatch]);

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

<div className="block">
<Loader />
<TodoList />
<div className="block">
<TodoFilter />
</div>
<div className="block">
{loading && <Loader />}
{!loading && <TodoList />}
</div>
</div>
</div>
</div>
</div>

<TodoModal />
</>
);
{currentTodo && <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;
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 { todosSlice } from '../features/todos';
import { currentTodoSlice } from '../features/currentTodo';
import { filterSlice } from '../features/filter';

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

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

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

const handleSelect = (event: React.ChangeEvent<HTMLSelectElement>) => {
dispatch(filterSlice.actions.setStatus(event.target.value));
};

const ChangeQuery = (event: React.ChangeEvent<HTMLInputElement>) => {
dispatch(filterSlice.actions.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"
value={filter.status}
onChange={handleSelect}
>
<option value={Status.All}>All</option>
<option value={Status.Active}>Active</option>
<option value={Status.Completed}>Completed</option>
</select>
</span>
</p>

<p className="control is-expanded has-icons-left has-icons-right">
<input
data-cy="searchInput"
value={query}
type="text"
className="input"
placeholder="Search..."
onChange={ChangeQuery}
/>
<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={() => dispatch(filterSlice.actions.setQuery(''))}
/>
</span>
)}
</p>
</form>
);
Expand Down
Loading

0 comments on commit a603430

Please sign in to comment.