-
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
added solution #2699
base: master
Are you sure you want to change the base?
added solution #2699
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,5 +1,5 @@ | ||
/* 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'; | ||
|
||
|
@@ -8,7 +8,25 @@ 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 [loading, setLoading] = useState(true); | ||
const [currentTodo, setCurrentTodo] = useState<Todo | null>(null); | ||
const [todos, setTodos] = useState<Todo[]>([]); | ||
const [allTodos, setAllTodos] = useState<Todo[]>(); | ||
|
||
useEffect(() => { | ||
if (!allTodos) { | ||
getTodos().then(setAllTodos, setTodos); | ||
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 |
||
} | ||
|
||
if (todos.length > 0) { | ||
setLoading(false); | ||
} | ||
}, [allTodos, todos]); | ||
|
||
return ( | ||
<> | ||
<div className="section"> | ||
|
@@ -17,18 +35,27 @@ export const App: React.FC = () => { | |
<h1 className="title">Todos:</h1> | ||
|
||
<div className="block"> | ||
<TodoFilter /> | ||
<TodoFilter allTodos={allTodos} setTodos={setTodos} /> | ||
</div> | ||
|
||
<div className="block"> | ||
<Loader /> | ||
<TodoList /> | ||
{loading ? ( | ||
<Loader /> | ||
) : ( | ||
<TodoList | ||
todos={todos} | ||
currentTodo={currentTodo} | ||
setCurrentTodo={setCurrentTodo} | ||
/> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<TodoModal /> | ||
{currentTodo && ( | ||
<TodoModal currentTodo={currentTodo} setCurrentTodo={setCurrentTodo} /> | ||
)} | ||
</> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
interface Props { | ||
optionValue: string; | ||
} | ||
|
||
export const Option: React.FC<Props> = ({ optionValue }) => { | ||
return <option value={optionValue.toLowerCase()}>{optionValue}</option>; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Option'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,94 @@ | ||
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 { Todo } from '../../types/Todo'; | ||
import { useState, useEffect } from 'react'; | ||
import { Option } from '../Option'; | ||
|
||
interface Props { | ||
allTodos?: Todo[]; | ||
setTodos: (value: Todo[]) => void; | ||
} | ||
|
||
type FilterType = 'all' | 'active' | 'completed'; | ||
|
||
export const TodoFilter: React.FC<Props> = ({ allTodos, setTodos }) => { | ||
const [inputSearch, setInputSearch] = useState<string>(''); | ||
const [close, setClose] = useState(false); | ||
const [filter, setFilter] = useState<FilterType>('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. The |
||
|
||
useEffect(() => { | ||
if (allTodos) { | ||
let newTodos = [...allTodos]; | ||
|
||
if (filter === 'active') { | ||
newTodos = allTodos?.filter(todo => !todo.completed); | ||
} else if (filter === 'completed') { | ||
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 use of optional chaining |
||
newTodos = allTodos?.filter(todo => todo.completed); | ||
} else { | ||
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 use of optional chaining |
||
newTodos = allTodos; | ||
} | ||
|
||
if (inputSearch.trim()) { | ||
newTodos = newTodos.filter(todo => | ||
todo.title.toLowerCase().includes(inputSearch.toLowerCase()), | ||
); | ||
} | ||
|
||
setTodos(newTodos); | ||
} | ||
}, [inputSearch, allTodos, filter, setTodos]); | ||
|
||
const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setInputSearch(event.target.value); | ||
setClose(!!event.target.value); | ||
}; | ||
|
||
const handleCloseClick = () => { | ||
setInputSearch(''); | ||
setClose(false); | ||
}; | ||
|
||
const handleSelectChange = (event: React.ChangeEvent<HTMLSelectElement>) => { | ||
setFilter(event.target.value as FilterType); | ||
}; | ||
|
||
return ( | ||
<form className="field has-addons"> | ||
<p className="control"> | ||
<span className="select"> | ||
<select | ||
data-cy="statusSelect" | ||
onChange={handleSelectChange} | ||
value={filter} | ||
> | ||
{['All', 'Active', 'Completed'].map(item => ( | ||
<Option optionValue={item} key={item} /> | ||
))} | ||
</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={inputSearch} | ||
onChange={handleInputChange} | ||
/> | ||
<span className="icon is-left"> | ||
<i className="fas fa-magnifying-glass" /> | ||
</span> | ||
{close && ( | ||
<span className="icon is-right" style={{ pointerEvents: 'all' }}> | ||
<button | ||
data-cy="clearSearchButton" | ||
type="button" | ||
className="delete" | ||
onClick={handleCloseClick} | ||
/> | ||
</span> | ||
)} | ||
</p> | ||
</form> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import classNames from 'classnames'; | ||
import { Todo } from '../../types/Todo'; | ||
|
||
interface Props { | ||
todo: Todo; | ||
currentTodo: Todo | null; | ||
setCurrentTodo: (todo: Todo) => void; | ||
} | ||
|
||
export const TodoItem: React.FC<Props> = ({ | ||
todo, | ||
currentTodo, | ||
setCurrentTodo, | ||
}) => { | ||
const handleClick = () => { | ||
setCurrentTodo(todo); | ||
}; | ||
|
||
const { title, id, completed } = todo; | ||
|
||
return ( | ||
<tr data-cy="todo" className=""> | ||
<td className="is-vcentered">{id}</td> | ||
{completed ? ( | ||
<td className="is-vcentered"> | ||
<span className="icon" data-cy="iconCompleted"> | ||
<i className="fas fa-check" /> | ||
</span> | ||
</td> | ||
) : ( | ||
<td className="is-vcentered" /> | ||
)} | ||
<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={handleClick} | ||
> | ||
<span className="icon"> | ||
<i | ||
className={classNames('far', { | ||
'fa-eye': currentTodo?.id !== id, | ||
'fa-eye-slash': currentTodo?.id === id, | ||
})} | ||
/> | ||
</span> | ||
</button> | ||
</td> | ||
</tr> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './TodoItem'; |
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.
Consider initializing
allTodos
with an empty array[]
instead ofundefined
. This will prevent potential issues when accessingallTodos
before it is set.