Skip to content

Commit

Permalink
solution 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Edgar Kocuba committed Oct 21, 2023
1 parent 606d711 commit 614bb50
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 78 deletions.
56 changes: 30 additions & 26 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable jsx-a11y/control-has-associated-label */
import React, {
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { UserWarning } from './UserWarning';
Expand Down Expand Up @@ -30,8 +30,6 @@ export const App: React.FC = () => {
const [editQuery, setEditQuery] = useState('');
const [editQueryPrev, setEditQueryPrev] = useState('');

const inputRef = useRef<HTMLBodyElement>(null);

const handleErrorSet = (errMessage: string) => {
setErrorWarning(errMessage);
setTimeout(() => {
Expand All @@ -43,7 +41,7 @@ export const App: React.FC = () => {
setErrorWarning('');
};

const loadTodos = async () => {
const loadTodos = useCallback(async () => {
try {
const loadedTodos = await getTodos(USER_ID);

Expand All @@ -52,15 +50,11 @@ export const App: React.FC = () => {
} catch {
handleErrorSet(ErrMessage.loadTodo);
}
};

useEffect(() => {
loadTodos();
}, []);

useEffect(() => {
inputRef.current?.focus();
}, [inputRef, pageIsLoaded, todos]);
loadTodos();
}, [loadTodos]);

const visibleTodos = (filterBy: FilterBy) => {
if (todos) {
Expand Down Expand Up @@ -90,21 +84,22 @@ export const App: React.FC = () => {
return;
}

const newTodo = {
id: todos.length + 10 || 10,
userId: USER_ID,
title: query.trim(),
completed: false,
};

setTempTodo({ ...newTodo, id: 0 });

try {
const newTodo = {
userId: USER_ID,
title: query.trim(),
completed: false,
};

setTempTodo({ ...newTodo, id: 0 });
setPageIsLoaded(false);
await addTodo('/todos', newTodo);
setTodos((state) => {
return [...state, newTodo];

const newTodos = await addTodo('/todos', newTodo);

setTodos(state => {
return [...state, newTodos as Todo];
});

setTempTodo(null);
setPageIsLoaded(true);
setQuery('');
Expand Down Expand Up @@ -158,27 +153,35 @@ export const App: React.FC = () => {
// eslint-disable-next-line no-param-reassign
todo.completed = false;
setIsUpdatingId([]);
} else if (!todo.completed) {

return [...stateMain];
}

if (!todo.completed) {
setIsUpdatingId(state => (
[...state, todo.id]
));
await updateTodo(todo.id, { completed: true });
// eslint-disable-next-line no-param-reassign
todo.completed = true;
setIsUpdatingId([]);

return [...stateMain];
}

return [...stateMain];
} catch {
handleErrorSet(ErrMessage.updateTodo);
setIsUpdatingId([]);

return [...stateMain];
}

return [...stateMain];
});

return [...stateMain];
});

setTodos(state => [...state]);
};

const handleDelete = async (id?: number) => {
Expand Down Expand Up @@ -288,10 +291,12 @@ export const App: React.FC = () => {

const completedTodos = useMemo(() => {
return todos.filter(todo => todo.completed).length;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [todos, isUpdatingId]);

const uncompletedTodos = useMemo(() => {
return todos.filter(todo => !todo.completed).length;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [todos, isUpdatingId]);

if (!USER_ID) {
Expand All @@ -311,7 +316,6 @@ export const App: React.FC = () => {
uncompletedTodos={uncompletedTodos}
toggleAll={toggleAll}
pageIsLoaded={pageIsLoaded}
inputRef={inputRef}
/>
{todos && (
<TodoList
Expand Down
10 changes: 7 additions & 3 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useRef } from 'react';
import cn from 'classnames';

type Props = {
Expand All @@ -9,7 +9,6 @@ type Props = {
uncompletedTodos: number,
toggleAll: () => void,
pageIsLoaded: boolean,
inputRef: any,
};

export const Header: React.FC<Props> = React.memo(({
Expand All @@ -20,8 +19,13 @@ export const Header: React.FC<Props> = React.memo(({
uncompletedTodos,
toggleAll,
pageIsLoaded,
inputRef,
}) => {
const inputRef = useRef<HTMLInputElement>(null);

useEffect(() => {
inputRef.current?.focus();
}, [inputRef, pageIsLoaded]);

return (
<header className="todoapp__header">
{/* this buttons is active only if there are some active todos */}
Expand Down
47 changes: 0 additions & 47 deletions src/components/TodoItem.tsx

This file was deleted.

27 changes: 25 additions & 2 deletions src/components/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import cn from 'classnames';
import { Todo } from '../types/Todo';
import { TodoItem } from './TodoItem';

type Props = {
todos: Todo[],
Expand Down Expand Up @@ -124,7 +123,31 @@ export const TodoList: React.FC<Props> = React.memo(({
);
})}
{tempTodo && (
<TodoItem tempTodos={tempTodo} />
<div id={`${tempTodo.id}`} data-cy="Todo" className="todo">
<label className="todo__status-label">
<input
data-cy="TodoStatus"
type="checkbox"
className="todo__status"
checked
readOnly
/>
</label>

<span data-cy="TodoTitle" className="todo__title">
{tempTodo.title}
</span>

<button type="button" className="todo__remove" data-cy="TodoDelete">
×
</button>

{/* 'is-active' class puts this modal on top of the todo */}
<div data-cy="TodoLoader" className="modal overlay is-active">
<div className="modal-background has-background-white-ter" />
<div className="loader" />
</div>
</div>
)}
</section>
);
Expand Down

0 comments on commit 614bb50

Please sign in to comment.