Skip to content

Commit

Permalink
refacor:fix all the comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ab3MN committed Nov 4, 2024
1 parent fd1d4f1 commit f3e2f62
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 41 deletions.
14 changes: 8 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import { Loader } from './components/Loader';
import { ITodoFilter, Todo } from './types/Todo';
import { getTodos } from './api';
import { getFiltredTodo } from './utils/filterTodo';
import { DEFAULT_FILTER_STATE } from './constants/TodoFilter';

export const App: React.FC = () => {
const [isLoading, setLoading] = useState(true);
const [todos, setTodos] = useState<Todo[]>([]);
const [selectedTodo, setselectedTodo] = useState<null | Todo>(null);
const [todosFilter, setTodosFilter] = useState<ITodoFilter>({
status: '',
filter: '',
});
const [todosFilter, setTodosFilter] =
useState<ITodoFilter>(DEFAULT_FILTER_STATE);

const filtredTodos = useMemo(
() => getFiltredTodo(todos, todosFilter),
Expand All @@ -35,7 +34,7 @@ export const App: React.FC = () => {
// eslint-disable-next-line no-console
console.log(e.message);
});
}, [getTodos]);
}, []);

const closeModal = () => setselectedTodo(null);

Expand All @@ -47,7 +46,10 @@ export const App: React.FC = () => {
<h1 className="title">Todos:</h1>

<div className="block">
<TodoFilter setTodosFilter={setTodosFilter} />
<TodoFilter
setTodosFilter={setTodosFilter}
todosFilter={todosFilter}
/>
</div>

<div className="block">
Expand Down
39 changes: 24 additions & 15 deletions src/components/TodoFilter/TodoFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
import { ChangeEvent, FC, useEffect, useState } from 'react';
import { ChangeEvent, Dispatch, FC, SetStateAction, useState } from 'react';

Check failure on line 1 in src/components/TodoFilter/TodoFilter.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

'useState' is defined but never used
import { ITodoFilter } from '../../types/Todo';
import {
DEFAULT_FILTER_STATE,
TODO_FILTER_OPTIONS,
} from '../../constants/TodoFilter';
import { Filter_Statuses } from '../../utils/enums/FiltersStatus';

interface IProps {
setTodosFilter: (filter: ITodoFilter) => void;
todosFilter: ITodoFilter;
setTodosFilter: Dispatch<SetStateAction<ITodoFilter>>;
}

export const TodoFilter: FC<IProps> = ({ setTodosFilter }) => {
const [status, setStatus] = useState('');
const [filter, setFilter] = useState('');

useEffect(() => {
setTodosFilter({ status, filter });
}, [status, filter]);
export const TodoFilter: FC<IProps> = ({ setTodosFilter, todosFilter }) => {
const { filter, status } = todosFilter;

const handleChangeFilter = (event: ChangeEvent<HTMLInputElement>) => {
setFilter(event.target.value.toLowerCase());
setTodosFilter(prevState => ({
...prevState,
filter: event.target.value.toLowerCase().trim(),
}));
};

const handleChangeStatus = (event: ChangeEvent<HTMLSelectElement>) => {
setStatus(event.target.value);
setTodosFilter(prevState => ({
...prevState,
status: event.target.value as Filter_Statuses,
}));
};

const handleClearFilter = () => {
setFilter('');
setTodosFilter(DEFAULT_FILTER_STATE);
};

return (
Expand All @@ -34,9 +41,11 @@ export const TodoFilter: FC<IProps> = ({ setTodosFilter }) => {
value={status}
onChange={handleChangeStatus}
>
<option value="all">All</option>
<option value="active">Active</option>
<option value="completed">Completed</option>
{TODO_FILTER_OPTIONS.map(({ value, title, id }) => (
<option value={value} key={id}>
{title}
</option>
))}
</select>
</span>
</p>
Expand Down
1 change: 1 addition & 0 deletions src/components/TodoList/TodoList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const TodoList: React.FC<IProps> = ({
<tbody>
{todos.map(todo => {
const { id, completed, title } = todo;

return (
<tr data-cy="todo" className="" key={id}>
<td className="is-vcentered">{id}</td>
Expand Down
15 changes: 11 additions & 4 deletions src/constants/TodoFilter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
export const TODO_FILTER_STATUS = {
all: undefined,
active: false,
completed: true,
import { Filter_Statuses } from '../utils/enums/FiltersStatus';

export const TODO_FILTER_OPTIONS = [
{ value: 'all', title: 'All', id: 1 },
{ value: 'active', title: 'Active', id: 2 },
{ value: 'completed', title: 'Completed', id: 3 },
];

export const DEFAULT_FILTER_STATE = {
status: Filter_Statuses.All,
filter: '',
};
4 changes: 3 additions & 1 deletion src/types/Todo.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Filter_Statuses } from '../utils/enums/FiltersStatus';

export interface Todo {
id: number;
title: string;
Expand All @@ -6,6 +8,6 @@ export interface Todo {
}

export interface ITodoFilter {
status: string;
status: Filter_Statuses;
filter: string;
}
5 changes: 5 additions & 0 deletions src/utils/enums/FiltersStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum Filter_Statuses {

Check failure on line 1 in src/utils/enums/FiltersStatus.ts

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

Enum name `Filter_Statuses` must match one of the following formats: PascalCase
All = 'all',
Active = 'active',
Completed = 'completed',
}
31 changes: 16 additions & 15 deletions src/utils/filterTodo.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { TODO_FILTER_STATUS } from '../constants/TodoFilter';
import { ITodoFilter, Todo } from '../types/Todo';

export const getTodoFilterStatus = (status = '') => {
if (status in TODO_FILTER_STATUS) {
return TODO_FILTER_STATUS[status as keyof typeof TODO_FILTER_STATUS];
}
return undefined;
};
import { Filter_Statuses } from './enums/FiltersStatus';

export const getFiltredTodo = (
todos: Todo[],
{ status, filter }: ITodoFilter,
) => {
const todoStatus = getTodoFilterStatus(status);

return todos.filter(todo =>
todoStatus === undefined
? todo.title.toLowerCase().includes(filter)
: todo.title.toLowerCase().includes(filter) &&
todo.completed === todoStatus,
let filteredTodos = todos.filter(({ title }) =>
title.toLowerCase().trim().includes(filter),
);

filteredTodos = filteredTodos.filter(({ completed }) => {
switch (status) {
case Filter_Statuses.Active:
return !completed;
case Filter_Statuses.Completed:
return completed;
default:
return true;
}
});

return filteredTodos;
};

0 comments on commit f3e2f62

Please sign in to comment.