-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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 #2615
base: master
Are you sure you want to change the base?
solution #2615
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,43 @@ | ||
/* 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 { getTodos } from './api'; | ||
import { Todo } from './types/Todo'; | ||
|
||
export const App: React.FC = () => { | ||
const [todos, setTodos] = useState<Todo[]>([]); | ||
const [isLoaded, setIsLoaded] = useState(false); | ||
const [selectedTodo, setSelectedTodo] = useState<Todo | undefined>(undefined); | ||
const [selectedStatus, setSelectedStatus] = useState('all'); | ||
const [query, setSelectedQuery] = useState(''); | ||
|
||
useEffect(() => { | ||
getTodos().then(result => { | ||
let filteredResult = result; | ||
|
||
if (selectedStatus !== 'all') { | ||
filteredResult = filteredResult.filter(todo => | ||
selectedStatus === 'active' ? !todo.completed : todo.completed, | ||
); | ||
} | ||
|
||
if (query) { | ||
filteredResult = filteredResult.filter(todo => | ||
todo.title.toLowerCase().includes(query.toLowerCase()), | ||
); | ||
} | ||
|
||
setTodos(filteredResult); | ||
setIsLoaded(true); | ||
}); | ||
}, [selectedStatus, query]); | ||
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 props to initialize state directly can lead to unexpected behavior. Consider using a function inside |
||
|
||
return ( | ||
<> | ||
<div className="section"> | ||
|
@@ -17,18 +46,30 @@ export const App: React.FC = () => { | |
<h1 className="title">Todos:</h1> | ||
|
||
<div className="block"> | ||
<TodoFilter /> | ||
<TodoFilter | ||
onSelect={setSelectedStatus} | ||
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 naming convention for event handler props suggests using |
||
onChange={setSelectedQuery} | ||
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 naming convention for event handler props suggests using |
||
/> | ||
</div> | ||
|
||
<div className="block"> | ||
<Loader /> | ||
<TodoList /> | ||
{!isLoaded ? ( | ||
<Loader /> | ||
) : ( | ||
<TodoList | ||
todos={todos} | ||
selectedTodo={selectedTodo} | ||
onSelect={setSelectedTodo} | ||
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 naming convention for event handler props suggests using |
||
/> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<TodoModal /> | ||
{selectedTodo && ( | ||
<TodoModal onClose={setSelectedTodo} todo={selectedTodo} /> | ||
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 naming convention for event handler props suggests using |
||
)} | ||
</> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,62 @@ | ||
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 React, { useState } from 'react'; | ||
|
||
<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> | ||
type Props = { | ||
onSelect: (value: string) => void; | ||
onChange: (value: string) => void; | ||
}; | ||
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 naming convention for event handler props should follow the pattern 'on'. Consider renaming 'onChange' to something more descriptive like 'onInputChange' to clearly indicate what event it handles. |
||
|
||
<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> = ({ onSelect, onChange }) => { | ||
const [value, setValue] = useState(''); | ||
const inputHandler: React.ChangeEventHandler<HTMLInputElement> = event => { | ||
onChange(event.target.value); | ||
setValue(event.target.value); | ||
}; | ||
|
||
const resetInputHandler = () => { | ||
onChange(''); | ||
setValue(''); | ||
}; | ||
|
||
return ( | ||
<form className="field has-addons"> | ||
<p className="control"> | ||
<span className="select"> | ||
<select | ||
data-cy="statusSelect" | ||
onChange={event => onSelect(event.target.value)} | ||
> | ||
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 inline function for the 'onChange' event of the select element could lead to performance issues as a new function is created on every render. Consider defining this function outside the JSX and passing it as a reference. |
||
<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..." | ||
value={value} | ||
onChange={inputHandler} | ||
/> | ||
<span className="icon is-left"> | ||
<i className="fas fa-magnifying-glass" /> | ||
</span> | ||
|
||
{value && ( | ||
<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={resetInputHandler} | ||
/> | ||
</span> | ||
)} | ||
</p> | ||
</form> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,77 @@ | ||
import React from 'react'; | ||
import { Todo } from '../../types/Todo'; | ||
import classNames from 'classnames'; | ||
|
||
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> | ||
interface Props { | ||
todos: Todo[]; | ||
selectedTodo: Todo | undefined; | ||
onSelect: (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"> | ||
export const TodoList: React.FC<Props> = ({ | ||
todos, | ||
selectedTodo: { id: selectedId } = { selectedId: 0 }, | ||
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 destructuring with default values for |
||
onSelect, | ||
}) => { | ||
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(({ id, title, completed, userId }) => ( | ||
<tr | ||
data-cy="todo" | ||
className={`${id === selectedId && 'has-background-info-light'}`} | ||
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. Consider using the |
||
key={id} | ||
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. Generating keys directly from the |
||
> | ||
<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={` | ||
${completed ? 'has-text-success' : 'has-text-danger'} | ||
`} | ||
> | ||
{title} | ||
</p> | ||
</td> | ||
<td className="has-text-right is-vcentered"> | ||
<button | ||
data-cy="selectButton" | ||
className="button" | ||
type="button" | ||
onClick={() => onSelect({ id, title, completed, userId })} | ||
> | ||
<span className="icon"> | ||
<i | ||
className={classNames( | ||
'far', | ||
selectedId === id ? 'fa-eye-slash' : 'fa-eye', | ||
)} | ||
/> | ||
</span> | ||
</button> | ||
</td> | ||
</tr> | ||
))} | ||
</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.
The state setter function here is named
setSelectedQuery
, but it should besetQuery
to match the state variable namequery
. This follows the naming convention for state setter functions.