-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
feat(todo): implement modal opening, filtering, and state management #2752
Open
misha-vynnyk
wants to merge
2
commits into
mate-academy:master
Choose a base branch
from
misha-vynnyk:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,14 +1,108 @@ | ||
/* eslint-disable max-len */ | ||
import React from 'react'; | ||
// #region import | ||
import React, { useEffect, useMemo, useState } from 'react'; | ||
import 'bulma/css/bulma.css'; | ||
import '@fortawesome/fontawesome-free/css/all.css'; | ||
import { getTodos, getUser } from './api'; | ||
|
||
import { TodoList } from './components/TodoList'; | ||
import { TodoFilter } from './components/TodoFilter'; | ||
import { TodoModal } from './components/TodoModal'; | ||
import { Loader } from './components/Loader'; | ||
|
||
import { Todo } from './types/Todo'; | ||
// #endregion import | ||
|
||
export enum Filter { | ||
all = 'all', | ||
active = 'active', | ||
completed = 'completed', | ||
} | ||
|
||
export const filterLabels: { [key in Filter]: string } = { | ||
[Filter.all]: 'All', | ||
[Filter.active]: 'Active', | ||
[Filter.completed]: 'Completed', | ||
}; | ||
|
||
export const App: React.FC = () => { | ||
const [todos, setTodos] = useState<Todo[]>([]); | ||
const [currentTodo, setCurrentTodo] = useState<Todo | null>(null); | ||
|
||
const [isModalOpen, setIsModalOpen] = useState(false); | ||
const [openTodoId, setOpenTodoId] = useState<number | null>(null); | ||
|
||
const [todosLoading, setTodosLoading] = useState(false); | ||
const [modalLoading, setModalLoading] = useState(false); | ||
|
||
const [filterValue, setFilterValue] = useState<Filter>(Filter.all); | ||
const [searchTerm, setSearchTerm] = useState(''); | ||
|
||
useEffect(() => { | ||
setTodosLoading(true); | ||
|
||
getTodos() | ||
.then(setTodos) | ||
.finally(() => setTodosLoading(false)); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (!currentTodo || currentTodo.user) { | ||
return; | ||
} | ||
|
||
getUser(currentTodo.userId) | ||
.then(user => { | ||
setCurrentTodo(prevTodo => { | ||
if (prevTodo?.id === currentTodo.id) { | ||
return { ...prevTodo, user }; | ||
} | ||
|
||
return prevTodo; | ||
Comment on lines
+56
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic inside |
||
}); | ||
}) | ||
.finally(() => setModalLoading(false)); | ||
}, [currentTodo]); | ||
|
||
const handleOpenModal = (todo: Todo): void => { | ||
setOpenTodoId(todo.id); | ||
setModalLoading(true); | ||
setIsModalOpen(true); | ||
|
||
setCurrentTodo(todo); | ||
}; | ||
|
||
const filteredAndSearchedTodos = useMemo(() => { | ||
return todos | ||
.filter(todo => { | ||
switch (filterValue) { | ||
case Filter.completed: | ||
return todo.completed; | ||
case Filter.active: | ||
return !todo.completed; | ||
case Filter.all: | ||
default: | ||
return true; | ||
} | ||
}) | ||
.filter(todo => { | ||
return todo.title.toLowerCase().includes(searchTerm.toLowerCase()); | ||
}); | ||
}, [todos, filterValue, searchTerm]); | ||
|
||
const handleSearchChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setSearchTerm(event.target.value); | ||
}; | ||
|
||
const handleCloseModal = (): void => { | ||
setIsModalOpen(false); | ||
setCurrentTodo(null); | ||
}; | ||
|
||
const handleClearSearch = () => { | ||
setSearchTerm(''); | ||
}; | ||
|
||
return ( | ||
<> | ||
<div className="section"> | ||
|
@@ -17,18 +111,33 @@ export const App: React.FC = () => { | |
<h1 className="title">Todos:</h1> | ||
|
||
<div className="block"> | ||
<TodoFilter /> | ||
<TodoFilter | ||
setFilterValue={setFilterValue} | ||
searchTerm={searchTerm} | ||
handleSearchChange={handleSearchChange} | ||
handleClearSearch={handleClearSearch} | ||
/> | ||
</div> | ||
|
||
<div className="block"> | ||
<Loader /> | ||
<TodoList /> | ||
{todosLoading && <Loader />} | ||
<TodoList | ||
todos={filteredAndSearchedTodos} | ||
handleOpenModal={handleOpenModal} | ||
isModalOpen={isModalOpen} | ||
openTodoId={openTodoId} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<TodoModal /> | ||
<TodoModal | ||
currentTodo={currentTodo} | ||
isModalOpen={isModalOpen} | ||
modalLoading={modalLoading} | ||
handleCloseModal={handleCloseModal} | ||
/> | ||
</> | ||
); | ||
}; |
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,30 +1,60 @@ | ||
export const TodoFilter = () => ( | ||
<form className="field has-addons"> | ||
<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> | ||
</span> | ||
</p> | ||
import { Filter, filterLabels } from '../../App'; | ||
|
||
<p className="control is-expanded has-icons-left has-icons-right"> | ||
<input | ||
data-cy="searchInput" | ||
type="text" | ||
className="input" | ||
placeholder="Search..." | ||
/> | ||
<span className="icon is-left"> | ||
<i className="fas fa-magnifying-glass" /> | ||
</span> | ||
interface Props { | ||
setFilterValue: React.Dispatch<React.SetStateAction<Filter>>; | ||
searchTerm: string; | ||
handleSearchChange: (event: React.ChangeEvent<HTMLInputElement>) => void; | ||
handleClearSearch: () => void; | ||
} | ||
|
||
<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> | ||
</p> | ||
</form> | ||
); | ||
export const TodoFilter: React.FC<Props> = ({ | ||
setFilterValue, | ||
searchTerm, | ||
handleSearchChange, | ||
handleClearSearch, | ||
}) => { | ||
return ( | ||
<form className="field has-addons"> | ||
<p className="control"> | ||
<span className="select"> | ||
<select | ||
data-cy="statusSelect" | ||
onChange={event => setFilterValue(event.target.value as Filter)} | ||
> | ||
{Object.values(Filter).map(filter => ( | ||
<option key={filter} value={filter}> | ||
{filterLabels[filter]} | ||
</option> | ||
))} | ||
</select> | ||
</span> | ||
</p> | ||
|
||
<p className="control is-expanded has-icons-left has-icons-right"> | ||
<input | ||
data-cy="searchInput" | ||
type="text" | ||
className="input" | ||
placeholder="Search..." | ||
value={searchTerm} | ||
onChange={handleSearchChange} | ||
/> | ||
<span className="icon is-left"> | ||
<i className="fas fa-magnifying-glass" /> | ||
</span> | ||
|
||
<span className="icon is-right" style={{ pointerEvents: 'all' }}> | ||
{searchTerm && ( | ||
<button | ||
aria-label="Clear search input" | ||
data-cy="clearSearchButton" | ||
type="button" | ||
className="delete" | ||
onClick={handleClearSearch} | ||
/> | ||
)} | ||
</span> | ||
</p> | ||
</form> | ||
); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition
!currentTodo || currentTodo.user
might lead to unexpected behavior ifcurrentTodo.user
is falsy but valid. Ensure this logic correctly reflects the intended check for whether the user data needs to be fetched.