-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Solution #2671
base: master
Are you sure you want to change the base?
Solution #2671
Changes from 1 commit
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,77 @@ | ||||||
/* eslint-disable max-len */ | ||||||
import React from 'react'; | ||||||
|
||||||
import React, { useEffect, 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 { Todo } from './types/Todo'; | ||||||
import { getTodos, getUser } from './api'; | ||||||
import { User } from './types/User'; | ||||||
|
||||||
export const App: React.FC = () => { | ||||||
const [todos, setTodos] = useState<Todo[]>([]); | ||||||
const [loading, setLoading] = useState(false); | ||||||
const [selectedTodo, setSelectedTodo] = useState<Todo | null>(null); | ||||||
const [user, setUser] = useState<User | null>(null); | ||||||
const [filteredField, setFilteredField] = useState<string>('All'); | ||||||
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. all/active/completed are magic values |
||||||
const [query, setQuery] = useState<string>(''); | ||||||
|
||||||
const preparedTodos = todos.filter(todo => { | ||||||
if (filteredField === 'completed') { | ||||||
return todo.completed === true; | ||||||
} | ||||||
|
||||||
if (filteredField === 'active') { | ||||||
return todo.completed === 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. Recommendation: 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.
Good point |
||||||
|
||||||
return true; | ||||||
}); | ||||||
|
||||||
const filteredTodos = preparedTodos.filter(todo => { | ||||||
if (query) { | ||||||
return todo.title.toLowerCase().includes(query.toLowerCase()); | ||||||
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. If you don't have query you don't need to filter at all |
||||||
} | ||||||
|
||||||
return true; | ||||||
}); | ||||||
|
||||||
useEffect(() => { | ||||||
setLoading(true); | ||||||
|
||||||
getTodos() | ||||||
.then(setTodos) | ||||||
.finally(() => setLoading(false)); | ||||||
}, []); | ||||||
|
||||||
const handelTodo = (todo: Todo) => { | ||||||
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. There is a typo in the function name 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.
Suggested change
typo in word |
||||||
setLoading(true); | ||||||
setSelectedTodo(todo); | ||||||
|
||||||
getUser(todo.userId) | ||||||
.then(setUser) | ||||||
.finally(() => setLoading(false)); | ||||||
}; | ||||||
|
||||||
const handleChangeStatus = (newStatus: string) => { | ||||||
setFilteredField(newStatus); | ||||||
}; | ||||||
|
||||||
const handleChangeQuery = (event: React.ChangeEvent<HTMLInputElement>) => { | ||||||
setQuery(event.target.value); | ||||||
}; | ||||||
|
||||||
const onReset = () => { | ||||||
setQuery(''); | ||||||
setFilteredField('All'); | ||||||
setSelectedTodo(null); | ||||||
setUser(null); | ||||||
}; | ||||||
|
||||||
return ( | ||||||
<> | ||||||
<div className="section"> | ||||||
|
@@ -17,18 +80,33 @@ export const App: React.FC = () => { | |||||
<h1 className="title">Todos:</h1> | ||||||
|
||||||
<div className="block"> | ||||||
<TodoFilter /> | ||||||
<TodoFilter | ||||||
onChangeStatus={handleChangeStatus} | ||||||
query={query} | ||||||
onChangeQuery={handleChangeQuery} | ||||||
onReset={onReset} | ||||||
/> | ||||||
</div> | ||||||
|
||||||
<div className="block"> | ||||||
<Loader /> | ||||||
<TodoList /> | ||||||
{loading && <Loader />} | ||||||
<TodoList | ||||||
todos={filteredTodos} | ||||||
showSelectedTodo={handelTodo} | ||||||
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 function |
||||||
selectedTodo={selectedTodo} | ||||||
/> | ||||||
</div> | ||||||
</div> | ||||||
</div> | ||||||
</div> | ||||||
|
||||||
<TodoModal /> | ||||||
{selectedTodo && ( | ||||||
<TodoModal | ||||||
user={user} | ||||||
todo={selectedTodo} | ||||||
loading={loading} | ||||||
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. Recommendation: The TodoModal component should have a personal loading state. When you click on the Todo button, the loadings duplicate. 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.
Good point |
||||||
onReset={onReset} | ||||||
/> | ||||||
)} | ||||||
</> | ||||||
); | ||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import classNames from 'classnames'; | ||
import { Todo } from '../../types/Todo'; | ||
import React from 'react'; | ||
|
||
type Props = { | ||
todo: Todo; | ||
selectedTodo: Todo | null; | ||
showSelectedTodo: (todo: Todo) => void; | ||
}; | ||
|
||
export const TodoItem: React.FC<Props> = props => { | ||
const { todo, showSelectedTodo, selectedTodo } = props; | ||
|
||
return ( | ||
<tr data-cy="todo" className=""> | ||
<td className="is-vcentered">{todo.id}</td> | ||
<td className="is-vcentered"> | ||
{todo.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': todo.completed, | ||
'has-text-danger': !todo.completed, | ||
})} | ||
> | ||
{todo.title} | ||
</p> | ||
</td> | ||
<td className="has-text-right is-vcentered"> | ||
<button | ||
data-cy="selectButton" | ||
className="button" | ||
type="button" | ||
onClick={() => showSelectedTodo(todo)} | ||
> | ||
{todo.id === selectedTodo?.id ? ( | ||
<span className="icon"> | ||
<i className="far fa-eye-slash" /> | ||
</span> | ||
) : ( | ||
<span className="icon"> | ||
<i className="far fa-eye" /> | ||
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. It's almost the same in both cases, only one class is different |
||
</span> | ||
)} | ||
</button> | ||
</td> | ||
</tr> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,25 @@ | ||
export const TodoFilter = () => ( | ||
import React from 'react'; | ||
|
||
type Props = { | ||
query: string; | ||
onChangeStatus: (newStatus: string) => void; | ||
onChangeQuery: (event: React.ChangeEvent<HTMLInputElement>) => void; | ||
onReset: () => void; | ||
}; | ||
|
||
export const TodoFilter: React.FC<Props> = ({ | ||
query, | ||
onChangeStatus, | ||
onChangeQuery, | ||
onReset, | ||
}) => ( | ||
<form className="field has-addons"> | ||
<p className="control"> | ||
<span className="select"> | ||
<select data-cy="statusSelect"> | ||
<select | ||
data-cy="statusSelect" | ||
onChange={event => onChangeStatus(event.target.value)} | ||
> | ||
<option value="all">All</option> | ||
<option value="active">Active</option> | ||
<option value="completed">Completed</option> | ||
|
@@ -16,15 +33,23 @@ export const TodoFilter = () => ( | |
type="text" | ||
className="input" | ||
placeholder="Search..." | ||
value={query} | ||
onChange={onChangeQuery} | ||
/> | ||
<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 && ( | ||
<span className="icon is-right" style={{ pointerEvents: 'all' }}> | ||
<button | ||
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. Avoid inline styles, use classes instead |
||
data-cy="clearSearchButton" | ||
type="button" | ||
className="delete" | ||
onClick={onReset} | ||
/> | ||
</span> | ||
)} | ||
</p> | ||
</form> | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,41 @@ | ||
import React from 'react'; | ||
import { Todo } from '../../types/Todo'; | ||
import { TodoItem } from '../Todo/Todo'; | ||
|
||
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[]; | ||
selectedTodo: Todo | null; | ||
showSelectedTodo: (todo: Todo) => void; | ||
}; | ||
|
||
<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"> | ||
<span className="icon"> | ||
<i className="far fa-eye" /> | ||
</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> | ||
export const TodoList: React.FC<Props> = props => { | ||
const { todos, showSelectedTodo, selectedTodo } = props; | ||
|
||
<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"> | ||
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> | ||
</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 => ( | ||
<TodoItem | ||
key={todo.id} | ||
todo={todo} | ||
showSelectedTodo={showSelectedTodo} | ||
selectedTodo={selectedTodo} | ||
/> | ||
))} | ||
</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.
Boolean variables should start from
is
orhas