Skip to content

Commit

Permalink
performed destructuring and added using a class library
Browse files Browse the repository at this point in the history
  • Loading branch information
BurchakDmitry committed Oct 30, 2024
1 parent a27bfdb commit ca2dcc9
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const App: React.FC = () => {
</div>
</div>

{currentTodo && (
{currentTodo?.id && (
<TodoModal currentTodo={currentTodo} setCurrentTodo={setCurrentTodo} />
)}
</>
Expand Down
2 changes: 1 addition & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const getActive = () =>
export const getCompleted = () =>
getTodos().then(response => response.filter(todo => todo.completed));

export const getFinded = (text: string) =>
export const getFounded = (text: string) =>
getTodos().then(response =>
response.filter(todo =>
todo.title.toLowerCase().includes(text.toLowerCase()),
Expand Down
7 changes: 7 additions & 0 deletions src/components/Option/Option.tsx
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>;
};
1 change: 1 addition & 0 deletions src/components/Option/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Option';
11 changes: 6 additions & 5 deletions src/components/TodoFilter/TodoFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Todo } from '../../types/Todo';
import { getAll, getActive, getCompleted, getFinded } from '../../api';
import { getAll, getActive, getCompleted, getFounded } from '../../api';
import { useState, useEffect } from 'react';
import { Option } from '../Option';

interface Props {
setTodos: (value: Todo[]) => void;
Expand All @@ -26,7 +27,7 @@ export const TodoFilter: React.FC<Props> = ({ setTodos }) => {
let filteredTodos = await loadTodosByFilter();

if (inputSearch.trim()) {
filteredTodos = await getFinded(inputSearch);
filteredTodos = await getFounded(inputSearch);

if (filter === 'active') {
filteredTodos = filteredTodos.filter(todo => !todo.completed);
Expand Down Expand Up @@ -64,9 +65,9 @@ export const TodoFilter: React.FC<Props> = ({ setTodos }) => {
onChange={handleSelectChange}
value={filter}
>
<option value="all">All</option>
<option value="active">Active</option>
<option value="completed">Completed</option>
{['All', 'Active', 'Completed'].map(item => (
<Option optionValue={item} key={item} />
))}
</select>
</span>
</p>
Expand Down
15 changes: 12 additions & 3 deletions src/components/TodoItem/TodoItem.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import classNames from 'classnames';
import { Todo } from '../../types/Todo';

interface Props {
Expand All @@ -20,7 +21,7 @@ export const TodoItem: React.FC<Props> = ({
return (
<tr data-cy="todo" className="">
<td className="is-vcentered">{id}</td>
{todo.completed ? (
{completed ? (
<td className="is-vcentered">
<span className="icon" data-cy="iconCompleted">
<i className="fas fa-check" />
Expand All @@ -30,7 +31,12 @@ export const TodoItem: React.FC<Props> = ({
<td className="is-vcentered" />
)}
<td className="is-vcentered is-expanded">
<p className={completed ? 'has-text-success' : 'has-text-danger'}>
<p
className={classNames({
'has-text-danger': !completed,
'has-text-success': completed,
})}
>
{title}
</p>
</td>
Expand All @@ -43,7 +49,10 @@ export const TodoItem: React.FC<Props> = ({
>
<span className="icon">
<i
className={`far ${currentTodo?.id === id ? 'fa-eye-slash' : 'fa-eye'} `}
className={classNames('far', {
'fa-eye': currentTodo?.id !== id,
'fa-eye-slash': currentTodo?.id === id,
})}
/>
</span>
</button>
Expand Down
14 changes: 7 additions & 7 deletions src/components/TodoModal/TodoModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Loader } from '../Loader';
import { getUser } from '../../api';
import { User } from '../../types/User';
import { Todo } from '../../types/Todo';
import classNames from 'classnames';

interface Props {
currentTodo: Todo;
Expand All @@ -13,7 +14,7 @@ export const TodoModal: React.FC<Props> = ({ currentTodo, setCurrentTodo }) => {
const [loader, setLoader] = useState(true);
const [user, setUser] = useState<User | null>(null);

const { userId, completed, id } = currentTodo;
const { userId, completed, id, title } = currentTodo;

useEffect(() => {
(async () => {
Expand Down Expand Up @@ -56,16 +57,15 @@ export const TodoModal: React.FC<Props> = ({ currentTodo, setCurrentTodo }) => {

<div className="modal-card-body">
<p className="block" data-cy="modal-title">
{currentTodo?.title}
{title}
</p>

<p className="block" data-cy="modal-user">
<strong
className={
currentTodo?.completed
? 'has-text-success'
: 'has-text-danger'
}
className={classNames({
'has-text-danger': !completed,
'has-text-success': completed,
})}
>
{completed ? 'Done' : 'Planned'}
</strong>
Expand Down

0 comments on commit ca2dcc9

Please sign in to comment.