-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Stepan Rad
authored and
Stepan Rad
committed
Sep 23, 2024
1 parent
71932b1
commit fd31fda
Showing
13 changed files
with
353 additions
and
302 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,72 @@ | ||
/* eslint-disable max-len */ | ||
import React, { useEffect, useState } from 'react'; | ||
import 'bulma/css/bulma.css'; | ||
import '@fortawesome/fontawesome-free/css/all.css'; | ||
import { Loader, TodoFilter, TodoList, TodoModal } from './components'; | ||
|
||
export const App = () => ( | ||
<> | ||
<div className="section"> | ||
<div className="container"> | ||
<div className="box"> | ||
<h1 className="title">Todos:</h1> | ||
import { TodoList } from './components/TodoList'; | ||
import { TodoFilter } from './components/TodoFilter'; | ||
import { Todo } from './types/Todo'; | ||
import { getTodos } from './api'; | ||
import { Loader } from './components/Loader'; | ||
import { TodoModal } from './components/TodoModal'; | ||
import { useAppDispatch, useAppSelector } from './app/store'; | ||
import { todosSlice } from './features/todos'; | ||
|
||
<div className="block"> | ||
<TodoFilter /> | ||
</div> | ||
export const App: React.FC = () => { | ||
const todos = useAppSelector(state => state.todos); | ||
const filter = useAppSelector(state => state.filter); | ||
const currentTodo = useAppSelector(state => state.currentTodo); | ||
const [loader, setLoader] = useState(false); | ||
const [searchFilter, setSeacrhFilter] = useState(''); | ||
const dispatch = useAppDispatch(); | ||
|
||
useEffect(() => { | ||
setLoader(true); | ||
getTodos() | ||
.then((data: Todo[]) => dispatch(todosSlice.actions.setTodos(data))) | ||
.finally(() => setLoader(false)); | ||
}, [dispatch]); | ||
|
||
const visibleTodos = () => { | ||
let visTodos = []; | ||
|
||
switch (filter) { | ||
case 'active': | ||
visTodos = todos.filter(todo => !todo.completed); | ||
break; | ||
|
||
case 'completed': | ||
visTodos = todos.filter(todo => todo.completed); | ||
break; | ||
|
||
default: | ||
return todos; | ||
} | ||
|
||
return visTodos.filter(todo => | ||
todo.title.toLowerCase().includes(searchFilter.toLowerCase()), | ||
); | ||
}; | ||
|
||
return ( | ||
<> | ||
<div className="section"> | ||
<div className="container"> | ||
<div className="box"> | ||
<h1 className="title">Todos:</h1> | ||
|
||
<div className="block"> | ||
<TodoFilter setSearchFilter={setSeacrhFilter} /> | ||
</div> | ||
|
||
<div className="block"> | ||
<Loader /> | ||
<TodoList /> | ||
<div className="block"> | ||
{loader ? <Loader /> : <TodoList todos={visibleTodos()} />} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<TodoModal /> | ||
</> | ||
); | ||
{currentTodo && <TodoModal />} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
import { combineSlices, configureStore } from '@reduxjs/toolkit'; | ||
import { todosSlice } from '../features/todos'; | ||
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux'; | ||
import { filterSlice } from '../features/filter'; | ||
import { currentTodoSlice } from '../features/currentTodo'; | ||
|
||
const rootReducer = combineSlices(); | ||
const rootReducer = combineSlices(todosSlice, filterSlice, currentTodoSlice); | ||
|
||
export const store = configureStore({ | ||
reducer: rootReducer, | ||
}); | ||
|
||
export type RootState = ReturnType<typeof rootReducer>; | ||
export type AppDispatch = typeof store.dispatch; | ||
|
||
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector; | ||
export const useAppDispatch = () => useDispatch<AppDispatch>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import classNames from 'classnames'; | ||
import { Todo } from '../../types/Todo'; | ||
import { useAppDispatch } from '../../app/store'; | ||
import { currentTodoSlice } from '../../features/currentTodo'; | ||
|
||
type Props = { | ||
todo: Todo; | ||
currentTodoId: number; | ||
}; | ||
|
||
export const TodoItem: React.FC<Props> = ({ todo, currentTodoId }) => { | ||
const { id, completed, title } = todo; | ||
const dispatch = useAppDispatch(); | ||
|
||
const add = (item: Todo) => { | ||
dispatch(currentTodoSlice.actions.setCurrentTodo(item)); | ||
}; | ||
|
||
return ( | ||
<tr | ||
key={id} | ||
data-cy="todo" | ||
className={classNames({ | ||
'has-background-info-light': id === currentTodoId, | ||
})} | ||
> | ||
<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={classNames({ | ||
'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" | ||
onClick={() => add(todo)} | ||
> | ||
<span className="icon"> | ||
{id !== currentTodoId ? ( | ||
<i className="far fa-eye" /> | ||
) : ( | ||
<i className="far fa-eye-slash" /> | ||
)} | ||
</span> | ||
</button> | ||
</td> | ||
</tr> | ||
); | ||
}; |
Oops, something went wrong.