-
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
dynamic list of todos #2758
base: master
Are you sure you want to change the base?
dynamic list of todos #2758
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,45 @@ | ||
/* eslint-disable max-len */ | ||
import React from 'react'; | ||
import React, { useEffect, useMemo, useState } from 'react'; | ||
import 'bulma/css/bulma.css'; | ||
import '@fortawesome/fontawesome-free/css/all.css'; | ||
|
||
import { TodoList } from './components/TodoList'; | ||
import { TodoFilter } from './components/TodoFilter'; | ||
import { TodoModal } from './components/TodoModal'; | ||
import { Loader } from './components/Loader'; | ||
import { getTodos } from './api'; | ||
import { Todo } from './types/Todo'; | ||
import { FilterStatus } from './types/FilterStatus'; | ||
|
||
export const App: React.FC = () => { | ||
const [todosFromAPI, setTodosFromAPI] = useState<Todo[]>([]); | ||
const [loading, setLoading] = useState(false); | ||
const [selectedId, setSelectedId] = useState<number | null>(null); | ||
const [query, setQuery] = useState(''); | ||
const [filterStatus, setFilterStatus] = useState(FilterStatus.All); | ||
|
||
useEffect(() => { | ||
setLoading(true); | ||
getTodos() | ||
.then(setTodosFromAPI) | ||
.finally(() => setLoading(false)); | ||
}, []); | ||
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 dependency array for |
||
|
||
const filteredTodos = useMemo(() => { | ||
return todosFromAPI.filter(todo => { | ||
const matchesStatus = | ||
filterStatus === FilterStatus.All || | ||
(filterStatus === FilterStatus.Active && !todo.completed) || | ||
(filterStatus === FilterStatus.Completed && todo.completed); | ||
|
||
const matchesQuery = todo.title | ||
.toLowerCase() | ||
.includes(query.toLowerCase()); | ||
|
||
return matchesStatus && matchesQuery; | ||
}); | ||
}, [todosFromAPI, filterStatus, query]); | ||
|
||
return ( | ||
<> | ||
<div className="section"> | ||
|
@@ -17,18 +48,33 @@ export const App: React.FC = () => { | |
<h1 className="title">Todos:</h1> | ||
|
||
<div className="block"> | ||
<TodoFilter /> | ||
<TodoFilter | ||
status={filterStatus} | ||
setStatus={setFilterStatus} | ||
query={query} | ||
setQuery={setQuery} | ||
/> | ||
</div> | ||
|
||
<div className="block"> | ||
<Loader /> | ||
<TodoList /> | ||
{loading && <Loader />} | ||
<TodoList | ||
todos={filteredTodos} | ||
onSelect={setSelectedId} | ||
selectedId={selectedId} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<TodoModal /> | ||
{selectedId && ( | ||
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. Using 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. Ensure that the conditional rendering of |
||
<TodoModal | ||
id={selectedId} | ||
todos={filteredTodos} | ||
onClose={setSelectedId} | ||
/> | ||
)} | ||
</> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import classNames from 'classnames'; | ||
import { Todo } from '../../types/Todo'; | ||
import React from 'react'; | ||
|
||
type Props = { | ||
todo: Todo; | ||
selectedId: number | null; | ||
onSelect: (id: number) => void; | ||
}; | ||
|
||
export const PostItem: React.FC<Props> = ({ todo, selectedId, onSelect }) => { | ||
const { id, title, completed } = todo; | ||
|
||
return ( | ||
<tr | ||
data-cy="todo" | ||
className={classNames({ | ||
'has-background-info-light': selectedId === id, | ||
})} | ||
> | ||
<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-success': completed, | ||
'has-text-danger': !completed, | ||
})} | ||
> | ||
{title} | ||
</p> | ||
</td> | ||
<td className="has-text-right is-vcentered"> | ||
<button | ||
data-cy="selectButton" | ||
className="button" | ||
type="button" | ||
onClick={() => onSelect(id)} | ||
> | ||
<span className="icon"> | ||
<i | ||
className={classNames('far', { | ||
'fa-eye': selectedId !== id, | ||
'fa-eye-slash': selectedId === id, | ||
})} | ||
/> | ||
</span> | ||
</button> | ||
</td> | ||
</tr> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,74 @@ | ||
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> | ||
|
||
<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> | ||
|
||
<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> | ||
); | ||
import React from 'react'; | ||
import { FilterStatus } from '../../types/FilterStatus'; | ||
|
||
type Props = { | ||
query: string; | ||
setQuery: (query: string) => void; | ||
status: FilterStatus; | ||
setStatus: (status: FilterStatus) => void; | ||
}; | ||
|
||
export const TodoFilter: React.FC<Props> = ({ | ||
query, | ||
setQuery, | ||
status, | ||
setStatus, | ||
}) => { | ||
function handleTitleChange(event: React.ChangeEvent<HTMLInputElement>) { | ||
const newQuery = event.target.value; | ||
|
||
setQuery(newQuery); | ||
} | ||
|
||
function handleFilterChange(event: React.ChangeEvent<HTMLSelectElement>) { | ||
setStatus(event.target.value as FilterStatus); | ||
} | ||
|
||
function clearInput() { | ||
setQuery(''); | ||
} | ||
|
||
return ( | ||
<form className="field has-addons"> | ||
<p className="control"> | ||
<span className="select"> | ||
<select | ||
data-cy="statusSelect" | ||
value={status} | ||
onChange={handleFilterChange} | ||
> | ||
<option value="all">All</option> | ||
<option value="active">Active</option> | ||
<option value="completed">Completed</option> | ||
</select> | ||
</span> | ||
</p> | ||
|
||
<p className="control is-expanded has-icons-left has-icons-right"> | ||
<input | ||
data-cy="searchInput" | ||
type="text" | ||
value={query} | ||
className="input" | ||
placeholder="Search..." | ||
onChange={handleTitleChange} | ||
/> | ||
<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 */} | ||
{query.length > 0 && ( | ||
<button | ||
data-cy="clearSearchButton" | ||
type="button" | ||
className="delete" | ||
onClick={clearInput} | ||
/> | ||
)} | ||
</span> | ||
</p> | ||
</form> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,39 @@ | ||
import React from 'react'; | ||
import { Todo } from '../../types/Todo'; | ||
import { PostItem } from '../PostItem/PostItem'; | ||
|
||
export const TodoList: React.FC = () => ( | ||
<table className="table is-narrow is-fullwidth"> | ||
<thead> | ||
<tr> | ||
<th>#</th> | ||
<th> | ||
<span className="icon"> | ||
<i className="fas fa-check" /> | ||
</span> | ||
</th> | ||
<th>Title</th> | ||
<th> </th> | ||
</tr> | ||
</thead> | ||
type Props = { | ||
todos: Todo[]; | ||
onSelect: (id: number) => void; | ||
selectedId: number | null; | ||
}; | ||
|
||
<tbody> | ||
<tr data-cy="todo" className=""> | ||
<td className="is-vcentered">1</td> | ||
<td className="is-vcentered" /> | ||
<td className="is-vcentered is-expanded"> | ||
<p className="has-text-danger">delectus aut autem</p> | ||
</td> | ||
<td className="has-text-right is-vcentered"> | ||
<button data-cy="selectButton" className="button" type="button"> | ||
export const TodoList: React.FC<Props> = ({ todos, onSelect, selectedId }) => { | ||
return ( | ||
<table className="table is-narrow is-fullwidth"> | ||
<thead> | ||
<tr> | ||
<th>#</th> | ||
<th> | ||
<span className="icon"> | ||
<i className="far fa-eye" /> | ||
<i className="fas fa-check" /> | ||
</span> | ||
</button> | ||
</td> | ||
</tr> | ||
<tr data-cy="todo" className="has-background-info-light"> | ||
<td className="is-vcentered">2</td> | ||
<td className="is-vcentered" /> | ||
<td className="is-vcentered is-expanded"> | ||
<p className="has-text-danger">quis ut nam facilis et officia qui</p> | ||
</td> | ||
<td className="has-text-right is-vcentered"> | ||
<button data-cy="selectButton" className="button" type="button"> | ||
<span className="icon"> | ||
<i className="far fa-eye-slash" /> | ||
</span> | ||
</button> | ||
</td> | ||
</tr> | ||
|
||
<tr data-cy="todo" className=""> | ||
<td className="is-vcentered">1</td> | ||
<td className="is-vcentered" /> | ||
<td className="is-vcentered is-expanded"> | ||
<p className="has-text-danger">delectus aut autem</p> | ||
</td> | ||
<td className="has-text-right is-vcentered"> | ||
<button data-cy="selectButton" className="button" type="button"> | ||
<span className="icon"> | ||
<i className="far fa-eye" /> | ||
</span> | ||
</button> | ||
</td> | ||
</tr> | ||
</th> | ||
<th>Title</th> | ||
<th> </th> | ||
</tr> | ||
</thead> | ||
|
||
<tr data-cy="todo" className=""> | ||
<td className="is-vcentered">6</td> | ||
<td className="is-vcentered" /> | ||
<td className="is-vcentered is-expanded"> | ||
<p className="has-text-danger"> | ||
qui ullam ratione quibusdam voluptatem quia omnis | ||
</p> | ||
</td> | ||
<td className="has-text-right is-vcentered"> | ||
<button data-cy="selectButton" className="button" type="button"> | ||
<span className="icon"> | ||
<i className="far fa-eye" /> | ||
</span> | ||
</button> | ||
</td> | ||
</tr> | ||
|
||
<tr data-cy="todo" className=""> | ||
<td className="is-vcentered">8</td> | ||
<td className="is-vcentered"> | ||
<span className="icon" data-cy="iconCompleted"> | ||
<i className="fas fa-check" /> | ||
</span> | ||
</td> | ||
<td className="is-vcentered is-expanded"> | ||
<p className="has-text-success">quo adipisci enim quam ut ab</p> | ||
</td> | ||
<td className="has-text-right is-vcentered"> | ||
<button data-cy="selectButton" className="button" type="button"> | ||
<span className="icon"> | ||
<i className="far fa-eye" /> | ||
</span> | ||
</button> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
); | ||
<tbody> | ||
{todos.map(todo => ( | ||
<PostItem | ||
todo={todo} | ||
onSelect={onSelect} | ||
selectedId={selectedId} | ||
key={todo.id} | ||
/> | ||
))} | ||
</tbody> | ||
</table> | ||
); | ||
}; |
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.
Good job on using
null
as the initial value forselectedId
. This helps avoid potential issues with a todo having an ID of0
.